Commit 42e475ad authored by Stephanie Tullao's avatar Stephanie Tullao

Updated Forum's urls.py and added templates for forum list and details in views.py

parent 6945a987
from django.urls import path
from .views import index
from .views import post_list_view, post_detail_view
urlpatterns = [
path('', index, name='index'),
path('', post_list_view, name='post-list'),
path('<int:pk>/details', post_detail_view, name='post-details'),
]
app_name = 'forum'
\ No newline at end of file
from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from .models import Post, Reply
def index(request):
heading = 'FORUM POSTS:<br>'
posts = Post.objects.all()
body = ''
for post in posts:
post_replies = Reply.objects.filter(original_post=post)
body += '{} by {} {} dated {}:<br>{}<br>'.format(
post.post_title, post.author.first_name, post.author.last_name,
post.pub_date, post.post_body
)
for post_reply in post_replies:
body += 'Reply by {} {} dated {}:<br>{}<br>'.format(
post_reply.author.first_name, post_reply.author.last_name,
post_reply.pub_date, post_reply.reply_body
)
body += '<br>'
return HttpResponse(heading + body)
def post_list_view(request):
context = {}
context['post_list'] = Post.objects.order_by('-pub_date').all()
return render(request, 'forum/forum_list.html', context)
def post_detail_view(request, pk):
post = Post.objects.get(pk=pk)
reply_list = Reply.objects.filter(original_post=post).order_by('-pub_date').all()
context = {'post': post,
'reply_list': reply_list
}
return render(request, 'forum/forum_details.html', context)
{% extends 'base.html' %}
{% load static %}
{% block title %}{{ post.post_title }}{% endblock %}
{% block styles %}
<link rel="stylesheet" href="{% static 'forum/css/detail_styles.css' %}">
{% endblock %}
{% block content %}
{% if post.id == 1 %}
<img src="{% static 'forum/img/GitBranch.png' %}" alt="view branch">
{% endif %}
{% if post.id == 2 %}
<img src="{% static 'forum/img/GitCheckout.png' %}" alt="switch branch">
{% endif %}
{% if post.id == 3 %}
<img src="{% static 'forum/img/GitBranchCreate.png' %}" alt="create branch">
{% endif %}
<h1>{{ post.post_title }}</h1>
<h2>
by {{ post.author.first_name }} {{ post.author.last_name }}, {{ post.pub_date|date:'d/m/Y' }}
</h2>
{{ post.post_body }}
<h3>Replies:</h3>
{% for reply in reply_list %}
<h2>
by {{ reply.author.first_name }} {{ reply.author.last_name }}, {{ reply.pub_date|date:'d/m/Y' }}
</h2>
{{ reply.reply_body }}
{% endfor %}
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Forum{% endblock %}
{% block styles %}
<link rel="stylesheet" href="{% static 'forum/css/list_styles.css' %}">
{% endblock %}
{% block content %}
<header>
<h1>
Welcome to Widget’s Forum!
</h1>
</header>
<p>
<h3>Forum posts:</h3>
{% for post in post_list %}
<li>
<a href="{% url 'forum:post-details' pk=post.pk %}">
{{ post.post_title }} by {{ post.author.first_name }} {{ post.author.last_name }} dated {{ post.pub_date|date:"d/m/Y" }}
</a>
</li>
{% endfor %}
</p>
{% endblock %}
\ 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