Printing out HTML that doesn't get converted to

1 view
Skip to first unread message

Alex

unread,
May 15, 2008, 6:17:57 PM5/15/08
to Django users
I'm working on my first Django project and I can't seem to get
information printed out as I would like it. I'm trying to print out
quotes to a page with the newlines turned into HTML <br> tags (similar
to PHP's nl2br function). So far I've been able to do this using the
__str__ function for the Quote model, but it escapes the tags into
html entities that I see the tags in my browser instead of the line
breaks. I'm using the development version of Django.

Here's what my __str__ function looks like:

return self.quote.replace('\n', '<br />')

And I'm just printing them out with a generic view that looks like
this:

{% for quote in object_list %}
<div class="quote">{{ quote }}
<div class="caption" align="right">
{% for a in quote.authors.all %}
&mdash;{{ a }}<br />
{% endfor %}
</div>
</div>
{% endfor %}

I'd appreciate any suggestions. I'm rather new to Python, so I
appreciate all the help I can get :)

--Alex

jonknee

unread,
May 15, 2008, 6:47:57 PM5/15/08
to Django users
On May 15, 6:17 pm, Alex <got...@gmail.com> wrote:
> I'm working on my first Django project and I can't seem to get
> information printed out as I would like it. I'm trying to print out
> quotes to a page with the newlines turned into HTML <br> tags (similar
> to PHP's nl2br function).

You're looking for linebreaks, a filter in Django templates:

http://www.djangoproject.com/documentation/templates/#linebreaks

Alex

unread,
May 15, 2008, 6:52:38 PM5/15/08
to Django users
Wow, that was incredibly simple. Thanks for the help! I have a strong
feeling I'm not going back to PHP...

--Alex

Adi J. Sieker

unread,
May 16, 2008, 4:45:40 AM5/16/08
to django...@googlegroups.com
Alex wrote:
> I'm working on my first Django project and I can't seem to get
> information printed out as I would like it. I'm trying to print out
> quotes to a page with the newlines turned into HTML <br> tags (similar
> to PHP's nl2br function). So far I've been able to do this using the
> __str__ function for the Quote model, but it escapes the tags into
> html entities that I see the tags in my browser instead of the line
> breaks. I'm using the development version of Django.
>
> Here's what my __str__ function looks like:
>
> return self.quote.replace('\n', '<br />')
>
> And I'm just printing them out with a generic view that looks like
> this:
>
> {% for quote in object_list %}
> <div class="quote">{{ quote }}
You need to run your HTML through the safe filter.
Django auto escapes HTML.
so the above would be {{ quote|safe}}

see
http://www.djangoproject.com/documentation/templates/#automatic-html-escaping

adi

--
Adi J. Sieker mobile: +49 - 178 - 88 5 88 13
Freelance developer skype: adijsieker
SAP-Consultant web: http://www.sieker.info/profile
openbc: https://www.openbc.com/hp/AdiJoerg_Sieker/

Gabriel

unread,
May 16, 2008, 7:35:42 AM5/16/08
to django...@googlegroups.com
Alex <gotfef <at> gmail.com> writes:

>
>
> I'm working on my first Django project and I can't seem to get
> information printed out as I would like it. I'm trying to print out
> quotes to a page with the newlines turned into HTML <br> tags (similar
> to PHP's nl2br function). So far I've been able to do this using the
> __str__ function for the Quote model, but it escapes the tags into
> html entities that I see the tags in my browser instead of the line
> breaks. I'm using the development version of Django.
>

Hi Alex

AFAIK the Django dev version automatically escapes output unless you mark it
safe. Check the docs for more info.

Tomás Garzón Hervás

unread,
May 19, 2008, 1:51:07 AM5/19/08
to django...@googlegroups.com
I think, you use the {% autoescape off %} text to escape {% endautoescape %}
Search more information of autoescape en django documentation
Gabriel escribió:


--
-------------------------------------------------------------------
Tomás Garzón Hervás
Director de Desarrollo y Calidad IACTIVE Intelligent Solutions, S.L.
--
Teléfono Móvil: 646760508

--

Bussines & Innovation Center (BIC)
Parque tecnológico de ciencias de la salud
Avd. de la innovación 1
C.P. 18100 Armilla (Granada)

-------------------------------------------------------------------

James Bennett

unread,
May 19, 2008, 2:45:20 AM5/19/08
to django...@googlegroups.com
On Mon, May 19, 2008 at 12:51 AM, Tomás Garzón Hervás
<t.ga...@iactive.es> wrote:
> I think, you use the {% autoescape off %} text to escape {% endautoescape %}
> Search more information of autoescape en django documentation

Turning autoescaping on and off for large sections of a template is a
sort of sledgehammer approach. For specific variables, as the case
seems to be here, the "safe" filter is much more appropriate and
fine-grained.


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Reply all
Reply to author
Forward
0 new messages