Commit 777b3744 authored by nekopilar's avatar nekopilar

added views for forum posts and replies

parent b963cf3b
# Generated by Django 4.0.3 on 2022-04-03 13:42
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('forum', '0002_post_author_reply'),
]
operations = [
migrations.AddField(
model_name='reply',
name='post',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='forum.post'),
),
]
......@@ -12,5 +12,6 @@ class Reply(models.Model):
reply_body = models.TextField()
pub_date = models.DateField(auto_now_add=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True)
post = models.ForeignKey(Post, on_delete=models.CASCADE, null=True)
\ No newline at end of file
from django.shortcuts import render
from django.http import HttpResponse
from .models import Post, Reply
# View for forum page
def index(request):
return HttpResponse("Welcome to Widget's Forum!")
return HttpResponse(display_forumposts(Post.objects.all(), Reply.objects.all()))
def display_forumposts(post_data, reply_data):
display_output = "FORUM POSTS: <br>"
for object in post_data:
post_title = object.post_title
post_first_name = object.author.first_name
post_last_name = object.author.last_name
post_pub_date = str(object.pub_date)
post_body = object.post_body
reply_objects = Reply.objects.filter(post_id=object.id).all()
display_output += f'''
{post_title} by {post_first_name} {post_last_name} dated {post_pub_date}:<br>
{post_body}<br>
'''
for objects in reply_objects:
display_output += f'''
Reply by {objects.author.first_name} {objects.author.last_name} dated {objects.pub_date}:<br>
{objects.reply_body}<br>
'''
display_output += "<br>"
return display_output
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