Salma Salma - 3 months ago 9
Javascript Question

send a list of string elements in Django

I am new to Django. This is my Tag model

class Tag(models.Model):
name=models.CharField(max_length=255)
post=models.ManyToManyField('Post')


I send a list of all tags name to a template. I want to use it in a javascript code. The problem is that, the list looks like this in javascript

[u'c++', u'c#', u'php', u'python',
u'django']


It should be as this


['c++', 'c#', 'php', 'python', 'django']


I think it's some thing about unicode, but I do not know how to handel it. any help please?

excuse my bad english

Answer

Can you tell me whether the mistake is when you render the first time the view, or is an ajax call?, I think you are doing ajax call, then can you debug the django process, and write here what are the datas that you recover of your database, please?.

In addition, can you put the json serializer that you are using please?

If you try the next code, you can see the problem is not unicode:

         import json
         json.dumps([unicode(i) for i in range(10)])

result: '["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]'

try send the above data, and tell us the result please. Thanks!

Comments