问题描述:

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 haha.

url(r'^/site/(?P[-\w]+)/$', 'rollout.views.update'),

Cheers

Xc0m

网友答案:

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).

相关阅读:
Top