Commit 9f5b222f authored by Mario Franco C. Deuna's avatar Mario Franco C. Deuna

Models and Views for Forum

updated models and added reply class and its fields. added views in forum to display posts and reply.
parent cf8d3ce5
from django.contrib import admin from django.contrib import admin
# Register your models here. # Register your models here.
from .models import Post from .models import Post, Reply
class PostAdmin(admin.ModelAdmin): class PostAdmin(admin.ModelAdmin):
model = Post model = Post
list_display = ('post_title', 'pub_date') list_display = ('post_title', 'pub_date')
class ReplyAdmin(admin.ModelAdmin):
model = Reply
admin.site.register(Post, PostAdmin) admin.site.register(Post, PostAdmin)
admin.site.register(Reply, ReplyAdmin)
# Generated by Django 4.0.3 on 2022-04-03 15:19
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '0001_initial'),
('forum', '0002_alter_post_pub_date'),
]
operations = [
migrations.AddField(
model_name='post',
name='author',
field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, to='homepage.widgetuser'),
preserve_default=False,
),
migrations.CreateModel(
name='Reply',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('reply_body', models.CharField(max_length=500)),
('pub_date', models.DateField(auto_now_add=True)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='homepage.widgetuser')),
],
),
]
from django.db import models from django.db import models
from homepage.models import WidgetUser
# Create your models here. # Create your models here.
class Post(models.Model): class Post(models.Model):
post_title=models.CharField(max_length=30) post_title=models.CharField(max_length=30)
post_body=models.TextField(max_length=500) post_body=models.TextField(max_length=500)
pub_date=models.DateField(auto_now_add=True) pub_date=models.DateField(auto_now_add=True)
author=models.ForeignKey(
WidgetUser,
on_delete=models.CASCADE
)
def __str__(self): def __str__(self):
return f"{self.post_title} {self.post_body} {self.pub_date}" return f"{self.post_title} {self.post_body} {self.pub_date}"
class Reply(models.Model):
reply_body = models.CharField(max_length=500)
pub_date = models.DateField(auto_now_add=True)
author=models.ForeignKey(
WidgetUser,
on_delete=models.CASCADE
)
def __str__(self):
return f"{self.reply_body} {self.pub_date}"
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from .models import Post, Reply
from homepage.models import WidgetUser
def get_reply():
text=''
for reply in Reply.objects.all():
text += (
'Reply by {} {} '.format(reply.author.first_name,
reply.author.last_name)
+ 'dated {}').format(reply.pub_date.strftime('%x')
+ ':<br>'
+ '{}<br>'.format(reply.reply_body)
+ '<br>'
)
return text
def forum(request): def forum(request):
return HttpResponse("Welcome to Widget's Forum!") text = '<h1>FORUM POSTS:</h1>'
for post in Post.objects.all():
text += (
'{} '.format(post.post_title)
+ 'by {} {} '.format(post.author.first_name,
post.author.last_name)
+ 'dated {}').format(post.pub_date.strftime('%x')
+ ':<br>'
+ '{}<br>'.format(post.post_body)
+ get_reply()
)
return HttpResponse(text)
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