#7041 closed (worksforme)
mistake in documentation - 'url_dispatch'
Reported by: | andy baxter | Owned by: | nobody |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I think there is a mistake in the documentation about writing named url patterns.
This example is included on the page:
http://www.djangoproject.com/documentation/url_dispatch/#naming-url-patterns
urlpatterns = patterns('', url(r'/archive/(\d{4})/$', archive, name="full-archive"), url(r'/archive-summary/(\d{4})/$', archive, {'summary': True}, "arch-summary"), )
Note that this shows two different ways of attaching a name to an url. The second way works, but not the first. Also, it would probably help if you told people that where there are no parameters to the view, you need to pass an empty dictionary in urlpatterns before giving the url name. E.g.:
urlpatterns = patterns('', url(r'/archive/(\d{4})/$', archive, {}, "full-archive"), url(r'/archive-summary/(\d{4})/$', archive, {'summary': True}, "arch-summary"), )
Change History (4)
comment:1 by , 17 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
comment:2 by , 17 years ago
Resolution: | worksforme |
---|---|
Status: | closed → reopened |
I have worked out what my mistake was - I hadn't noticed that when using named urls, you need to put the url definitions inside the 'url' function. So I was doing:
urlpatterns = patterns('', (r'/archive/(\d{4})/$', archive, name="full-archive"), ... )
instead of:
urlpatterns = patterns('', url(r'/archive/(\d{4})/$', archive, name="full-archive"), ... )
I don't know whether this is a common mistake (made when converting a list of un-named url patterns to named ones), but if so it might be worth adding something to the 'Note' box below the example telling people not to forget to add the 'url' function prefix.
comment:3 by , 17 years ago
Resolution: | → worksforme |
---|---|
Status: | reopened → closed |
The documentation is correct here, so I'm pushing this closed again, and I guess I'm kind of stumped as to how it could be rewritten to be more helpful -- no offense here, but trying to pass keyword names into a tuple is a mistake that shows a fundamental unfamiliarity with Python, and if we tried to document every place in Django where that could happen we'd never have time to do much else...
comment:4 by , 17 years ago
OK, fair enough. I did work out in the end from what I knew of python that you couldn't pass arguments like this, just made a mistake in copying the code in the example without looking at it properly.
I use the first form (keyword arguments to
url()
all the time and I've never seen it not work. If you're running into problems, consider asking for help on the django-users mailing list.