Python Team Python Team - 4 months ago 33
Python Question

How to filter special characters in django template

I am trying to display ValuesQuerySet list to drop down list in django template page. I jus to filter special characters while displaying in drop down. I tried autoescape syntax but it doesn't work. Is anyother way to do this.

in views.py:

email_accounts = EmailAccount.objects.filter(user__user=self.request.user).values()
form.fields['account'].queryset = email_accounts.values_list('a_email')


Here the value should like [{'a_email': u'xx@gmail.com'}, {'a_email': u'yy@gmail.com'}, {'a_email': u'zzz@gmail.com'}].

In template page

{{ form.account }}


So it displayed like below in drop down list

(u'xx@gmail.com')
(u'yy@gmail.com')
(u'zz@gmail.com')


I need to remove (u') those special chars when displaying in to drop down list. How to do that? any one suggest me.

Answer

You shouldn't be using a ValuesQueryset at all here. The queryset parameter for a ModelChoiceField expects, not surprisingly, a standard queryset.

email_accounts = EmailAccount.objects.filter(user__user=self.request.user)
form.fields['account'].queryset = email_accounts