1

I am using Django 1.11. I am trying to get url pattern to match uuid but it does not work. I have tried more than 3 times but I am still getting error :

Using the URLconf defined in eMarket.urls, Django tried these URL patterns, in this order:
http://127.0.0.1:8000/view/49c26740-2211-4cc9-971b-5ff62ddc2e0e/

urlpatterns = [
    url(r'^view/(?P<slug>\b[0-9A-Fa-f]{8}\b(-\b[0-9A-Fa-f]{4}\b){3}-\b[0-9A-Fa-f]{12}\b)$', viewProduct, name="view"),
    url('admin/', admin.site.urls),
]
7
  • What are all those \b elements doing in there? Commented Jun 23, 2018 at 16:28
  • i don't exactly i just copy it here, because everything is working with int id but when i change to uuid it is require the url pattern which can meet so i never done before that why i am asking for help Commented Jun 23, 2018 at 16:48
  • when a user clicked view the url should be like: Commented Jun 23, 2018 at 16:49
  • 127.0.0.1:8000/view/49c26740-2211-4cc9-971b-5ff62ddc2e0e Commented Jun 23, 2018 at 16:49
  • try adding a trailing slash to the end of the pattern: ...)/?$. Also, word boundaries (\b) are safe to remove. Commented Jun 23, 2018 at 17:00

3 Answers 3

2

Could you use the django native UUID path converter instead of trying to manually create the regex?

Sign up to request clarification or add additional context in comments.

Comments

0
import re

url = "http://127.0.0.1:8000/view/49c26740-2211-4cc9-971b-5ff62ddc2e0e/"

m = re.search("(?P<slug>[0-9A-Fa-f]{8}(-[0-9A-Fa-f]{4}){3}-[0-9A-Fa-f]{12})", url)
print(m.group(0))

gives:
49c26740-2211-4cc9-971b-5ff62ddc2e0e

5 Comments

path(r'^view/(?P<slug>[0-9A-Fa-f]{8}(-[0-9A-Fa-f]{4}){3}-[0-9A-Fa-f]{12})', viewProduct, name="view"),
can you define not working ? what is the error you get ?
this the error: Using the URLconf defined in eMarket.urls, Django tried these URL patterns, in this order:
urlpatterns = [ url(r'^$', homePage), url(r'^about/', aboutPage), url(r'^contact/', contactPage), url(r'^create_products/', addProducts), path(r'^view/(?P<slug>[0-9A-Fa-f]{8}(-[0-9A-Fa-f]{4}){3}-[0-9A-Fa-f]{12})', viewProduct, name="view"), url(r'^search/', searchProduct, name="search"), #name="search" url(r'^login/', loginPage), url(r'^register/', signupPage), url('admin/', admin.site.urls), ]
pls. go through the Django docs and try to debug by excluding all other possibilities for instance.
0

Only thing so far i can imagine that could cause issues with the URL resolver: you are using a capturing group within your named slug group. This way Django will receive the full match plus 2 groups, which could avoid a proper URL resolution. You can check this quickly on regex101

enter image description here

While using a non-capturing group (?:) for the repeating pattern, it will return only the named capturing group - again on regex101

enter image description here

So, can you try (?P<slug>[0-9A-Fa-f]{8}(?:-[0-9A-Fa-f]{4}){3}-[0-9A-Fa-f]{12}) - because other than that, i see really no issues in your urlpattern.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.