update: modified the urls in buttons to open the correct URL for the new forum post

parent 8d4a32ff
...@@ -9,10 +9,10 @@ ...@@ -9,10 +9,10 @@
{% for post in posts_list %} {% for post in posts_list %}
<li><a href="{% url 'forum:detail' post.id %}">{{ post.post_title }}</a> by {{ post.author.first_name }} {{ post.author.last_name}} dated {{ post.pub_date|date:"SHORT_DATE_FORMAT" }}</li> <li><a href="{% url 'forum:detail' post.id %}">{{ post.post_title }}</a> by {{ post.author.first_name }} {{ post.author.last_name}} dated {{ post.pub_date|date:"SHORT_DATE_FORMAT" }}</li>
{% endfor %} {% endfor %}
<button onclick="location.href = '/posts/add'">New Forum Post</button> <button onclick="location.href = '/forum/posts/add'">New Forum Post</button>
</ul> </ul>
{% else %} {% else %}
<p>No posts are available.</p> <p>No posts are available.</p>
<button onclick="location.href = '/posts/add'">New Forum Post</button> <button onclick="location.href = '/forum/posts/add'">New Forum Post</button>
{% endif %} {% endif %}
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
<h1>NEW FORUM POST</h1> <h1>NEW FORUM POST</h1>
</div> </div>
<div style = "position: relative; left:80px; top:30px;"> <div style = "position: relative; left:80px; top:30px;">
<form method="POST" action ="{% url 'forum:add' %}" enctype="multipart/form-data"> <form method="POST" action ="{% url 'forum:post_add' %}" enctype="multipart/form-data">
{% csrf_token %} {% csrf_token %}
{{ post_form.as_p }} {{ post_form.as_p }}
<button class="button" type="Save Post">Save Post</button> <button class="button" type="Save Post">Save Post</button>
......
from django.urls import path from django.urls import path
from . import views from . import views
app_name = "forum" app_name = 'forum'
urlpatterns = [ urlpatterns = [
path('', views.index, name="index"), path('', views.ForumView.as_view(), name="index"),
path('posts/<int:post_id>/details/', views.detail, name="detail"), path('posts/<int:post_id>/details/', views.detail, name="detail"),
path('posts/add/', views.add, name="post_add"), path('posts/add/', views.add, name="post_add"),
# path('welcome', views.welcome, name='welcome') path('welcome', views.welcome, name='welcome')
] ]
\ No newline at end of file
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
from django.template import loader from django.views import View
from .models import Post, Reply from .models import Post, Reply
from .forms import PostForm from .forms import PostForm
# Create your views here. # Create your views here.
def index(request): def welcome(request):
posts_list = Post.objects.order_by("pub_date") return HttpResponse('Welcome to Widget Forum!')
template = loader.get_template("forum/index.html")
context = { class ForumView(View):
"posts_list": posts_list, def get(self, request):
} posts_list = Post.objects.order_by("pub_date")
return HttpResponse(template.render(context, request)) return render(request, "forum/index.html", {"posts_list": posts_list})
def detail(request, post_id): def detail(request, post_id):
try: try:
......
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