Django:如何在表单标签中包含 url 链接

发布于 2024-11-17 02:12:09 字数 771 浏览 1 评论 0原文

我的用例看起来非常基本,但我在网上找不到任何东西!

这个想法是一个带有复选框“我已阅读并同意条款和条件”的表格 还有一个“条款和条件”链接,指向包含此类条款和条件的页面...... 经典的!

所以我的表单中有一个字段如下:

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)而不是rev​​erse,但它没有解决任何问题。

有办法让这个工作吗?该用例看起来非常非常基本,所以肯定有一种方法可以做到这一点,而不必分解我的模板内的表单?!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三生池水覆流年 2024-11-24 02:12:09

我使用这个语法

from django.urls import reverse
from django.utils.functional import lazy

privacy = forms.BooleanField(label = lazy(lambda: _("Privacy <a href='%s' a>policy</a>" % reverse('privacy'))))

I use this syntax

from django.urls import reverse
from django.utils.functional import lazy

privacy = forms.BooleanField(label = lazy(lambda: _("Privacy <a href='%s' a>policy</a>" % reverse('privacy'))))
万人眼中万个我 2024-11-24 02:12:09

lazy_reverse 不起作用,因为您在使用 "...%s..." % lazy(blah) 表示法后第二秒就转身并解除了它的惰性。

我想你可以尝试偷懒整个事情,即

label = lazy(lambda: _("bla %s bla" % reverse('something')))

但我没有测试这个

,只是覆盖 __init__ 处的标签,即

self.fields['myfield'].label = 'blah %s bla' % reverse('bla')

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.

label = lazy(lambda: _("bla %s bla" % reverse('something')))

but I did not test this

alternatively, just override the label at __init__, i.e.

self.fields['myfield'].label = 'blah %s bla' % reverse('bla')
岁月苍老的讽刺 2024-11-24 02:12:09

您可以提供一个链接,如下所示的表单标签:

foo_filter=forms.ModelChoiceField(FooFilter.objects.all(),
               label=format_html('<a href="{}">{}</a>', reverse_lazy('foo-filter'), 
                                 FooFilter._meta.verbose_name))

请参阅AppRegistryNotReady:lazy format_html ()?

You can provide a link a form label like this:

foo_filter=forms.ModelChoiceField(FooFilter.objects.all(),
               label=format_html('<a href="{}">{}</a>', reverse_lazy('foo-filter'), 
                                 FooFilter._meta.verbose_name))

See AppRegistryNotReady: lazy format_html()?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文