Reputation: 4680
I want to have a checkbox for terms and conditions, label of which should contain a link to a page with, well, terms and conditions.
The following field will have the label with the tags escaped.
BooleanField(label="I agree to <a href='terms-conditions.html'>terms&conditions</a>")
Upvotes: 21
Views: 22717
Reputation: 742
I know that this question is old but I have found a solution
forms.py
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'))))
Upvotes: 3
Reputation: 132
I also think that to hardcode link in label isn't a good variant. so it'll be better to use render_to_string
from django.template.loader
. Also we need to think about laziness like for i18n. keep_lazy_text
and lazystr
fits our requirements for it. So the result it:
forms.py
from django.utils.functional import keep_lazy_text, lazystr
from django.template.loader import render_to_string
from allauth.account.forms import SignupForm
lazy_render_to_string = keep_lazy_text(render_to_string)
def lazy_render(template_name):
"""
Keep laziness for rendering to string from template
"""
return lazy_render_to_string(lazystr(template_name))
class ExtendedSignupForm(SignupForm):
accept_term_of_use = forms.BooleanField(
label=lazy_render('common/accept_term_of_use_label.html'),
required=True
)
field_order = [
'email',
'password1',
'password2',
'accept_term_of_use'
]
common/accept_term_of_use_label.html
Accept <a href="{% url "term_of_use" %}" target="_blank">Term Of Use</a> and <a href="privacy_statement" target="_blank">Privacy Statement</a>.
Upvotes: 2
Reputation: 1548
If do not wish to hardcode the URL, you could set the field's label from the constructor:
from django import forms
from django.core.urlresolvers import reverse_lazy
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
class ExampleForm(forms.Form):
name = forms.CharField(max_length='255', label=_('Name'))
forms.BooleanField(label=_("Terms and Conditions"), required=False)
def __init__(self, **kwargs):
super(ExampleForm, self).__init__(**kwargs)
terms_and_conditions = reverse_lazy("terms_and_conditions")
self.fields['read_terms'].label = mark_safe(_("I have read and agree with the "
"<a href='%s'>Terms and Conditions</a>")) % (terms_and_conditions)
Upvotes: 9
Reputation: 331
Here is the correct link to the Django documentation on the subject of iteration over the form.
What you want is:
<form method="post">
{% for field in form %}
<p>
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
{% if field.name == "toc" %}
<a href="{% url terms %}">Terms and Conditions</a>
{% endif %}
</p>
{% endfor %}
<p><input type="submit" value="Send message" /></p>
</form>
Upvotes: 11
Reputation: 1097
Import django's mark_safe from utils.safestring as explained at http://www.kelvinism.com/howtos/using-html-django-form-label/
From the link:
from django.utils.safestring import mark_safe
from django import forms
class AccountForm(forms.Form):
name = forms.CharField(widget=forms.TextInput(),
max_length=15,
label=mark_safe('Your Name (<a href="/questions/whyname/" target="_blank">why</a>?)'))
Upvotes: 46
Reputation: 10227
Let me guess - you're using django_registration
- I see I'm not the only person who had this problem:)
As scunliffe said markup is best left out of the label tag - here is a short template snippet to do what you want without resorting to writing the whole thing out in HTML:
{% for field in form %}
{{ field.label_tag }}
{% ifequal field.name 'tos' %}
<a href="/terms/">I have read and agreed to the Terms of Service</a>
{% endif %}
{% endif %}
Upvotes: 2
Reputation: 63580
I'm not sure how django works, but if the label in the above code creates a:
<label>I agree to terms&conditions</label>
element then this is NOT the way to do this.
The label element inherently selects the associated field by default, so you'd be mixing 2 click actions (one that checks, and another that opens a window)
I would just add your label, and an a:href beside it.
<label>I agree to terms&conditions</label> (<a href="terms-conditions.html">terms&conditions</a>)
Upvotes: 5