Created the forum app with corresponding models, views, admin, and urls

parent 6d06d3c0
from django.contrib import admin
# Register your models here.
from .models import ForumPost, Reply
class ForumPostAdmin(admin.ModelAdmin):
model = ForumPost
list_display = ('title', 'pub_datetime',)
search_fields = ('title',)
list_filter = ('title', 'pub_datetime',)
class ReplyAdmin(admin.ModelAdmin):
model = Reply
list_display = ('pub_datetime',)
list_filter = ('pub_datetime',)
admin.site.register(ForumPost, ForumPostAdmin)
admin.site.register(Reply, ReplyAdmin)
from django.apps import AppConfig
class ForumConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'forum'
# Generated by Django 3.2 on 2023-03-05 08:33
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='ForumPost',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(default='', max_length=50, null=True, unique=True)),
('body', models.TextField(default='', null=True)),
('pub_datetime', models.DateTimeField(null=True)),
],
),
migrations.CreateModel(
name='Reply',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('body', models.TextField(default='', null=True)),
('pub_datetime', models.DateTimeField(null=True)),
],
),
]
# Generated by Django 3.2 on 2023-03-05 13:44
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('forum', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='reply',
name='replies',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='forum.forumpost'),
),
]
# Generated by Django 3.2 on 2023-03-05 14:07
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('forum', '0002_reply_replies'),
]
operations = [
migrations.RenameField(
model_name='reply',
old_name='replies',
new_name='replied_post',
),
]
from django.db import models
# Create your models here.
class ForumPost(models.Model):
title = models.CharField(max_length = 50, unique = True, default = "", null = True,)
body = models.TextField(default = "", null = True,)
# author = models.ForeignKey (dashboard, on_delete = models.CASCADE)
pub_datetime = models.DateTimeField(null = True,)
def __str__(self):
return self.title
class Reply(models.Model):
body = models.TextField(default = "", null = True,)
# author = models.ForeignKey(dashboard, on_delete = models.CASCADE)
pub_datetime = models.DateTimeField(null = True,)
replied_post = models.ForeignKey(ForumPost, on_delete = models.CASCADE, null = True,)
from django.test import TestCase
# Create your tests here.
from django.urls import path
from.import views
urlpatterns = [
path('', views.forum, name = "forum"),
]
\ No newline at end of file
from .models import ForumPost, Reply
from django.http import HttpResponse
# Create your views here.
def forum(request):
posts = ForumPost.objects.all()
replies = Reply.objects.all()
response = "Widget's Forums <br> <br>" + "Forum Posts: <br>"
for post in posts:
post_name = "test"
#post.author.first_name + " " + post.author.last_name
response += post.title + " by " + post_name + " posted " + post.pub_datetime.strftime("%a, %b %d, %Y %I:%M %p") + ": <br>" + post.body + "<br>"
for reply in replies:
if(post.title == reply.replied_post.title):
reply_name = "reply test"
#reply.author.first_name + " " + reply.author.last_name
response += "Reply by " + reply_name + " posted " + reply.pub_datetime.strftime("%a, %b %d, %Y %I:%M %p") + ": <br>" + reply.body + "<br>"
return HttpResponse(response)
...@@ -37,6 +37,7 @@ INSTALLED_APPS = [ ...@@ -37,6 +37,7 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'forum.apps.ForumConfig',
] ]
MIDDLEWARE = [ MIDDLEWARE = [
......
...@@ -14,8 +14,9 @@ Including another URLconf ...@@ -14,8 +14,9 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
from django.contrib import admin from django.contrib import admin
from django.urls import path from django.urls import path, include
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('forum/', include("forum.urls"))
] ]
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