可选参数在django中? [英] Optional get parameters in django?

查看:119
本文介绍了可选参数在django中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下如何编写一个url模式,并且可以查看允许可选参数?我已经成功完成了,但我总是打破url模板标签。



这是我目前所在:



格式

 (r'^ so /(?P< required> \d + /?(?P<optional>(.*))/?$','myapp.so')

查看

  def so(请求,必需,可选):

如果我在这个例子中使用url模板标签提供两个参数,它的工作原理很好;但是,如果我省略了可选参数,我会收到一个反向错误。



我该如何实现?



谢谢,
Pete

解决方案

我通常使用 named url

  url(r'^ so /(?P< required> \d +)/ $','myapp.so',name ='something'),
url '^ so /(?P< required> \d +)/(?P< optional>。*)/ $','myapp.so',name ='something_else'),


Can someone please explain how you can write a url pattern and view that allows optional parameters? I've done this successfully, but I always break the url template tag.

Here's what I have currently:

Pattern

(r'^so/(?P<required>\d+)/?(?P<optional>(.*))/?$', 'myapp.so')

View

def so(request, required, optional):

If I use the url template tag in this example providing both arguments, it works just fine; however, if I omit the optional argument, I get a reversing error.

How can I accomplish this?

Thanks, Pete

解决方案

I generally make two patterns with a named url:

url(r'^so/(?P<required>\d+)/$', 'myapp.so', name='something'),
url(r'^so/(?P<required>\d+)/(?P<optional>.*)/$', 'myapp.so', name='something_else'),

这篇关于可选参数在django中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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