-3

I am trying to make a field with Django Tweaks and I am trying to add some HTMX attributes. I want to use the following syntax:

{% render_field field <other attributes> hx-vals='{"country": this.value}' %}

However, if I use this syntax, I get an rendering error. This is because it tries to interpret the brackets.

I have searched a lot on the internet, and each answer for escaping characters in Django Templates, suggests using the verbatim tag or something similar. The problem is that that only allows you to render special characters, not use them in a variable.

Is there a solution that allows me to escape special characters in Django template variables?

Right now I am sending the string in the context, but that is not ideal.

Thanks in advance!

3
  • Can you share the full error? Commented Feb 28 at 9:48
  • always put full error message because there are other useful information. Commented Feb 28 at 10:05
  • maybe django allows to use {{ to render it as { Commented Feb 28 at 10:08

1 Answer 1

-1

I have solved the problem now by putting the special characters in seperate variables. I also had an issue with putting special characters in a URL, which I solved by putting in non-special characters and replacing them.

{% url 'subdivisions' 'replace' as full_url %}
{% with full_url|cut:"/replace"|add:"{country}" as full_url %}
  {% with "{'country': 'None'}" as hx_attrs %}
    {% render_field field hx-get=full_url hx-trigger="change" hx-target="#id_principal_subdivision" hx-swap="outerHTML" hx-attrs=hx_attrs %}
  {% endwith %}
{% endwith %}

A cleaner way to put in the URL would be appreciated.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.