Commit 175beaf3 authored by nekopilar's avatar nekopilar

created templates for forum and converted views from fbv to cbv

parent 03ddadad
...@@ -7,6 +7,7 @@ class Post(models.Model): ...@@ -7,6 +7,7 @@ class Post(models.Model):
post_body = models.TextField() post_body = models.TextField()
pub_date = models.DateField(auto_now_add=True) pub_date = models.DateField(auto_now_add=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True) author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True)
post_pic = models.FileField(upload_to="uploads", null=True, blank=True)
class Reply(models.Model): class Reply(models.Model):
reply_body = models.TextField() reply_body = models.TextField()
......
<!-- forum/index.html -->
{% extends 'base.html' %}
{% load static %}
{% block title %}Announcements{% endblock %}
{% block styles %}
<link rel="stylesheet" href="{% static 'announcement_board/announcement_index.css' %}">
{% endblock %}
{% block header %}
Welcome to Widget's Forum!
{% endblock %}
{% block content %}
<p class="banner-text">Forum posts:</p>
<ul>
{% for post in all_posts %}
<li>
<a class="post-link" href="{{post.id}}/details">
{{post.post_title}} by
{{post.author.first_name}},
{{post.author.last_name}} dated
{{post.pub_date | date:"d/m/o"}}
</a> </br>
</li>
{% endfor %}
</ul>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import index from .views import ForumView, ForumDetailView
urlpatterns = [ urlpatterns = [
path('', index, name='index'), path('', ForumView.as_view(), name='index'),
path('<int:pk>/details', ForumDetailView.as_view(), name='index'),
] ]
app_name = "forum" app_name = "forum"
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.views import View
from django.views.generic.detail import DetailView
from .models import Post, Reply from .models import Post, Reply
# View for forum page
def index(request): class ForumView(View):
return HttpResponse(display_forumposts(Post.objects.all(), Reply.objects.all())) def get(self, request):
objects_set = {
"all_posts": [obj for obj in Post.objects.all()]
}
return render(request, 'forum/index.html', objects_set)
def display_forumposts(post_data, reply_data):
display_output = "FORUM POSTS: <br>"
for object in post_data:
post_title = object.post_title
post_first_name = object.author.first_name
post_last_name = object.author.last_name
post_pub_date = str(object.pub_date)
post_body = object.post_body
reply_objects = Reply.objects.filter(post_id=object.id).all() class ForumDetailView(DetailView):
model = Post
display_output += f'''
{post_title} by {post_first_name} {post_last_name} dated {post_pub_date}:<br>
{post_body}<br>
'''
for objects in reply_objects:
display_output += f'''
Reply by {objects.author.first_name} {objects.author.last_name} dated {objects.pub_date}:<br>
{objects.reply_body}<br>
'''
display_output += "<br>"
return display_output
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