Commit f96c5861 authored by Junho Park's avatar Junho Park

feat: added announcement adding options

parent 82b996f8
from statistics import mode
from django.forms import ModelForm
from .models import Announcement
class AnnouncementForm(ModelForm):
class Meta:
model = Announcement
fields = ["announcement_title", "announcement_body", "author", "image"]
# Generated by Django 4.0.4 on 2022-05-26 18:29
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0007_alter_reaction_announcement'),
]
operations = [
migrations.AddField(
model_name='announcement',
name='image',
field=models.ImageField(blank=True, default=None, null=True, upload_to='images/'),
),
]
# Generated by Django 4.0.4 on 2022-05-26 19:00
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('announcements', '0008_announcement_image'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_date',
field=models.DateTimeField(blank=True, default=django.utils.timezone.now, verbose_name='date published'),
),
]
# Generated by Django 4.0.4 on 2022-05-26 19:13
import datetime
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0009_alter_announcement_pub_date'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_date',
field=models.DateTimeField(blank=True, default=datetime.datetime.now, verbose_name='date published'),
),
]
# Generated by Django 4.0.4 on 2022-05-26 19:18
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0010_alter_announcement_pub_date'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_date',
field=models.DateTimeField(auto_now=True, verbose_name='date published'),
),
]
from django.db import models
from homepage.models import WidgetUser
# Create your models here.
class Announcement(models.Model):
announcement_title = models.CharField(max_length=100)
announcement_body = models.CharField(max_length=1000)
pub_date = models.DateTimeField('date published')
pub_date = models.DateTimeField('date published', auto_now=True)
author = models.ForeignKey(
'homepage.WidgetUser',
on_delete=models.CASCADE,
default=None,
null=True
)
image = models.ImageField(
null=True,
blank=True,
upload_to="images/",
default=None,
)
def __str__(self):
return self.announcement_title
......
......@@ -28,10 +28,11 @@ h3 {
ol {
text-align: center;
font-family: 'Courier New', Courier, monospace;
}
p {
color:#15133C;
color: #15133C;
font-weight: bold;
font-family: 'Courier New', Courier, monospace;
text-align: center;
......@@ -47,7 +48,6 @@ body {
a {
color: #EC994B;
font-size: 20px;
font-family: 'Courier New', Courier, monospace;
}
img {
......
{% extends "announcements/base.html" %}
{% block content %}
<h1>Add Assignment</h1>
<form method="POST" enctype="multipart/form-data" action="{% url 'announcements:add' %}">
{% csrf_token %}
{{announcement_form.media}}
{{ announcement_form.as_p }}
<button class="button" type="submit">Save Announcement</button>
</form>
<br>
<button type="button"> <a href="/announcements/" id = "back"> Back to Main Announcements Page</a> </button>
{% endblock %}
......@@ -10,14 +10,8 @@
<br/>
<div>
{% if announcement.id == 1 %}
<img src = "{% static 'announcements/images/headache.jpeg' %}" alt = ":o">
{% elif announcement.id == 2 %}
<img src = "{% static 'announcements/images/backpain.jpeg' %}" alt = "hehe>">
{% elif announcement.id == 3 %}
<img src = "{% static 'announcements/images/fear.jpeg' %}" alt = "hehe>">
{% elif announcement.id == 4 %}
<img src = "{% static 'announcements/images/sore_eyes.jpeg' %}" alt = "profile image">
{% if announcement.image %}
<img src="{{ announcement.image.url }}">
{% endif %}
</div>
......
......@@ -7,13 +7,15 @@
<h2>Important announcements:</h2>
{% if announcement_list %}
<ul>
{% for announcement in announcement_list %}
<ol><a href="/announcements/{{ announcement.id }}/details/">{{ announcement.announcement_title }} by {{ announcement.author.first_name }} {{ announcement.author.last_name }} dated {{ announcement.pub_date }}</a></ol>
{% endfor %}
</ul>
{% else %}
<p>No announcements are available.</p>
{% endif %}
<button type="button"> <a href="{% url 'announcements:add' %}" id="new"> New Announcement</a> </button>
{% endblock %}
......@@ -3,7 +3,9 @@ from . import views
urlpatterns = [
path("", views.index, name='Announcements'),
path("<int:announcement_id>/details/", views.details, name="details")
path("<int:announcement_id>/details/", views.details, name="details"),
path("add/", views.add, name='add'),
]
app_name = 'announcements'
from django.http import HttpResponse, Http404
from .models import Announcement, Reaction
from django.shortcuts import render
from django.shortcuts import render,redirect
from .forms import AnnouncementForm
# Create your views here.
......@@ -25,3 +26,17 @@ def details(request, announcement_id):
raise Http404("Announcement does not exist.")
return render(request, "Announcements/detail.html", {"announcement": announcement, "angry": angry, "love": love, "like":like})
#announcements/add
def add(request):
if request.method == "POST":
announcement_form = AnnouncementForm(request.POST, request.FILES)
if announcement_form.is_valid():
new_announcement = announcement_form.save()
return redirect("announcements:add")
else:
announcement_form = AnnouncementForm()
context = {"announcement_form": announcement_form}
return render(request, "announcements/add.html", context)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment