Search code examples
djangotemplatetags

Why is the variable set using the django 'with' tag broken into individual strings when using it in a template provided by the include tag?


I am using the with tag to declare a variable my_var. I am using my_var as a placeholder in my input box. Only the first word of my_var is recognized as part of the variable. How do I get the whole string to be recognized? I have explicitly put in "placeholder='this is a test'" and all of the words show up as a place holder which leads me to believe it has something to do with using django templatetags and not the placeholder itself. This picture shows only the word "Must" being displayed.

    <input type="text" class="form-control" id="username" placeholder="Must" be="" at="" least="" 4="" characters="" long="" (underscore="" period).="">

The above shows my_var being broken into pieces, what is wrong? Here is the code that declares my_var

    {% with form.username as field %}
            {% with "Must be at least 4 characters long." as my_var%}
                {% include "emp/regfield.html" %}
            {% endwith %}
    {% endwith %}

regfield.html looks like this

    <div id="div_{{ field.name }}" class="form-group">
        <label for="id_{{ field.name }}">{{ field.label }}</label>
       <input type="text" class="form-control" id="{{ field.name }}" placeholder={{ my_var }}>
       <div id="{{ field.name }}_error" class="error_div">
            {{ field.errors }}
       </div>
    </div>

Solution

  • I think you might be missing some quotation marks around your templated {{my_var}} - change to "{{my_var}}".