Django urls为正则表达式查询字符串 [英] Django urls regex for query string

查看:454
本文介绍了Django urls为正则表达式查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是urls.py:

Following is from urls.py:

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

可以通过在shell中调用django.core.urlresolvers.reverse来验证它的工作原理:

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'

,但是当我尝试访问 http:// localhost:8000 /?view = course
i获得404。

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

我在这里做错什么?

谢谢

编辑:

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

这是一个从前的项目,按预期工作。叹息...

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

推荐答案

查询字符串不是URL的一部分。如果你想这样做,你必须使用 url(r'^ $','index',name ='index')然后查找请求.GET字典在视图中。

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.

然而,通常的方法是使用 url(r'(?P< vtype>课程|房间)/ $','index',name ='index')。 querystring方法是根据非查询字符串URL部分无法直接请求的常见解决方法。 Django没有这个限制。

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.

这篇关于Django urls为正则表达式查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆