Reputation: 2485
I tried to use this pattern to pass 2 parameters in an URL, where the second parameter is optional. This is my code:
urlpatterns = patterns('',
url(r'^topic/(?P<topic>.*)/(?P<page>.*)/$', activities_list,
url(r'^topic/(?P<topic>.*)/$', activities_list, name='activities'),name='activities_page'),
)
This works, except when the second parameter is 1
. In that case, topic is topic/1
and and page is None
. It works for any other number as a second parameter.
Who can put me in the right direction to solve this bug?
UPDATE, this is getting weird
After trying several things, I discovered that this template tag makes the problem:
<img src="{{recommendation.user_picture_url}}" />
When I delete this tag, or even remove it from the src attribute and place it outside the image tag, the URL works. The variable contains a URL to a LinkedIn avatar on Linkedin's servers.
This is my view
def activities_list(request, topic, page=1):
print topic
print page
...
return render_to_response(...
I don't understand why my view is getting the wrong parameters from the URL (as seen in the prints), while the hole response hasn't even been rendered yet.
Update: Solved! (see below)
Upvotes: 1
Views: 185
Reputation: 369354
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>[^/]+)
Upvotes: 1
Reputation: 2485
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
Upvotes: 1