Add new announcement view and change index view to obv

parent 0a9fa98c
from urllib import response from urllib import response
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
from django.shortcuts import render from django.shortcuts import render, redirect
from django.views import View
from forum.models import Post from forum.models import Post
from forum.models import Reply from forum.models import Reply
from .forms import PostForm
# Create your views here. # Create your views here.
def index(request): class IndexView(View):
def get(self, request):
post_list = Post.objects.order_by("pub_date") post_list = Post.objects.order_by("pub_date")
context = { context = {
"post_list": post_list "post_list": post_list
} }
return render(request, "forum/index.html", context) return render(request, "forum/index.html", context)
def details (request, post_id): def details (request, post_id):
reply_list = Reply.objects.order_by("pub_date") reply_list = Reply.objects.order_by("pub_date")
#error handling #error handling
try: try:
posts = Post.objects.get(pk=post_id) posts = Post.objects.get(pk=post_id)
except Post.DoesNotExist: except Post.DoesNotExist:
raise Http404("Post does not exist.") raise Http404("Post does not exist.")
return render(request, "forum/details.html", {"posts":posts, 'reply_list':reply_list}) return render(request, "forum/details.html", {"posts":posts, 'reply_list':reply_list})
"""post_objects = Post.objects.all() def add(request):
reply_objects = Reply.objects.all()
response = "FORUM POSTS:" if request.method == "POST":
post_form = PostForm(request.POST, request.FILES)
for post in post_objects: if post_form.is_valid():
response = (response + f"<br> {post.post_title} by " post_form.save()
+ f"{post.author.first_name} {post.author.last_name} " return redirect("forum:add")
+ f"dated {post.pub_date.date()}:<br> {post.post_body} <br>") else:
for reply in reply_objects: post_form = PostForm()
if(reply.post.post_title==post.post_title): return render(request, "forum/add.html", {"post_form": post_form})
response = (response + "Reply by "
+ f"{reply.author.first_name} {reply.author.last_name} " """post_objects = Post.objects.all()
+ f"dated {reply.pub_date.date()}:<br> {reply.reply_body} <br>") reply_objects = Reply.objects.all()
response = "FORUM POSTS:"
return HttpResponse(response)"""
for post in post_objects:
\ No newline at end of file response = (response + f"<br> {post.post_title} by "
+ f"{post.author.first_name} {post.author.last_name} "
+ f"dated {post.pub_date.date()}:<br> {post.post_body} <br>")
for reply in reply_objects:
if(reply.post.post_title==post.post_title):
response = (response + "Reply by "
+ f"{reply.author.first_name} {reply.author.last_name} "
+ f"dated {reply.pub_date.date()}:<br> {reply.reply_body} <br>")
return HttpResponse(response)"""
\ 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