I am getting this error page only when I set the url to “posts/read-later”.
This is my urls.py: -
from django.urls import path
from . import views
urlpatterns = [
path('', views.IndexView.as_view(), name="index"),
path('posts/', views.AllPostsView.as_view(), name="posts"),
path('posts/<slug:slug>', views.PostDetailView.as_view(), name="post-detail"),
path('posts/read-later', views.ReadLaterView.as_view(), name="read-later"),
]
The screenshot of the error page is:-
If I change the url to “read-later”, then the error doesn’t happen.
from django.urls import path
from . import views
urlpatterns = [
path('', views.IndexView.as_view(), name="index"),
path('posts/', views.AllPostsView.as_view(), name="posts"),
path('posts/<slug:slug>', views.PostDetailView.as_view(), name="post-detail"),
path('read-later', views.ReadLaterView.as_view(), name="read-later"),
]
views.py file:-
from typing import Any
from django.db.models.query import QuerySet
from django.shortcuts import render, get_object_or_404
from .models import Post
from django.views import View
from django.views.generic import ListView, DetailView
from .forms import CommentForm
from django.http import HttpResponseRedirect
from django.urls import reverse
# Create your views here.
# def index(request):
# latest_posts = Post.objects.all().order_by("-date")[:3]
# return render(request, "blog/index.html", {
# "posts": latest_posts
# })
class IndexView(ListView):
template_name = "blog/index.html"
model = Post
context_object_name = "posts"
ordering = ["-date"]
def get_queryset(self):
queryset = super().get_queryset()
data = queryset[:3]
return data
# def posts(request):
# all_posts = Post.objects.all().order_by("-date")
# return render(request, "blog/all-posts.html", {
# "all_posts": all_posts
# })
class AllPostsView(ListView):
model = Post
template_name = "blog/all-posts.html"
ordering = ["-date"]
context_object_name = "all_posts"
# def post_detail(request, slug):
# identified_post = get_object_or_404(Post, slug=slug)
# return render(request, "blog/post-detail.html", {
# "identified_post": identified_post,
# "post_tags": identified_post.tags.all()
# })
class PostDetailView(View):
def get(self, request, slug):
post = Post.objects.get(slug=slug)
context = {
"post": post,
"comment_form": CommentForm(),
"comments": post.comments.all().order_by("-id")
}
return render(request, "blog/post-detail.html", context)
def post(self, request, slug):
comment_form = CommentForm(request.POST)
post = Post.objects.get(slug=slug)
if comment_form.is_valid():
comment = comment_form.save(commit=False)
comment.related_post = post
comment.save()
return HttpResponseRedirect(reverse("post-detail", args=[slug]))
context = {
"post": post,
"comment_form": comment_form,
"comments": post.comments.all().order_by("-id")
}
return render(request, "blog/post-detail.html", context)
class ReadLaterView(View):
def post(self, request):
stored_posts = request.session.get("stored_posts")
if stored_posts is None:
stored_posts = []
if (post_id := request.POST["post_id"]) not in stored_posts:
stored_posts.append(int(post_id))
return HttpResponseRedirect("/")
Please help me and if any other file or code is required, please ask.