page not found when making new url / view Django

Viewed 84

Hello i am trying to add a basic url called localhost:8000/shop

so that when i am on my homepage, I can click a link called shop and It will lead me to localhost:8000/shop

in my urls.py i added

from django.conf import settings
from django.conf.urls.static import static

from django.contrib import admin
from django.urls import include, path
from homepage import views




urlpatterns = [
    path('' , views.home),
    path('admin/', admin.site.urls),
    path('reviews/' , include('reviews.urls')),
    path('shop/' , include('product.urls')),

] 

in my folder called product i have a urls.py file with

from django.urls import include, path
from . import views

urlpatterns = [
    path('shop/' , views.shop),   
        
]

and in my product folder i have a views.py file with

from django.shortcuts import render


# Create your views here.

def shop(request):
    return render(request, 'product/shop.html')

linking It to my html file inside my product folder.. when i run the server, I get this error message

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/shop
Using the URLconf defined in yorleico.urls, Django tried these URL patterns, in this order:

admin/
reviews/
shop/
The current path, shop, didn't match any of these.

What am i doing wrong?!

2 Answers

All url patterns in the product app already start with shop/, due to the path('shop/', include('product.urls')). Therefore your urls.py for the products app should look like:

# product/urls.py

from django.urls import include, path
from . import views

urlpatterns = [
    path('' , views.shop),   
]

otherwise the path should be /shop/shop/.

To register the path /shop, you need to use path('' , views.shop), in the urls.py in your shop app. The /shop prefix is already being defined by the path('shop/' , include('product.urls')), line in your project level urls.py.

Related