Commit 1de9ebf8 authored by Michael Lopez's avatar Michael Lopez

removed html file in replace of building the announcements page on views.py....

removed html file in replace of building the announcements page on views.py. two nested loops were used
parent 8c532d57
......@@ -3,9 +3,11 @@ from .models import Announcement, Reaction
class AnnouncementAdmin(admin.ModelAdmin):
model = Announcement
list_display = ["title", "body", "author"]
class ReactionAdmin(admin.ModelAdmin):
model = Reaction
list_display = ["announcement", "name", "tally"]
admin.site.register(Announcement, AnnouncementAdmin)
admin.site.register(Reaction, ReactionAdmin)
\ No newline at end of file
......@@ -9,7 +9,7 @@ class Announcement(models.Model):
def __str__(self):
return self.title
class Reaction(models.Model):
LIKE = 'like'
......@@ -28,3 +28,6 @@ class Reaction(models.Model):
tally = models.IntegerField()
announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE)
def __str__(self):
return self.name
<p>Widget's Announcement Board</p>
<p>Announcements:</p>
{% for announcement in announcements %} {% for reaction in reactions %}
<p>
{{ announcement.title }} by {{ announcement.author }} published
{{announcement.pub_datetime }}<br />
{{ announcement.body }}<br />
Like: {{ reaction.tally }}<br />
Love: {{ reaction.tally }}<br />
Angry: {{ reaction.tally }}
</p>
{% endfor %} {% endfor %}
from django.shortcuts import render
from .models import Announcement, Reaction
from django.http import HttpResponse
def pageview(request):
announcements = Announcement.objects.all()
reactions = Reaction.objects.all()
return render(request, 'announcements/announcements.html',
{'announcements': announcements, 'reactions': reactions})
string_builder = "Widget's Announcement Board <br> <br> Announcements: <br> "
for announcement in announcements:
string_builder = "{} <br> {} by {} published {} <br> {}".format(
string_builder, announcement.title, announcement.author, announcement.pub_datetime, announcement.body
)
for reaction in reactions:
if reaction.announcement == announcement:
string_builder = "{} <br> {}: {}".format(string_builder, reaction.name, reaction.tally)
string_builder += "<br>"
return HttpResponse(string_builder)
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