These are chat archives for django/django

3rd
Jan 2016
Puneet Aggarwal
@puneetagg
Jan 03 2016 10:55 UTC
Hi...
Puneet Aggarwal
@puneetagg
Jan 03 2016 11:05 UTC
I am getting 404 on using this url defined in urls.py. Please help
    url(r'^/(?P<username>[0-9a-zA-Z_-]+)/posts/$', UserPostList.as_view(), name='userpost-list'),
lego
@legobillyjoe
Jan 03 2016 16:30 UTC

In Django tutorial https://docs.djangoproject.com/en/1.9/intro/tutorial03/#writing-more-views when we write more views, there is a %s simbol. What does it mean?

def detail(request, question_id):
return HttpResponse("You're looking at question %s." % question_id)
...

Obviously, this is a variable, that takes its respective question_id and puts to %s in a return. But wtf why %s exactly?

Peter Bittner
@bittner
Jan 03 2016 17:33 UTC
@puneetagg According to the Django URL dispatcher documentation there shouldn't be a slash at the beginning of an URL pattern. You should even get a warning when you do a manage.py check or manage.py runserver IIRC. Maybe that's the problem? -- Otherwise please state what's the exact URL your trying to access (that produces 404). You could also set DEBUG=True in your settings and inspect the debug output; it will tell you which patterns Django tried; maybe your urls.py are not included.
@legobillyjoe This is basic Python. It's the placeholder within strings. The value in the variable after the string and % sign is replaced by it. -- See here for documentation: https://docs.python.org/2/library/stdtypes.html#string-formatting
lego
@legobillyjoe
Jan 03 2016 17:38 UTC
Yep. Thank you. :)
Peter Bittner
@bittner
Jan 03 2016 17:42 UTC
Puneet Aggarwal
@puneetagg
Jan 03 2016 19:44 UTC
@bittner Thanks a lot for the reply. I have tried without slash in the beginning as well. Actually I am trying to create URLs similar to FB and Twitter for usernames (example.com/username).
Peter Bittner
@bittner
Jan 03 2016 23:38 UTC
@puneetagg I'm afraid if DEBUG=True doesn't help you ("Is my urls.py parsed?"), without full source code disclosed there's no-one that can help you with your issue. The regex seems right, and what you're trying to achieve is usually straight-forward with Django.
Also, let me remind you that you still have not disclosed yet what the exact URL is that you use to produce the 404 on the URL pattern you mention.
My suspect: The webserver will usually add a trailing slash, the runserver won't. Try adding a trailing slash to the URL.