darkryder
darkryder

Reputation: 832

Django understanding urls

In my core urls.py, I have

url(r'^student/', include('studentportal.urls')),

In studentportal.urls, I have this url

    url(r'^project/(?P<project_id>[0-9])/edit/$', views.editproject, name='editproject'),   
    url(r'^project/(?P<project_id>[0-9])/upload/$', views._upload, name='upload_document'),
    url(r'^project/(?P<project_id>[0-9])/$', views.viewproject, name='viewproject'),
    url(r'^download/(?P<document_id>[0-9])/', views.download, name='download_document'),

NoReverseMatch errors are popping up while rendering the template at this line

<p><a class="btn btn-default" href="{% url 'viewproject' p.id %}" role="button">View details »</a></p> 

It checks these urls

2 pattern(s) tried: ['student/project/(?P<project_id>[0-9])/$', '$project/(?P<project_id>[0-9])/$']

I am pretty sure that the error is in my way of using the urls and not in views, nor templates. Also, the exclusion of '$' at the end of the urlpatterns results in NoReverseMatch errors. Even though I've read the django documentation about the urlpatterns.
'^' means the start of the line
'$' means that the url should end here
'(?P< named_variable >)' is used to catch a variable from the url.

So why is the pattern not matching when clearly the first pattern should match 'viewproject' with arguments '('10',)' ?

Upvotes: 0

Views: 151

Answers (1)

GAEfan
GAEfan

Reputation: 11370

To match non-single-digit numbers, change to:

url(r'^project/(?P<project_id>[0-1000])/edit/$

or

url(r'^project/(?P<project_id>[0-9]+)/edit/$

Upvotes: 3

Related Questions