curtisp
curtisp

Reputation: 2315

Django url pattern doesn't match

I am using new Django 1.8 app to learn Django.

I am stumped as to how to get this my simple url to be resolved by urls.py

I create the url in another view as:

<a href="/photoview/{{photo.id}}/"}>

I can successfully pass this url to the browser as:

http://localhost:8000/photoview/300/

I am expecting that this url can be matched by the urls.py expression:

url('r^photoview/(?P<id>\d+)/$', views.photoview),

But this is not working. I have tried variations of this but none have worked so far, such as:

url('r^photoview/(?P<id>[0-9]+)/$', views.photoview),

I get this message in browser when it fails to match

Page not found (404)
Request Method:     GET
Request URL:    http://localhost:8000/photoview/300/

Using the URLconf defined in asset.urls, Django tried these URL patterns, in this order:

^admin/
^$ [name='index']
^time/$
^about/$
^accounts/$
^photos/$
^tags/$
^users/$
r^photoview/(?P<id>\d+)/$
^static\/photos\/(?P<path>.*)$

The current URL, photoview/300/, didn't match any of these.

Appreciate any help getting this to work.

Upvotes: 1

Views: 1841

Answers (1)

Foon
Foon

Reputation: 6433

you have url('r^photoview/(?P<id>\d+)/$', views.photoview), you want url(r'^photoview/(?P<id>\d+)/$', views.photoview),

(Note the r is in front of the string, not the first character)

As noted in docs.djangoproject.com/en/1.8/topics/http/urls,

The 'r' in front of each regular expression string is optional but recommended. It tells Python that a string is “raw” – that nothing in the string should be escaped

Also note that you should use a friendly name in your url definition (e.g. photoview) and then use {% url 'photoview' photo.id %} in your template instead of hardcoding the URL pattern.

Upvotes: 4

Related Questions