Django:如何在表单标签中包含 url 链接
我的用例看起来非常基本,但我在网上找不到任何东西!
这个想法是一个带有复选框“我已阅读并同意条款和条件”的表格 还有一个“条款和条件”链接,指向包含此类条款和条件的页面...... 经典的!
所以我的表单中有一个字段如下:
tos = forms.BooleanField(widget=forms.CheckboxInput(),
label=_(u'I have read and agree to the <a href="%s" target="_blank">terms and conditions</a>' % reverse('terms_of_use')),
initial=False)
其中“使用条款”是 urls.py 中我的 url 模式之一的名称
但是我收到错误:
ImproperlyConfigured: The included urlconf urls doesn't have any patterns in it
我的 urlconf 在整个网站上运行良好,所以我认为问题是渲染表单时 urlconf 是否尚未填充?
我尝试使用lazy_reverse=lazy(reverse,str)而不是reverse,但它没有解决任何问题。
有办法让这个工作吗?该用例看起来非常非常基本,所以肯定有一种方法可以做到这一点,而不必分解我的模板内的表单?!
My use case looks very basic but I couldn't find anything on the web!
The idea is a form with checkbox "I have read and agree to the terms and conditions"
And a link on "terms and conditions" which points to a page with such terms and conditions...
Classic!
So I have a field in my form as follows:
tos = forms.BooleanField(widget=forms.CheckboxInput(),
label=_(u'I have read and agree to the <a href="%s" target="_blank">terms and conditions</a>' % reverse('terms_of_use')),
initial=False)
where 'terms of use' is the name of one of my url patterns in urls.py
But I get an error:
ImproperlyConfigured: The included urlconf urls doesn't have any patterns in it
My urlconf works fine on the whole site so I supposed that the problem was that the urlconf is not yet populated when the form is rendered ?
I tried using lazy_reverse = lazy(reverse, str) instead of reverse, but it doesn't solve anything.
Is there a way to make this work ? The use case seems very very basic so there surely is a way to do it without having to break up the form inside my template ?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用这个语法
I use this syntax
lazy_reverse 不起作用,因为您在使用
"...%s..." % lazy(blah)
表示法后第二秒就转身并解除了它的惰性。我想你可以尝试偷懒整个事情,即
但我没有测试这个
,只是覆盖
__init__
处的标签,即lazy_reverse won't work since you're turning around and unlazying it the second after with your
"...%s..." % lazy(blah)
notation.I suppose you could try to lazy the whole thing, i.e.
but I did not test this
alternatively, just override the label at
__init__
, i.e.您可以提供一个链接,如下所示的表单标签:
请参阅AppRegistryNotReady:lazy format_html ()?
You can provide a link a form label like this:
See AppRegistryNotReady: lazy format_html()?