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
class Announcement(models.Model):
announcement_title = models.CharField(max_length=100)
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)
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 @@
{% block content %}
<form action="{% url 'announcements:index' %}">
<button type="submit">Go Back</button>
</form>
<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>
......
......@@ -5,7 +5,7 @@
{% if announcement_list %}
<ul>
{% 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>
{% endfor %}
</ul>
......@@ -13,4 +13,8 @@
<p>No announcements available.</p>
{% endif %}
<form action="{% url 'announcements:addAnnouncement' %}">
<button type="submit">New Announcement</button>
</form>
{% endblock %}
\ No newline at end of file
from django.urls import path
from . import views
from .views import AnnouncementView
app_name = "announcements"
urlpatterns = [
path('', views.index, name='index'),
path("<int:announcement_id>/details/", views.announcement, name='announcement')
path('', AnnouncementView.as_view(), name='index'),
path("<int:announcement_id>/details/", views.announcement, name='announcement'),
path("posts/add/", views.newAnnouncement,name= 'addAnnouncement')
]
from django.http import HttpResponse
from . models import Announcement, Reaction
from . forms import AnnouncementForm
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.
def index(request):
class AnnouncementView(View):
def get(self, request):
announcement = Announcement.objects.all()
reaction = Reaction.objects.all()
......@@ -22,3 +25,14 @@ def announcement(request, announcement_id):
except Announcement.DoesNotExist:
raise Http404("Announcement doesnt exist")
return render(request, "announcements/announcement.html", {"announcement": announcement})
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