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 django.urls import path
from .views import index from .views import post_list_view, post_detail_view
urlpatterns = [ 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' 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.http import HttpResponse
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from .models import Post, Reply from .models import Post, Reply
def index(request):
heading = 'FORUM POSTS:<br>' def post_list_view(request):
posts = Post.objects.all() context = {}
body = '' context['post_list'] = Post.objects.order_by('-pub_date').all()
for post in posts: return render(request, 'forum/forum_list.html', context)
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, def post_detail_view(request, pk):
post.pub_date, post.post_body post = Post.objects.get(pk=pk)
) reply_list = Reply.objects.filter(original_post=post).order_by('-pub_date').all()
for post_reply in post_replies: context = {'post': post,
body += 'Reply by {} {} dated {}:<br>{}<br>'.format( 'reply_list': reply_list
post_reply.author.first_name, post_reply.author.last_name, }
post_reply.pub_date, post_reply.reply_body return render(request, 'forum/forum_details.html', context)
)
body += '<br>'
return HttpResponse(heading + body)
{% 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