Commit 61d4015a authored by Andre Dalwin's avatar Andre Dalwin

Implemented models, views, and admin for Forum

parent fd90c34d
{
"python.analysis.typeCheckingMode": "basic"
}
from django.contrib import admin
# Register your models here.
from .models import ForumPost, Reply
class ForumPostAdmin(admin.ModelAdmin):
model = ForumPost
list_display = ('title', 'author', 'body','pub_datetime')
class ReplyAdmin(admin.ModelAdmin):
model = Reply
list_display = ('forum', 'author', 'body','pub_datetime')
admin.site.register(ForumPost, ForumPostAdmin)
admin.site.register(Reply, ReplyAdmin)
\ No newline at end of file
from django.db import models
# if need to import WidgetUser
# from widget_casanatics.dashboard.models import WidgetUser
# Create your models here.
class ForumPost(models.Model):
title = models.CharField(max_length=50)
body = models.CharField(max_length=1000)
author = models.CharField(max_length = 100)
# If connecting to WidgetUser
# author = models.ForeignKey(WidgetUser, on_delete = models.CASCADE)
pub_datetime = models.DateTimeField("Publication Date and Time")
def __str__(self):
return self.title
class Reply(models.Model):
forum = models.ForeignKey(ForumPost, on_delete = models.CASCADE)
body = models.CharField(max_length=1000)
author = models.CharField(max_length = 100)
# If connecting to WidgetUser
# author = models.ForeignKey(WidgetUser, on_delete = models.CASCADE)
pub_datetime = models.DateTimeField("Publication Date and Time")
def __str__(self):
return self.body
from django.urls import path
from . import views
urlpatterns = [
path("", views.forum, name="index"),
]
from django.shortcuts import render
from django.http import HttpResponse
from .models import ForumPost, Reply
# Create your views here.
def forum(request):
forum_view=""
# fetch forum posts and replies
forum = ForumPost.objects.all()
replies = Reply.objects.all()
forum_view = "Widget's Forum <br> <br> Forum Posts:<br>"
for post in forum:
forum_view += '''
{} by {} posted {}: <br>
{} <br>
'''.format(
post.title,
post.author,
post.pub_datetime.strftime("%m/%d/%Y, %I:%M %p"),
post.body,
)
# Check if reply is for that specific forum_post
for reply in replies:
if reply.forum == post:
forum_view += '''
Reply by {} posted {}: <br>
{} <br>
'''.format(
reply.author,
reply.pub_datetime.strftime("%m/%d/%Y, %I:%M %p"),
reply.body
)
forum_view += "<br>"
return HttpResponse(forum_view)
\ No newline at end of file
......@@ -14,8 +14,9 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include
urlpatterns = [
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