Commit 65ecb061 authored by Alliyah Marcelo's avatar Alliyah Marcelo

Configured views.py that displays content of the forum app

parent 6e0df981
# Generated by Django 4.1.7 on 2023-03-04 15:09
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('forum', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='reply',
name='forumpost',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='forumpost_reply', to='forum.forumpost'),
),
]
from django.db import models from django.db import models
class ForumPost(models.Model): class ForumPost(models.Model):
title = models.CharField(max_length=100) title = models.CharField(max_length=100)
body = models.TextField() body = models.TextField()
...@@ -27,7 +28,7 @@ class Reply(models.Model): ...@@ -27,7 +28,7 @@ class Reply(models.Model):
related_name='reply_author' related_name='reply_author'
) )
pub_datetime = models.DateTimeField() pub_datetime = models.DateTimeField()
forumpost = models.OneToOneField( forumpost = models.ForeignKey(
ForumPost, ForumPost,
on_delete=models.CASCADE, on_delete=models.CASCADE,
related_name='forumpost_reply' related_name='forumpost_reply'
......
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
#from .models import ForumPost, Reply from .models import ForumPost, Reply
def index(request): def index(request):
return HttpResponse('Sample muna.') return_string = "<p>Widget's Forum</p>Forum Posts:"
for post in ForumPost.objects.all():
counter = 0
for reply in Reply.objects.all():
if counter == 0 and post.title == reply.forumpost.title:
return_string += '<br>{} by {} {} posted {}<br>{}'.format(
reply.forumpost.title,
reply.forumpost.author.first_name,
reply.forumpost.author.last_name,
reply.forumpost.pub_datetime.strftime('%m/%d/%Y, %H:%M %p:'),
reply.forumpost.body,
)
return_string += '<br>Reply by {} {} posted {}<br>{}'.format(
reply.author.first_name,
reply.author.last_name,
reply.pub_datetime.strftime('%m/%d/%Y, %H:%M %p:'),
reply.body,
)
counter += 1
continue
elif counter == 1 and post.title == reply.forumpost.title:
return_string += '<br>Reply by {} {} posted {}<br>{}'.format(
reply.author.first_name,
reply.author.last_name,
reply.pub_datetime.strftime('%m/%d/%Y, %H:%M %p:'),
reply.body,
)
continue
elif counter == 1 and post.title != reply.forumpost.title:
return_string += '<br>'
break
else:
continue
html_string = '<html><body>{}</body></html>'.format(return_string)
return HttpResponse(html_string)
\ No newline at end of file
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