开发者

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 real开发者_如何学Cly 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


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


You don't have to use a template for everything - it might be easier to use a plain HttpResponse constructor and build the text for output in Python:

>>> response = HttpResponse()
>>> response.write("<p>Here's the text of the Web page.</p>")
>>> response.write("<p>Here's another paragraph.</p>")


As @DrTyrsa said, you can use StripWhitespaceMiddleware. Alternatively, if you only occasionally want to strip the whitespace, you can pull the core of this middleware out into a utility class like this:

import re
from django.template import loader, Context

class StripWhitespace():
    """
    Renders then strips whitespace from a file
    """

    def __init__(self):
        self.left_whitespace = re.compile('^\s+', re.MULTILINE)
        self.right_whitespace = re.compile('\s+$', re.MULTILINE)
        self.blank_line = re.compile('\n+', re.MULTILINE)


    def render_clean(self, text_file, context_dict):
        context = Context(context_dict)
        text_template = loader.get_template(text_file)
        text_content = text_template.render(context)
        text_content = self.left_whitespace.sub('', text_content)
        text_content = self.right_whitespace.sub('\n', text_content)
        text_content = self.blank_line.sub('\n', text_content)
        return text_content

Then you can use it in your views.py like this:

def text_view(request):
    context = {}
    strip_whitespace = StripWhitespace()
    text_content = strip_whitespace.render_clean('sample.txt', context)
    return HttpResponse(text_content)

Note that I added a blank_line regular expression so you can remove ALL blank lines. If you still want to see one blank line between sections, you can remove this regex.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜