在布尔值分配中存储布尔变量 [英] Store boolean variable in url dispatching

查看:82
本文介绍了在布尔值分配中存储布尔变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下url定义应通过url中是否存在 results /

The following url definition should pass whether results/ is present in the url:

url(r'^(?P<question_id>[0-9]+)/(?P<results>(results/)?)shorten/$', views.shorten, name='shorten')

当前它通过 results / 就足够简单了:

Currently it passes results/ or None which is sufficient for a simple:

if results:
    pass

但是拥有 True False

推荐答案

您可以有两个URL模式并传递结果在kwargs中:

You could have two URL patterns and pass results in the kwargs:

url(r'^(?P<question_id>[0-9]+)/results/shorten/$', views.shorten, {'results': True}, name='shorten'),
url(r'^(?P<question_id>[0-9]+)/shorten/$', views.shorten, {'results': False}, name='shorten'),

如果您不想这样做,那么当前还没有一种简单的方法可以将 results 字符串转换为布尔值。您可以编写一个中间件或装饰器,但这太过分了。

If you don't want to do this, then there isn't currently a simple way to cast the results string to a boolean value. You could write a middleware or decorator, but that would be overkill.

这篇关于在布尔值分配中存储布尔变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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