Created forum.html and ForumPostDetailView

parent 7d762344
# forum/models.py
from django.db import models
from dashboard.models import WidgetUser
from django.urls import reverse
class ForumPost(models.Model):
title = models.CharField(max_length=100)
......@@ -19,12 +20,17 @@ class ForumPost(models.Model):
def format_pub_datetime(self):
return self.pub_datetime.strftime('%m/%d/%Y %I:%M %p')
def get_absolute_url(self):
return reverse('forum:forumpost-details', kwargs={'pk': self.pk})
class Reply(models.Model):
post = models.ForeignKey(
ForumPost,
null=True,
default=True,
on_delete=models.CASCADE,
related_name="replies",
)
body = models.CharField(max_length=200)
author = models.ForeignKey(
......
{% extends 'base.html' %}
{% load static %}
{% block title %} Widget's Forum {% endblock %}
{% block heading %}
<h1> Welcome to Widget's Forum! </h1>
{% endblock %}
{% block content %}
<h2> Forum Posts: </h2>
<ul>
{% for forumpost in forumposts %}
<li>
<a href="{{ forumpost.get_absolute_url }}"> {{ forumpost.title }} by {{ forumpost.author.first_name }} {{ forumpost.author.last_name }} </a>
</li>
{% endfor %}
</ul>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %} {{ object.title }} {% endblock %}
{% block heading %}
<h1> {{ object.title }} </h1>
<h2> by {{ object.author.first_name }} {{ object.author.last_name }} </h2>
<h4> {{ object.format_pub_datetime }} </h4>
<p> {{ object.body }} </p>
{% endblock %}
{% block content %}
<hr>
<h2> Post Replies: </h2>
{% for reply in object.replies.all %}
<p> by {{ reply.author.first_name }} {{ reply.author.last_name }} </p>
<p> {{ reply.format_pub_datetime }} </p>
<p> {{ reply.body }} </p>
<br>
{% endfor %}
{% endblock %}
\ No newline at end of file
# forum/urls.py
from django.urls import path
from .views import index
from .views import index, ForumPostDetailView
urlpatterns = [
path('', index, name='index'),
path('forumposts/<int:pk>/details', ForumPostDetailView.as_view(), name='forumpost-details'),
]
# This might be needed, depending on your Django version
......
# appname/views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import ForumPost, Reply
from dashboard.models import WidgetUser
def index(request):
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
head = "<h1 style='border-bottom:4px solid lightgray;\
padding-bottom:30px;\
font-size:450%;'>\
Widget Forum\
</h1>"
body = "<h2>Forum Posts:</h2>"
from dashboard.models import WidgetUser
from .models import ForumPost, Reply
for post in ForumPost.objects.all():
body += "<div style='border: 2px solid gray; border-radius:5px; padding:20px 30px;'>\
<b>{}</b> by {} {} posted {}:\
<br>\
{}".format(post.title, post.author.first_name, post.author.last_name, post.format_pub_datetime(), post.body)
for reply in Reply.objects.all():
if reply.post == post:
body += "<p style='border: 1px dashed gray; border-radius:5px; padding:20px 30px;'>\
Reply by {} {} posted {}:\
<br>\
{}".format(reply.author.first_name, reply.author.last_name, reply.format_pub_datetime(), reply.body)
body += "</div>"
body += "<p>&nbsp;</p>"
return_string = "<html>\
<body style = 'font-family:helvetica;\
padding:30px;'>\
{}{}\
</body></html>".format(head, body)
def index(request):
forumposts = ForumPost.objects.all()
replies = Reply.objects.all()
return render(request, 'forum/forum.html', {'forumposts': forumposts, 'replies': replies})
return HttpResponse(return_string)
class ForumPostDetailView(DetailView):
model = ForumPost
template_name = 'forum/forumpost-details.html'
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