0

I am setting up a new URL path but I keep getting a page not found. This is my URL that isn't working:

  url(r'^collections/(?P<collection_name>\d+)$', collections.home, name='collections'),

This is the function in my view:

def home(request, collection_name, slug=None):
  collection_data = Collections.objects.get(collection_name=collection_name)
  try: 
    logged_id = request.GET.get('admin_id')
  except:
    logged_id = ""
  return render(request, 'collections.html', {'collection_data':collection_data,'logged_id':logged_id})

This is the error I am getting:

enter image description here

If I turn it into a simple URL and remove the parameter from the URL and view function as follows, it works fine so I know I'm pointing to the right view:

url(r'^collections$', collections.home, name='collections'),

In the same file I have another URL as follows, and it also works fine:

url(r'^store/(?P<seller_id>\d+)$', store.home, name='store'),

This leads me to believe that I have a simple typo or something really basic that I am overlooking. Can anyone help me spot the error here? Thank you!

2
  • 2
    What url do you write when you get the 404 error? Is collection_name a string? Commented Mar 24, 2021 at 17:55
  • Hi @AbdulAzizBarkat I've edited it with a screen shot of the error. Yes, collection_name is a string. Commented Mar 24, 2021 at 17:59

2 Answers 2

1

The url function uses regex to capture arguments. when you write \d+ it expects one or more digits (numbers) in that place. collection_name is a string so it won't match \d+. You should write a pattern that can match a string instead, e.g. [^/]+

url(r'^collections/(?P<collection_name>[^/]+)$', collections.home, name='collections'),

Better yet shift to using the path function unless you really need to match some url with complex regular expressions:

from django.urls import path

path('collections/<str:collection_name>', collections.home, name='collections'),

Note: Ideally one should end their url patterns with a trailing slash /. By default Django adds a trailing slash to any incoming url without one and redirects the user to this new url. Causing people to face issues with urls not working for them.

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

1 Comment

Worked great. Thank you for the suggestion on paths, much easier. I was following precedent of the legacy code base (written back in 2013). Thanks for going extra bit to help solve and provide a suggestion.
1

It looks like you are using an outdated version of django, or the old format. for example, in the newer versions of django the urls don't use '^r'. I would recommend reading a newer book/tutorial, and/or updating the version of django you use. Even if this isn't the source of this particular issue, it will cause you many, many problems in th future. I had the same problem when I started, and it was really confusing, because I was using a book from like 7 years ago, and the code wasn't accurate to wat is used now. To upgrade django, run: python -m pip install --upgrade pip pip install --upgrade Django Also, we are on python3, so make sure you update to that.

2 Comments

Thanks for the heads up. Yes, all upgraded to python3 and Django 2.2, but my app was original built in 2013, so while it all works great certainly need to upgrade the syntax.
Wow! Yeah, the new syntax is a lot cleaner. The book I was using was from 2013 also, so I know what you mean!

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.