5π
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>)
44π
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>?)'))
11π
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>
9π
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)
2π
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 %}
1π
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>.
1π
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'))))