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):
"announcement_title",
"announcement_body",
"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.utils import timezone
from homepage.models import WidgetUser
import os
......@@ -8,11 +9,11 @@ Angry = 'Angry'
REACTION_CHOICES = ((Like, 'Like'), (Love, 'Love'), (Angry, 'Angry'))
# Create your models here.
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_body = models.CharField(max_length=500)
pub_date = models.DateTimeField("date published")
image = models.FileField(upload_to='static/announcements', null=True)
pub_date = models.DateTimeField("date published", default=timezone.now, null=True)
image = models.FileField(upload_to='static/announcements', null=True, blank=True)
def __str__(self):
return self.announcement_title
......
......@@ -3,23 +3,11 @@
{% block page-title %}Add New Announcement{% endblock %}
{% block content %}
<script>
function createUserOptions(){
var useroptions = "<option value = "0">Select</option>";
for (user in user_list){
useroptions += "<option value="+user.first_name+">"+user.first_name+" "+user.last_name+"</option>"
}
<form method="POST" action="{% url 'announcements:add' %}" enctype="multipart/form-data">
{% csrf_token %}
{{ announcement_form.as_p }}
document.getElementById('author').innerHTML = useroptions;
}
</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>
<button class="button" type="submit">Save Announcement</button>
<button onclick="location.href = '/announcements'">Return to Announcements Page</button>
</form>
{% endblock %}
......@@ -16,7 +16,7 @@
</li>
{% endfor %}
</ul>
<form method="get" action="announcements/new/">
<form method="get" action="add/">
<button type="submit">New Announcement</button>
</form>
</body>
......
......@@ -4,7 +4,7 @@ from . import views
app_name = "announcements"
urlpatterns = [
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('welcome', views.welcome, name='welcome')
]
from django.http import HttpResponse, Http404
import datetime
from django.shortcuts import render
from django.shortcuts import render, redirect
from .models import WidgetUser, Announcement, Reaction
from .forms import AnnouncementForm
from django.template import loader
# Create your views here.
def index(request):
announcements_list = Announcement.objects.all().order_by("pub_date")
......@@ -41,14 +43,13 @@ def detail(request, announcement_id):
def add(request):
if request.method == "POST":
announcement_title = request.POST.get("announcement_title")
announcement_body = request.POST.get("announcement_body")
author = request.POST.get("author")
image = request.POST.get("image")
pub_date = datetime.date.today()
announcement = Announcement.objects.create(announcement_title = announcement_title, announcement_body = announcement_body, author = author, image = image, pub_date = pub_date)
messages.success(request, "Announcement Saved.")
return render(request, "")
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()
return render(request, "announcements/add.html", {"announcement_form":announcement_form})
# Code from previous lab activities:
# 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