URL not found

Hi, I am having an error 404 when clicking a link when the url is in an app url instead of django url, but the link is working if the url is in the django url. I have been googling for days and read the documentation, nothing is working.

Here are the codes in the django urls:-

from django.contrib import admin

from django.urls import path, include

from pages.views import homepage_view, about_view, experts_view

urlpatterns = [

    path('', homepage_view, name='home'),

    path('home/', homepage_view, name='home'),

    path('about_us/', about_view, name='about_us'),

    path('experts/', experts_view, name='experts'),

    path('blog/', include('blog.urls')) ,

    path('admin/', admin.site.urls),

]

Here are the codes in the app urls:-


from blog import views

from django.urls import path

name_app = 'blog'

urlpatterns = [

    path('', views.PostList.as_view(), name='blog'),

    path('<slug:slug>/', views.PostDetail.as_view(), name='post_detail'),

    path('create/', views.PostCreateView.as_view(), name='create'),

]

The code when I click the link:-


{% block sidebar %}

<style>

        .card{

            box-shadow: 0 16px 48px #E3E7EB;

        }

       

</style>

<!-- Sidebar Column -->

<div class="col-md-4 float-right ">

<div class="card my-4">

        <h5 class="card-header">About Us</h5>

    <div class="card-body">

        <p class="card-text"> Test</p>

        <a href="{% url 'create' %}"

           class="btn btn-danger">Know more!</a>

    </div>

</div>

</div>

{% endblock sidebar %}

I think the problem is in “{% url ‘create’ %}”

Note that all other links in the app urls is working except for “create”.

Thank you in advance.

That’s the template being rendered - can you show how that link shows up in the browser after being rendered?

This is the link that shows up after I click “{% url ‘create’ %}”

Ahh… I see the issue here.

The urlpatterns are checked in the order that they’re defined. You’re issuing a request for the url /blog/create/. However, as urlpatterns is being checked, ‘create/’ is going to pass the test for <slug:slug>/ and dispatch to PostDetail. It’s never going to even check against 'create/'.

You’ve got at least two options here:

  1. Change your path for PostDetail to (perhaps) be something like 'view/<slug:slug>/' (My recommendation)
  2. Move the 'create/' path ahead of the '<slug:slug>/' path.

Ken

2 Likes

OMG! Thank you so much! You’re a lifesaver! Everything is working now.


Hello there i hope you’re doing fine, i faced the same problem and i thought maybe you could help me, i’m new with django.