Home > OS >  Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/blog/blog/Blog comments/
Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/blog/blog/Blog comments/

Time:02-17

blog app urls

urlpatterns = [

path('', views.blog, name="blog"),
path('postComment/', views.postComment, name="postComment"),
path('<str:slug>/', views.blogPage, name="blogPage"),

] static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

project urls/

urlpatterns = [

path('admin/', admin.site.urls),
path('', include('home.urls')),
path('blog/', include('blog.urls')),

]

home app urls/

urlpatterns = [

path('', views.index, name="index"),
path('contact/', views.contact, name="contact"),
path('about/', views.about, name="about"),

] static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

blog.html/

               <a href="blog/{{post.slug}}">{{post.title}}</a>

after this url became http://127.0.0.1:8000/blog/blog/Blog comments/

blog.html(changed)/

                <a href="/{{post.slug}}">{{post.title}}</a>

and after this url became http://127.0.0.1:8000/Blog comments/

in both case page not found

but from home page url became this and work http://127.0.0.1:8000/blog/Blog comments/

index.html/

               <a href="blog/{{post.slug}}">{{post.title}}</a>

blog/views.py/

def blog(request):

allPosts= Post.objects.all()
context={'allPosts': allPosts}
return render(request, "blog/blog.html", context)

def blogPage(request, slug):

post=Post.objects.filter(slug=slug).first()
comments= BlogComment.objects.filter(post=post, parent=None)
context={"post":post, 'comments': comments,}
return render(request, "blog/blogPage.html", context)

CodePudding user response:

You are using wrong url configuration. just use this in your html for render your url:

<a href="{% url 'blogPage' post.slug %}">{{post.title}}</a>

don't use any / or blog/ inside href.

  • Related