Commit bfa7a979 authored by Rac Gerard Elizaga's avatar Rac Gerard Elizaga

Updated forum Post Model, added forum Reply Model in forum/models.py. Updated...

Updated forum Post Model, added forum Reply Model in forum/models.py. Updated views.py and added template file to display all Posts and related Replies.
parent a16aa04b
from django.contrib import admin from django.contrib import admin
from .models import Post from .models import Post, Reply
class PostAdmin(admin.ModelAdmin): class PostAdmin(admin.ModelAdmin):
model = Post model = Post
readonly_fields = ('pub_date',) readonly_fields = ('pub_date',)
class ReplyAdmin(admin.ModelAdmin):
model = Reply
readonly_fields = ('pub_date',)
admin.site.register(Post, PostAdmin) admin.site.register(Post, PostAdmin)
admin.site.register(Reply, ReplyAdmin)
\ No newline at end of file
# Generated by Django 4.0.3 on 2022-03-31 04:35
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '0001_initial'),
('forum', '0003_alter_post_pub_date'),
]
operations = [
migrations.AddField(
model_name='post',
name='author',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='homepage.widgetuser'),
),
migrations.AlterField(
model_name='post',
name='post_body',
field=models.CharField(max_length=1000),
),
migrations.AlterField(
model_name='post',
name='pub_date',
field=models.DateField(auto_now_add=True, verbose_name='date published'),
),
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=1000)),
('pub_date', models.DateField(auto_now_add=True, verbose_name='date published')),
('author', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='homepage.widgetuser')),
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='forum.post')),
],
),
]
# Generated by Django 4.0.3 on 2022-03-31 04:39
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0004_post_author_alter_post_post_body_alter_post_pub_date_and_more'),
]
operations = [
migrations.AlterField(
model_name='post',
name='pub_date',
field=models.DateField(auto_now_add=True),
),
migrations.AlterField(
model_name='reply',
name='pub_date',
field=models.DateField(auto_now_add=True),
),
]
# Generated by Django 4.0.3 on 2022-03-31 04:59
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0005_alter_post_pub_date_alter_reply_pub_date'),
]
operations = [
migrations.AlterField(
model_name='post',
name='pub_date',
field=models.DateTimeField(auto_now_add=True, verbose_name='date published'),
),
migrations.AlterField(
model_name='reply',
name='pub_date',
field=models.DateTimeField(auto_now_add=True, verbose_name='date published'),
),
]
from django.db import models from django.db import models
from homepage.models import WidgetUser
class Post(models.Model): class Post(models.Model):
post_title = models.CharField(max_length=50) post_title = models.CharField(max_length=50)
post_body = models.CharField(max_length=100) post_body = models.CharField(max_length=1000)
pub_date = models.DateTimeField('date published', auto_now_add=True) pub_date = models.DateTimeField('date published', auto_now_add=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null = True)
def __str__(self): def __str__(self):
return self.post_title return self.post_title
class Reply(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE)
reply_body = models.CharField(max_length=1000)
pub_date = models.DateTimeField('date published', auto_now_add=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null = True)
\ No newline at end of file
FORUM POSTS:<br>
{% for i in post %}
{{i.post_title}} by {{i.author.first_name}} {{i.author.last_name}} dated {{i.pub_date.date|date:"m/d/Y"}}:<br>
{{i.post_body}}<br>
{% for j in reply %}
{% if j.post == i %}
Reply by {{j.author.first_name}} {{j.author.last_name}} dated {{j.pub_date.date|date:"m/d/Y"}}:<br>
{{j.reply_body}}<br>
{% endif %}
{% endfor %}
<br>
{% endfor %}
\ No newline at end of file
from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from .models import Post, Reply
def index(request): def index(request):
return HttpResponse("Welcome to Widget's Forum!") post = Post.objects.all()
\ No newline at end of file reply = Reply.objects.all()
return render(request, 'forum.html', {'post': post, 'reply': reply})
#display_message = "FORUM POSTS: \n"
#for i in Post.objects.all():
# display_message += f"{i.post_title}"
#return HttpResponse(display_message)
\ 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