-4

I have been working on a new Django site and I am having problems with the url dispatcher.

Basically I need help getting the following urls working.

/site/aaa-00

/site/aaa-00-00

I have looked at the URL dispatcher on django docs and im more confused now.

url(r'^/site/(?P<name>[-\w]+)/$', 'rollout.views.update'),
Tms91
  • 3,456
  • 6
  • 40
  • 74
xc0m
  • 121
  • 5

1 Answers1

2

Your regex isn't going to do the job. I'd expect it to look more like:

^site/(?P<path>[-\w]+)/$

to do what you want. Two main differences: URL patterns shouldn't match against a leading slash, ie the slash gets stripped (or, more accurately, isn't part of the path component of a URL, but that's getting pedantic); and ?P in expressions should take a name for the group (which gets converted into a parameter to your view function).

One other thing you may not be aware of: you have a trailing / at the end of your URLconf line, but not in the URLs you're trying to match. Note that by default Django (with the CommonMiddleware running) will auto-redirect to include a trailing / unless the path already matches something in the URLconf; this can be controlled using the APPEND_SLASH configuration option. That should "just work" in your case, although it results in a redirect so you shouldn't emit URLs without the trailing slash (or make the slash optional in the URLconf).

James Aylett
  • 3,332
  • 19
  • 20