Django操作的默认参数(Default parameters to actions with Django)

编程入门 行业动态 更新时间:2024-10-13 22:19:56
Django操作的默认参数(Default parameters to actions with Django)

有没有一种方法可以在正则表达式不匹配任何使用django的情况下将一个默认参数传递给一个动作?

urlpatterns = patterns('',(r'^test/(?P<name>.*)?$','myview.displayName')) #myview.py def displayName(request,name): # write name to response or something

我已经尝试将urlpatterns中的第三个参数设置为一个字典,其中包含'并且为方法赋予name参数默认值,但其中没有一个可以工作。 name参数总是显示为None。 如果我可以设置一个默认值,我真的不想代码检查无。

澄清:这是我改变它的一个例子。

def displayName(request,name='Steve'): return HttpResponse(name) #i also tried urlpatterns = patterns('', (r'^test/(?P<name>.*)?$', 'myview.displayName', dict(name='Test') ) )

当我将浏览器指向视图时,它显示文本'无'

有任何想法吗?

Is there a way to have a default parameter passed to a action in the case where the regex didnt match anything using django?

urlpatterns = patterns('',(r'^test/(?P<name>.*)?$','myview.displayName')) #myview.py def displayName(request,name): # write name to response or something

I have tried setting the third parameter in the urlpatterns to a dictionary containing ' and giving the name parameter a default value on the method, none of which worked. the name parameter always seems to be None. I really dont want to code a check for None if i could set a default value.

Clarification: here is an example of what i was changing it to.

def displayName(request,name='Steve'): return HttpResponse(name) #i also tried urlpatterns = patterns('', (r'^test/(?P<name>.*)?$', 'myview.displayName', dict(name='Test') ) )

when i point my browser at the view it displays the text 'None'

Any ideas?

最满意答案

问题是,当模式匹配'test /'时,正则表达式捕获的groupdict包含映射'name'=> None:

>>> url.match("test/").groupdict() {'name': None}

这意味着当调用视图时,使用我期望的类似于下面的内容:

view(request, *groups, **groupdict)

相当于:

view(request, name = None)

为'test /',意思是这个名字被赋值为None而不是未赋值。

这给你两个选择。 您可以:

在视图代码中显式检查无,这是一种骇人听闻的方式。 重写URL调度规则以使名称捕获不可选,并在未提供名称时引入另一个规则以捕获。

例如:

urlpatterns = patterns('', (r'^test/(?P<name>.+)$','myview.displayName'), # note the '+' instead of the '*' (r'^test/$','myview.displayName'), )

当采取第二种方法时,您可以简单地调用没有捕获模式的方法,并让python处理默认参数,或者您可以调用不同的委托视图。

The problem is that when the pattern is matched against 'test/' the groupdict captured by the regex contains the mapping 'name' => None:

>>> url.match("test/").groupdict() {'name': None}

This means that when the view is invoked, using something I expect that is similar to below:

view(request, *groups, **groupdict)

which is equivalent to:

view(request, name = None)

for 'test/', meaning that name is assigned None rather than not assigned.

This leaves you with two options. You can:

Explicitly check for None in the view code which is kind of hackish. Rewrite the url dispatch rule to make the name capture non-optional and introduce a second rule to capture when no name is provided.

For example:

urlpatterns = patterns('', (r'^test/(?P<name>.+)$','myview.displayName'), # note the '+' instead of the '*' (r'^test/$','myview.displayName'), )

When taking the second approach, you can simply call the method without the capture pattern, and let python handle the default parameter or you can call a different view which delegates.

更多推荐

urlpatterns,参数,parameter,电脑培训,计算机培训,IT培训"/> <meta name="desc

本文发布于:2023-08-06 20:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455791.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   操作   Django   actions   parameters

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!