Commit 3c4a7fcf authored by Neal Berones's avatar Neal Berones

edited announcement model and views, tested new forms

parent 3699ce39
...@@ -8,5 +8,5 @@ class AnnouncementForm(ModelForm): ...@@ -8,5 +8,5 @@ class AnnouncementForm(ModelForm):
"announcement_title", "announcement_title",
"announcement_body", "announcement_body",
"author", "author",
"image" "image",
] ]
# Generated by Django 4.0.3 on 2022-05-21 06:47
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0004_alter_announcement_image'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_date',
field=models.DateTimeField(null=True, verbose_name='date published'),
),
]
# Generated by Django 4.0.3 on 2022-05-21 06:55
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('announcements', '0005_alter_announcement_pub_date'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_date',
field=models.DateTimeField(default=django.utils.timezone.now, null=True, verbose_name='date published'),
),
]
# Generated by Django 4.0.3 on 2022-05-21 07:17
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '0009_alter_widgetuser_profile_url'),
('announcements', '0006_alter_announcement_pub_date'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='author',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='homepage.widgetuser'),
),
]
# Generated by Django 4.0.3 on 2022-05-21 07:22
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '0009_alter_widgetuser_profile_url'),
('announcements', '0007_alter_announcement_author'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='author',
field=models.ForeignKey(blank=True, default=None, null=True, on_delete=django.db.models.deletion.CASCADE, to='homepage.widgetuser'),
),
]
# Generated by Django 4.0.3 on 2022-05-21 07:29
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0008_alter_announcement_author'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='image',
field=models.FileField(blank=True, null=True, upload_to='static/announcements'),
),
]
from django.db import models from django.db import models
from django.utils import timezone
from homepage.models import WidgetUser from homepage.models import WidgetUser
import os import os
...@@ -8,11 +9,11 @@ Angry = 'Angry' ...@@ -8,11 +9,11 @@ Angry = 'Angry'
REACTION_CHOICES = ((Like, 'Like'), (Love, 'Love'), (Angry, 'Angry')) REACTION_CHOICES = ((Like, 'Like'), (Love, 'Love'), (Angry, 'Angry'))
# Create your models here. # Create your models here.
class Announcement(models.Model): class Announcement(models.Model):
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True) author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True, blank=True, default=None)
announcement_title = models.CharField(max_length=50) announcement_title = models.CharField(max_length=50)
announcement_body = models.CharField(max_length=500) announcement_body = models.CharField(max_length=500)
pub_date = models.DateTimeField("date published") pub_date = models.DateTimeField("date published", default=timezone.now, null=True)
image = models.FileField(upload_to='static/announcements', null=True) image = models.FileField(upload_to='static/announcements', null=True, blank=True)
def __str__(self): def __str__(self):
return self.announcement_title return self.announcement_title
......
...@@ -3,23 +3,11 @@ ...@@ -3,23 +3,11 @@
{% block page-title %}Add New Announcement{% endblock %} {% block page-title %}Add New Announcement{% endblock %}
{% block content %} {% block content %}
<script> <form method="POST" action="{% url 'announcements:add' %}" enctype="multipart/form-data">
function createUserOptions(){ {% csrf_token %}
var useroptions = "<option value = "0">Select</option>"; {{ announcement_form.as_p }}
for (user in user_list){
useroptions += "<option value="+user.first_name+">"+user.first_name+" "+user.last_name+"</option>"
}
document.getElementById('author').innerHTML = useroptions; <button class="button" type="submit">Save Announcement</button>
} <button onclick="location.href = '/announcements'">Return to Announcements Page</button>
</script>
<h1>New Announcement</h1>
<form action="announcements/add" method="POST">
<h2>Announcement Title: </h2><input type="text" name="announcement_title">
<h2>Announcement Body: </h2><input type="text" name="announcement_body">
<h2>
<label for="author">Author</label>
<select id="author"></select>
</h2>
</form> </form>
{% endblock %} {% endblock %}
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
<form method="get" action="announcements/new/"> <form method="get" action="add/">
<button type="submit">New Announcement</button> <button type="submit">New Announcement</button>
</form> </form>
</body> </body>
......
...@@ -4,7 +4,7 @@ from . import views ...@@ -4,7 +4,7 @@ from . import views
app_name = "announcements" app_name = "announcements"
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), path('', views.index, name='index'),
path('announcements/add/', views.add, name='add'), path('add/', views.add, name='add'),
path('<int:announcement_id>/details', views.detail, name="detail") path('<int:announcement_id>/details', views.detail, name="detail")
#path('welcome', views.welcome, name='welcome') #path('welcome', views.welcome, name='welcome')
] ]
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
import datetime import datetime
from django.shortcuts import render from django.shortcuts import render, redirect
from .models import WidgetUser, Announcement, Reaction from .models import WidgetUser, Announcement, Reaction
from .forms import AnnouncementForm
from django.template import loader from django.template import loader
# Create your views here. # Create your views here.
def index(request): def index(request):
announcements_list = Announcement.objects.all().order_by("pub_date") announcements_list = Announcement.objects.all().order_by("pub_date")
...@@ -41,14 +43,13 @@ def detail(request, announcement_id): ...@@ -41,14 +43,13 @@ def detail(request, announcement_id):
def add(request): def add(request):
if request.method == "POST": if request.method == "POST":
announcement_title = request.POST.get("announcement_title") announcement_form = AnnouncementForm(request.POST, request.FILES)
announcement_body = request.POST.get("announcement_body") if announcement_form.is_valid():
author = request.POST.get("author") new_announcement = announcement_form.save()
image = request.POST.get("image") return redirect("announcements:add")
pub_date = datetime.date.today() else:
announcement = Announcement.objects.create(announcement_title = announcement_title, announcement_body = announcement_body, author = author, image = image, pub_date = pub_date) announcement_form = AnnouncementForm()
messages.success(request, "Announcement Saved.") return render(request, "announcements/add.html", {"announcement_form":announcement_form})
return render(request, "")
# Code from previous lab activities: # Code from previous lab activities:
# def welcome(request): # def welcome(request):
......
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