Django tutorial part 3: Always shows raw HTML

41 views
Skip to first unread message

Helly

unread,
Sep 17, 2012, 3:02:20 PM9/17/12
to django...@googlegroups.com
I'm currently going through the Django tutorial, and everything was going perfectly smoothly until the template part. This is what I always get for the polls/index.html:


I noticed that it gets everything right (the {{ poll.id }} is 1, there's no other error) but just doesn't seem to "understand" html.

The only answer I found related to this problem mentioned autoescaping.

So I added the {% autoescape off %} {% endautoescape %} tags to the index.html template (this is my polls/index.html, btw):
 

But, it didn't fix the problem. I have tried this template without those tags, with autoescape on, and with the tags in various different positions. Changing the tags around moves the text around a little bit, does nothing, or produces a syntax error. I also tried it with the render_to_response() shortcut, and it still displayed the same, raw HTML.

When I Google this, other answers mostly regard what to do if you do want the raw html to show up. Also, it doesn't really make sense for the default to be leaving my HTML hanging out for everyone to see.

This is my views.py: http://pastebin.com/74t5PMgj


I didn't experiment at all. They are exactly what is written in the tutorial. I am on Mac OS X 10.7.4, Django 1.4.1, Python 2.7.1. I am just doing the tutorial on my localhost (127.0.0.1:8000). What is going on? Normally, I can load HTML to my browser just fine. I'm very new to this so I could absolutely be missing something totally obvious. Any help would be greatly appreciated! In the meantime, I will keep trying at this.

Helly

unread,
Sep 17, 2012, 4:45:01 PM9/17/12
to django...@googlegroups.com
I am SO SORRY, and in the future I promise to wait at least a week before posting my problems to see if I can figure it out in the meantime. I fixed this by saving the file in TextWrangler instead of TextEdit, and now it interprets the HTML properly. If anyone knows why this fixes the problem, I would still appreciate your insight, but it appears that I have fixed the main issue for now.

Nikolas Stevenson-Molnar

unread,
Sep 17, 2012, 6:11:36 PM9/17/12
to django...@googlegroups.com
Text Wranger is a plain-text only editor, whereas TextEdit is a rich text editor by default (you can toggle plain text). This means that when you type HTML characters in to TextEdit, such as <strong>Blah</strong>, you're not getting true <> characters, but something like &lt; (<) and &gt; (>). So your template probably actually had something like &lt;strong&gt;Blah&lt;/strong&gt; which is, of course, not an HTML tag. TextWrangler, on the other hand, doesn't do rich text at all, so what you see is what gets saved to the file.

_Nik
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/pPpt3enw-bwJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Nikolas Stevenson-Molnar

unread,
Sep 17, 2012, 6:19:46 PM9/17/12
to django...@googlegroups.com
To go a bit further: never use TextEdit to edit source code of any kind, even in plain text mode. It's simply not designed for that. TextWrangler makes a great source editor and has some very handy features. However, for full-blown projects with many source files, etc. you may find it easier to use an IDE. Aptana is free and decent: http://www.aptana.com/. PyCharm costs money, but is amazing for Python projects; the debugger along is worth the price of admission, IMO: http://www.jetbrains.com/pycharm/

_Nik
Reply all
Reply to author
Forward
0 new messages