'Hitting returns' in template files are interpreted as NewLines while I want it interpreted as nothing
introduction
hi, I am trying to use django's view-template to export some contents to a text files with some format, e.g. carriage return, indentionh etc. But seems every control statement of django templte(if/for/else) are all interpreted as new lines in the exported TXT files, which make my exported files ugly. The followings are my code related:
code
- views
!#python
categories = Category.objects.filter(field__in=request.POST.getlist("categories"))
limits = request.POST.getlist("category_limits")
response = HttpResponse(mimetype="text/csv; charset=utf-16-be")
response["Content-Disposition"] = "attachment; filename=indesign_export.txt"
template = loader.get_template("indesign_export.txt")
context = viewdata(city, categories, limits)
response.write(unicode(template.render(context), "utf-8").encode("utf-16-be"))
return response
- templates indesign_export.txt
<UNICODE-MAC>
{% for category in categories %}
{% for subcategory in category.subcategories %}
{% for listing in subcategory.listings %}
{% if listing.english_menu_ok %} English menu, {% endif %}
{% if listing.wifi_ok %} Wi-Fi internet, {% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
- Currently what resolution I have is not using Templare.reder(), I mean writing the contents directly to a file, whose content will be outputed as HttpResponse('file_content'). But this violates the MVC priciples, no VIEW, that is templates in django.
- So I think this is a bug of the template system. Thanks.
Change History
(4)
Summary: |
'Hitting returns' in template files are interpreted as NewLines while I need it interpreted as plain text → 'Hitting returns' in template files are interpreted as NewLines while I need it interpreted as nothing
|
Summary: |
'Hitting returns' in template files are interpreted as NewLines while I need it interpreted as nothing → 'Hitting returns' in template files are interpreted as NewLines while I want it interpreted as nothing
|
Resolution: |
→ invalid
|
Status: |
new → closed
|
This is expected template behavior. Every character you type -- yes, including newlines -- is echoed into the output unless it's a template tag or variable. You might take a look at the spaceless tag, or just be careful about where you put newlines.