Code

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#2244 closed defect (invalid)

'Hitting returns' in template files are interpreted as NewLines while I want it interpreted as nothing

Reported by: chengqi@… Owned by: adrian
Component: Template system Version: master
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

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.

Attachments (0)

Change History (4)

comment:1 Changed 8 years ago by anonymous

  • Summary changed from 'Hitting returns' in template files are interpreted as NewLines while I need it interpreted as plain text to 'Hitting returns' in template files are interpreted as NewLines while I need it interpreted as nothing

comment:2 Changed 8 years ago by anonymous

  • Summary changed from 'Hitting returns' in template files are interpreted as NewLines while I need it interpreted as nothing to 'Hitting returns' in template files are interpreted as NewLines while I want it interpreted as nothing

comment:3 Changed 8 years ago by jacob

  • Resolution set to invalid
  • Status changed from new to 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.

comment:4 Changed 8 years ago by adrian

  • milestone Version 0.93 deleted

Milestone Version 0.93 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.