[Fixed]-How to have a link in label of a form field



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>)


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(), 
                           label=mark_safe('Your Name (<a href="/questions/whyname/" target="_blank">why</a>?)'))


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 %}
        {{ field.errors }}
        {{ field.label_tag }}: {{ field }}
        {% if field.name == "toc" %}
          <a href="{% url terms %}">Terms and Conditions</a>
        {% endif %}
{% endfor %}
<p><input type="submit" value="Send message" /></p>


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)


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 %}


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:


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(

    field_order = [


Accept <a href="{% url "term_of_use" %}" target="_blank">Term Of Use</a> and <a href="privacy_statement" target="_blank">Privacy Statement</a>.



I know that this question is old but I have found a solution


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'))))
πŸ‘€Diego Bianchi

Leave a comment