Django urls regex for query string

2019-07-21 02:09发布

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

2条回答
ら.Afraid
2楼-- · 2019-07-21 02:50

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

查看更多
小情绪 Triste *
3楼-- · 2019-07-21 02:52

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.

查看更多
登录 后发表回答