Trying to make it work Regex/url/views on Django
Trying to make it work Regex/url/views on Django
I'm new with Python and Django and I'm trying to make a url regex using PyCharm, but I have no clue why it doesn't work.
I have this example...
from django.contrib import admin
from django.urls import path
from .views import (home, client_detail,)
urlpatterns = [
path(r'^$', home),
path(r'^/cliente/(?P<id>d+)/$', client_detail),
path(r'admin/', admin.site.urls),
]
And I have a views.py that has the below code inside it:
from django.http import HttpResponse
def home(request):
return HttpResponse('HOME')
def client_detail(request, id):
return HttpResponse(id)
The question is: When I write path(r'^/cliente/(?P<id>d+)/$', client_detail)
instead path(r'/cliente/<id>', client_detail)
, I receive the print error below
path(r'^/cliente/(?P<id>d+)/$', client_detail)
path(r'/cliente/<id>', client_detail)
Can someone please tell me what I'm missing? Thanks in advance! :)
Error
1 Answer
1
You are using path
but your pattern is a regex. Use then re_path
:
path
re_path
https://docs.djangoproject.com/en/2.1/ref/urls/#re-path
re_path(r'^/cliente/(?P<id>d+)/$', client_detail),
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.