create Reply model, update and migrate Forum models, update Forum views.py,...

create Reply model, update and migrate Forum models, update Forum views.py, registered new model in admin
parent 076243d0
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
admin.site.register(Post) admin.site.register(Post)
admin.site.register(Reply)
\ No newline at end of file
# Generated by Django 3.2.12 on 2022-03-24 17:35
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('Homepage', '0001_initial'),
('Forum', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='post',
name='author',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='Homepage.widgetuser'),
),
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.DateTimeField(auto_now_add=True, verbose_name='date published')),
('author', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='Homepage.widgetuser')),
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='Forum.post')),
],
),
]
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=50) post_title = models.CharField(max_length=50)
post_body = models.CharField(max_length=500) post_body = models.CharField(max_length=500)
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, default=1)
class Reply(models.Model):
reply_body = models.CharField(max_length=500)
pub_date = models.DateTimeField("date published", auto_now_add=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, default=1)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
\ No newline at end of file
from django.http import HttpResponse from django.http import HttpResponse
from .models import Post, Reply
def get_readable_time(timestamp):
return timestamp.strftime("%m/%d/%Y")
def index(request): def index(request):
return HttpResponse('Welcome to Widget\'s Forum!') out = "FORUM POSTS: <br />"
\ No newline at end of file
posts = Post.objects.all()
for post in posts:
out += f'{post.post_title} by {post.author.first_name} {post.author.last_name} dated {get_readable_time(post.pub_date)}: <br />'
out += f'{post.post_body} <br />'
post_replies = post.reply_set.all()
for reply in post_replies:
out += f'Reply by {reply.author.first_name} {reply.author.last_name} dated {get_readable_time(reply.pub_date)}: <br />'
out += f'{reply.reply_body} <br />'
out += '<br />'
return HttpResponse(out)
\ 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