created local branch, added buttons to view page to add new announcements,...

created local branch, added buttons to view page to add new announcements, added buttons to addAnnouncement and detail pages to return to view page, changed index from FBV to CBV
parent adfc2aac
# Generated by Django 3.2.12 on 2022-05-25 14:34
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('Forum', '0006_alter_post_post_imageurl'),
('Forum', '0006_post_post_imageurl'),
]
operations = [
]
from django.forms import ModelForm
from .models import Announcement
class AnnouncementForm(ModelForm):
class Meta:
model = Announcement
fields = ["announcement_title", "announcement_body", "author", "announcement_imageUrl"]
# Generated by Django 3.2.12 on 2022-05-25 14:51
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0007_announcement_announcement_imageurl'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_date',
field=models.DateTimeField(auto_now_add=True, verbose_name='date published'),
),
]
...@@ -5,7 +5,7 @@ from homepage.models import WidgetUser ...@@ -5,7 +5,7 @@ from homepage.models import WidgetUser
class Announcement(models.Model): class Announcement(models.Model):
announcement_title = models.CharField(max_length=100) announcement_title = models.CharField(max_length=100)
announcement_body = models.CharField(max_length=400) announcement_body = models.CharField(max_length=400)
pub_date = models.DateTimeField("date published") pub_date = models.DateTimeField("date published", auto_now_add=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, default=1) author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, default=1)
announcement_imageUrl = models.CharField(max_length=999, default = 1) announcement_imageUrl = models.CharField(max_length=999, default = 1)
......
{% extends "announcements/base.html" %}
{% block content %}
<h1>New Announcement</h1>
<form action="{% url 'announcements:index' %}">
<button type="submit">Go Back</button>
</form>
<form method="POST" action= "{% url 'announcements:addAnnouncement' %}">
{% csrf_token %}
{{announcement_Form.as_p}}
<button class="button" type="submit">Submit Announcement</button>
</form>
{% endblock %}
\ No newline at end of file
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
{% block content %} {% block content %}
<form action="{% url 'announcements:index' %}">
<button type="submit">Go Back</button>
</form>
<h1>{{announcement.announcement_title}}</h1> <h1>{{announcement.announcement_title}}</h1>
<h4>{{announcement.author.first_name}} {{announcement.author.last_name}}, {{announcement.pub_date.day}}/{{announcement.pub_date.month}}/{{announcement.pub_date.year}}</h4> <h4>{{announcement.author.first_name}} {{announcement.author.last_name}}, {{announcement.pub_date.day}}/{{announcement.pub_date.month}}/{{announcement.pub_date.year}}</h4>
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
{% if announcement_list %} {% if announcement_list %}
<ul> <ul>
{% for announcement in announcement_list %} {% for announcement in announcement_list %}
<h3><li><a href ="{% url 'announcement' announcement.id %}">{{announcement.announcement_title}}</a> <h3><li><a href ="{% url 'announcements:announcement' announcement.id %}">{{announcement.announcement_title}}</a>
</br> by {{announcement.author.first_name}} {{announcement.author.last_name}} dated {{announcement.pub_date.day}}/{{announcement.pub_date.month}}/{{announcement.pub_date.year}}</li></h3> </br> by {{announcement.author.first_name}} {{announcement.author.last_name}} dated {{announcement.pub_date.day}}/{{announcement.pub_date.month}}/{{announcement.pub_date.year}}</li></h3>
{% endfor %} {% endfor %}
</ul> </ul>
...@@ -13,4 +13,8 @@ ...@@ -13,4 +13,8 @@
<p>No announcements available.</p> <p>No announcements available.</p>
{% endif %} {% endif %}
<form action="{% url 'announcements:addAnnouncement' %}">
<button type="submit">New Announcement</button>
</form>
{% endblock %} {% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from . import views from . import views
from .views import AnnouncementView
app_name = "announcements"
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), path('', AnnouncementView.as_view(), name='index'),
path("<int:announcement_id>/details/", views.announcement, name='announcement') path("<int:announcement_id>/details/", views.announcement, name='announcement'),
path("posts/add/", views.newAnnouncement,name= 'addAnnouncement')
] ]
from django.http import HttpResponse from django.http import HttpResponse
from . models import Announcement, Reaction from . models import Announcement, Reaction
from . forms import AnnouncementForm
from homepage.models import WidgetUser from homepage.models import WidgetUser
from django.shortcuts import render from django.views import View
from django.shortcuts import render, redirect
# Create your views here. # Create your views here.
def index(request): class AnnouncementView(View):
def get(self, request):
announcement = Announcement.objects.all() announcement = Announcement.objects.all()
reaction = Reaction.objects.all() reaction = Reaction.objects.all()
announcement_list = Announcement.objects.order_by("pub_date") announcement_list = Announcement.objects.order_by("pub_date")
context ={ context ={
'announcement':announcement, 'announcement':announcement,
'reaction':reaction, 'reaction':reaction,
'announcement_list':announcement_list 'announcement_list':announcement_list
} }
return render(request, "announcements/view.html", context) return render(request, "announcements/view.html", context)
def announcement(request, announcement_id): def announcement(request, announcement_id):
try: try:
announcement = Announcement.objects.get(pk=announcement_id) announcement = Announcement.objects.get(pk=announcement_id)
except Announcement.DoesNotExist: except Announcement.DoesNotExist:
raise Http404("Announcement doesnt exist") raise Http404("Announcement doesnt exist")
return render(request, "announcements/announcement.html", {"announcement": announcement}) return render(request, "announcements/announcement.html", {"announcement": announcement})
\ No newline at end of file
def newAnnouncement(request):
announcement_Form = AnnouncementForm()
if request.method == "POST":
announcement_Form = AnnouncementForm(request.POST)
if announcement_Form.is_valid():
announcement_Form.save()
return redirect("announcements:addAnnouncement")
else:
announcement_Form = AnnouncementForm()
return render(request, "announcements/addAnnouncement.html", {"announcement_Form":announcement_Form})
\ No newline at end of file
No preview for this file type
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