Django urls regex for query string

旧时模样 提交于 2020-01-15 07:15:27

问题


Following is from urls.py:

url(r'^\?view=(?P<vtype>instructor|course|room)$', 'index', name='index'),

i can verify that it works by simply calling django.core.urlresolvers.reverse in shell :

In [6]: reverse('index', args=["course"])
Out[6]: '/?view=course'

but when i try to access http://localhost:8000/?view=course i get 404.

What am i doing wrong here?

Thanks

Edit:

url('^search/\?user=(?P<userid>\d+)&type=topic', 'search_forum', name='my_topics'),

this is from a former project which works as expected. sigh...


回答1:


Query string is not part of the URL. If you want to do it this way, you have to use url(r'^$', 'index', name='index') and then look it up in request.GET dictionary in the view.

The usual way, however, is to use url(r'(?P<vtype>instructor|course|room)/$', 'index', name='index'). The querystring approach is the usual workaround for not being able to direct requests according to the non-querystring URL part. Django does not have that limitation.




回答2:


Your regex matches from the beginning (^) but I don't see anything to match the leading '/'. Could this be it?



来源:https://stackoverflow.com/questions/6011146/django-urls-regex-for-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!