I tried searching but could not find any answer to this, in Django crispy forms there is an HTML layout Object which allows you to inject customized HTML into your form, I am working on a CreateView Form which implements some arbitary creation of related elements, I inject the HTML under my field the following way:

HTML(
'''<p class="add">
      <a href="{% url 'manager-add-category' %}">{% trans 'Add a category' %}</a>
   </p>'''
),

My problem is that although url tags work ok, trans tags are not parsed within crispy forms, is there an alternative (maybe within crispy forms?) to maintain i18n? In the form template there is proper i18n loading of tags:

{% load i18n %}
{% load crispy_forms_tags %}
有帮助吗?

解决方案

Since you are trying to do it in some .py file (as i understood), then why do you bother with templatetags - use python.

injected_html = u"<p class='add'><a href='%(url)s'>%(translation)s</a></p>" % {'url':some_get_url_method(), 'translation':_(u"Add a category")}

HTML(injected_html)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top