Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#7041 closed (worksforme)

mistake in documentation - 'url_dispatch'

Reported by: andyb 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 7 years ago by ubernostrum

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

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 7 years ago by andyb

  • Resolution worksforme deleted
  • Status changed from closed to 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 Changed 7 years ago by ubernostrum

  • Resolution set to worksforme
  • Status changed from reopened to 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 Changed 7 years ago by andyb

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