feat: added a detail template and changed index template code from hardcoding to referencing

parent a3d2c4c6
<h1>{{ post.post_title }}</h1>
<p>{{ post.post_body }}</p>
\ No newline at end of file
{% if posts_list %}
<ul>
{% for post in posts_list %}
<li><a href="/forum/{{ author.id }}">{{ post.post_title }}</a></li>
<li><a href="{% url 'forum:detail' post.id %}">{{ post.post_title }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No articles are available.</p>
<p>No posts are available.</p>
{% endif %}
\ No newline at end of file
from django.urls import path
from . import views
app_name = "forum"
urlpatterns = [
path('', views.index, name="index"),
path('<int:post_id>/', views.detail, name="detail"),
path('<int:post_id>/replies/', views.replies, name="replies")
# path('welcome', views.welcome, name='welcome')
]
\ No newline at end of file
from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponse, Http404
from django.template import loader
from .models import Post, Reply
......@@ -16,6 +16,18 @@ def index(request):
# return HttpResponse(view)
# return HttpResponse("Welcome to Widget's forum!")
def detail(request, post_id):
try:
post = Post.objects.get(pk=post_id)
except Post.DoesNotExist:
raise Http404("Post does not exist!")
return render(request, "forum/detail.html", {"post": post})
# return HttpResponse("These are the details for post # %s" % author_id)
def replies(request, post_id):
reply_out = "These are the replies to post # %s."
return HttpResponse(reply_out % post_id)
# def welcome(request):
# forum_view = "FORUM POSTS: "
# # posts = Post.objects.all()
......
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