Opened 9 years ago

Closed 9 years ago

Last modified 9 years ago

#7041 closed (worksforme)

mistake in documentation - 'url_dispatch'

Reported by: andy baxter Owned by: nobody
Component: Documentation Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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 Changed 9 years ago by James Bennett

Resolution: worksforme
Status: newclosed

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.

comment:2 Changed 9 years ago by andy baxter

Resolution: worksforme
Status: closedreopened

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 Changed 9 years ago by James Bennett

Resolution: worksforme
Status: reopenedclosed

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 Changed 9 years ago by andy baxter

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.

Note: See TracTickets for help on using tickets.
Back to Top