Django, Error 404, Not Found: /products/5 GIVE me Your hand
I have quation regarding Django, I tried to solve it myself during 2 days, but I haven't any ideas about this problem, can YOU help me? I read a book about Django, and there is example: urls.py
from django.urls import re_path
from django.urls import path
from firstapp import views
urlpatterns = [
re_path(r'^products/?P<productid>\d+/', views.contact),
re_path(r'^users/(?P<id>\d+)/?P<name>\D+/', views.about),
re_path(r'^about/contact/', views.contact),
re_path(r'^about', views.about),
path('', views. index),
]
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("<h2>Main</h2>")
def about(request):
return HttpResponse("<h2>About site</h2>")
def contact(request):
return HttpResponse("<h2>Contacts</h2>")
def products(request, productid):
output = "<h2>Product № {0}</h2>".format(productid)
return HttpResponse(output)
def users(request, id, name):
output = "<h2>User</h2><h3>id: {О} " \
"Name:{1}</hЗ>".format(id, name)
return HttpResponse(output)
But after using this link(http://127.0.0.l:8000/products/5), I get this text: Using the URLconf defined in hello.urls, Django tried these URL patterns, in this order:
^products/?P\d+/ ^users/(?P\d+)/?P\D+/ ^about/contact/ ^about The current path, products/5, didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
And this thing in terminal: Not Found: /products/5 [08/Feb/2023 12:17:13] "GET /products/5 HTTP/1.1" 404 2597
I need Your help!
I tried to delet code about:
re_path(r'^products/?P<productid>\d+/', views.contact),
re_path(r'^users/(?P<id>\d+)/?P<name>\D+/', views.about),
and I haven't this 'ERROR 404', but I can't finish my project without this text
Haven't tested this solution myself yet - but it looks like you're missing some parentheses. I believe the first path you've configured should be something like:
re_path(r'^products/(?P<productid>\d+)/', views.contact),
This should work, but will match products/5
as well as products/5/anything/else/here
which is probably not the behaviour you're after. In that case the path should be something like:
re_path(r'^products/(?P<productid>\d+)/$', views.contact),
The django docs do a pretty good job at explaining how all this works in any level of detail you need, see https://docs.djangoproject.com/en/4.1/ref/urls/#re-path
EDIT:
In the above, views.contact
should be views.products
. Presumably this should be pointing to your def products
view.