[Answered ]-Django url pattern with 2 parameters misinterprets the second parameter

1👍

After a lot of digging I found that when pictures in my test-database are missing, the template tag will give a None value in the image tag src attribute. This caused a request to the server to look for the image on URL /topic/1/None. Somehow, this happened even before the response was send to the client, so that my view received new, wrong, parameters.

This tag in my template on page …/topic/1/ requests the image from …/topic/1/None, sending wrong parameters.

<img src="{{recommendation.user_picture_url}}" />

PS Thanks falsetru for the improvements in the regex

👤JacobF

1👍

Use non-greey qualifier (.*? or .+?):

urlpatterns = patterns('',
    url(r'^topic/(?P<topic>.+?)/$', activities_list, name='activities'),
    url(r'^topic/(?P<topic>.+?)/(?P<page>.+?)/$', activities_list, name='activities_page'),
)

For page number, \d+ is more appropriate.

urlpatterns = patterns('',
    url(r'^topic/(?P<topic>.*?)/$', activities_list, name='activities'),
    url(r'^topic/(?P<topic>.*?)/(?P<page>\d+)/$', activities_list, name='activities_page'),
)

Instead of (?P<topic>.*?), you can also use (?P<topic>[^/]+)

Leave a comment