Home > Software design >  I can't quite figure out why I can' access a URL with Django
I can't quite figure out why I can' access a URL with Django

Time:09-30

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 I'm the issue is or what I'm doing wrong, it would be much appreciated. Thanks in advance.

CodePudding user response:

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')),
]
  • Related