I can't quite figure out why I can' access a URL with Django

Issue

I’m trying to access the URL ending basket/ but when I go to the page I receive the 404 error shown below.

enter image description here

I understand this error isn’t thrown due to Django not being able to find the template, it has something to do with my product_detail view.

Here are my urls linking me to basket/

core/urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

app_name = 'core'

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('main_store.urls', namespace='main_store')),
    path('basket/', include('basket.urls', namespace='basket')),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

basket/urls.py

from django.urls import path

from . import views

app_name = 'basket'

urlpatterns = [
    path('', views.basket_summary, name='basket_summary')
]

Here is the basket/views.py:

from django.shortcuts import render


def basket_summary(request):
    return render(request, 'main_store/basket/summary.html')

And here is the view that is throwing the error. main_store/views.py:

def product_detail(request, slug):
    product = get_object_or_404(Product, slug=slug, in_stock=True)
    return render(request, 'main_store/products/single_product.html', {'product': product})

If anyone can shed some light on what the issue is or what I’m doing wrong, it would be much appreciated. Thanks in advance.

Solution

Your main_store.urls has lookup by slug and any word will be passed to that, you should move up basket url like this

urlpatterns = [
    path('admin/', admin.site.urls),
    path('basket/', include('basket.urls', namespace='basket')),
    path('', include('main_store.urls', namespace='main_store')),
]

Answered By – Malik Kekenov

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published