Commit ce0f0758 authored by Rurik Serzo's avatar Rurik Serzo

Added PostDetailView and corresponding images per post

parent 26331164
...@@ -24,7 +24,7 @@ class Reply(models.Model): ...@@ -24,7 +24,7 @@ class Reply(models.Model):
reply_body = models.TextField() reply_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 = models.ForeignKey(Post,on_delete=models.CASCADE,null=True,related_name="posts") post = models.ForeignKey(Post,on_delete=models.CASCADE,null=True,related_name="replies")
def getReply(self): def getReply(self):
return "Reply by {} {} {}:<br>{}<br>".format( return "Reply by {} {} {}:<br>{}<br>".format(
......
{% extends 'base.html' %}
{% load static %}
{% block title %}Forum{% endblock %}
{% block styles %}
<link rel="stylesheet" href="{% static 'forum.css' %}">
{% endblock %}
{% block content %}
<h1 class="forum-heading">{{ post.post_title }}</h1>
<h2 class="forum-subtitle">by {{ post.author.first_name }} {{ post.author.last_name }} dated {{ post.pub_date|date:'d/m/Y' }}</h2>
<p>
{{ post.post_body }}
</p>
{% load static %}
{% if post.id == 1 %}
<img src="{% static 'post1.jpg' %}" class="post-image" alt="Leni Robredo">
{% elif post.id == 2 %}
<img src="{% static 'post2.jpg' %}" class="post-image" alt="Lady Gaga MET camp">
{% else %}
<img src="{% static 'post3.jpg' %}" class="post-image" alt="Bruno Mars Grammys 2022">
{% endif %}
<div class="forum-reactions">
{% for reply in post.replies.all %}
<p>{{ reply.author.first_name }} {{ reply.author.last_name }}, {{ reply.pub_date|date:'d/m/Y' }}: {{ reply.reply_body }}</p>
{% endfor %}
</div>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import index from .views import index, PostDetailView
urlpatterns = [ urlpatterns = [
path("", index, name="index") path("", index, name="index"),
path("<int:pk>/details", PostDetailView.as_view(), name="post-detail" )
] ]
app_name = "Forum" app_name = "Forum"
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from django.template import loader from django.template import loader
from django.views.generic.detail import DetailView
from .models import Post, Reply from .models import Post, Reply
def index(request): def index(request):
context = { context = {
"posts": Post.objects.order_by("-pub_date"), "posts": Post.objects.order_by("-pub_date"),
} }
template = loader.get_template("forum/index.html") template = loader.get_template("forum/index.html")
return HttpResponse(template.render(context, request)) return HttpResponse(template.render(context, request))
\ No newline at end of file
class PostDetailView(DetailView):
model = Post
\ 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