yet another unicode question...

0 views
Skip to first unread message

Juan Hernandez

unread,
Mar 18, 2009, 11:32:34 AM3/18/09
to Django users
Hi there,

This has been killing me for hours and I don't know what else to do. Is about the famous charset in django.

I have this model:

class Post(models.Model):
    user = models.ForeignKey(User)
    category = models.ForeignKey(Categories)
    title = models.CharField(max_length=50)
    post = models.CharField(max_length=10000)
    date = models.DateTimeField(auto_now_add=True)
   
    def __unicode__(self):
        return u%s %s %s %s %s(self.user, self.category, self.title, self.post, self.date)

with this information on the database:

mysql> select * from blog_post;
+----+---------+-------------+------------------+--------------------------------------------                                 +---------------------+
| id | user_id | category_id | title            | post                                                                  | date                |
+----+---------+-------------+------------------+--------------------------------------------                                +---------------------+
|  8 |       1 |           2 | html <i>Aqui</i> | asdfasd <em><strong>sdfsdfsd</strong></em> | 2009-03-18 01:27:49 |
+----+---------+-------------+------------------+--------------------------------------------                                  +---------------------+
8 rows in set (0.00 sec)

and the post column is being rendered on django html like this:

asdfasd &lt;em&gt;&lt;strong&gt;sdfsdfsd&lt;/strong&gt;&lt;/em&gt;

What is going on? I have changed the CHARACTER_SET in settings.py and followed many things on different threats with no success at all

Everything on the DB is utf8 and I also have DEFAULT_CHARSET = 'utf8' at settings.py

How can I have django display html code from the databse without any problems?

Thanks a lot
Juan

Alex Gaynor

unread,
Mar 18, 2009, 11:34:53 AM3/18/09
to django...@googlegroups.com
This has nothing to do with your db charset, this is Django's template autoescape feature, you can have it render as is by doing {{ field|safe }} that is, using the safe filter.

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

Briel

unread,
Mar 18, 2009, 11:44:19 AM3/18/09
to Django users
Hi.
I'm not sure what exactly you are doing and how/where you are
displaying this stuff. But it sounds like you are escaping the html,
django does this by default to protect sites from XSS attacks ect. You
can either stop the auto escaping in the template or use the |safe
tag. You can read about django auto escaping in the docs at:
http://docs.djangoproject.com/en/dev/topics/templates/#id2

~Jakob

Adi Sieker

unread,
Mar 18, 2009, 11:47:55 AM3/18/09
to django...@googlegroups.com
Hi,

and see if it helps you along.

adi


Thanks a lot
Juan





--
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/



Reply all
Reply to author
Forward
0 new messages