djangotemplatesplaintexttemplatetag

Django plaintext template


I'm currently writing a template which mostly outputs my fields and it's contents from the database as plaintext so that it can be downloaded (supposed to be a configuration file for ltsp) and I'm in a bind.

I often do things like this:

{% for model in modelqueryset %}
...
{% ifnotequal model.fieldx "" %}
    fieldx = {{ model.fieldx }}
{% endifnotequal %}
...
{% endfor %}

"..." is/are for a long list/a lot of entries of:

{% ifnotequal model.fieldy "" %}
    fieldy = {{ model.fieldy }}
{% endifnotequal %}

Now if the fieldx is in fact empty it displays an empty line however that just takes up unneccesary space and will make the plaintext difficult to read. Now on to the question:

How can I remove those empty lines? I tried {% spaceless %}...{% endspaceless %} and it doesn't really help. Do I have to write a custom templatetag or did I do something wrong or overlook something?

Any help is appreciated and I will also already say Thanks


Solution

  • You have an empty line because of a linebreak.

    ... <- here
    {% ifnotequal model.fieldx "" %}
        fieldx = {{ model.fieldx }}
    {% endifnotequal %}
    

    So you can rewrite it like this

    ...{% ifnotequal model.fieldx "" %}
           fieldx = {{ model.fieldx }}
       {% endifnotequal %}
    

    Or try StripWhitespaceMiddleware