#19935 closed Bug (invalid)
Unicode chars in verbose_name results in UnicodeDecodeError instead of 404 in generic views
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Generic views | Version: | 1.5-rc-1 |
Severity: | Normal | Keywords: | utf-8 |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
If you name your model in Meta class using verbose_name and you use some non-ASCII character in it, it breaks DetailView for that object. If you call DetailView with existing ID, it works, but when you try to retrieve non-existent object (using non-existent PK), it will return 'UnicodeDecodeError 'ascii' codec can't decode byte 0xc4 in position 0: ordinal not in range(128)' instead of 404. When you append "u" to that string or don't use "verbose_name" at all, it works.
Note that you can use UTF-8 chars in verbose_name_plural and it works.
And yes, I have "# -*- coding: utf-8 -*-" on the first line of that file. I have named everything with utf-8 characters and I don't have problem with anything other.
You can see the traceback here: http://dpaste.com/hold/1006177/
Change History (6)
comment:1 by , 12 years ago
comment:2 by , 12 years ago
Here is the complete definition of my model:
class Article(models.Model): title = models.CharField("nadpis", max_length=255) author = models.ForeignKey(User, verbose_name="autor") date_published = models.DateTimeField("datum", auto_now=True, editable=False) content = models.TextField("obsah") tags = models.ManyToManyField(Tag, blank=True, null=True, verbose_name="tagy") published = models.BooleanField("publikováno", default=False) def __unicode__(self): return self.title def get_absolute_url(self): return reverse("article_detail", kwargs={"pk":self.id}) class Meta: verbose_name = "článek" verbose_name_plural = "články" ordering = ["-date_published", "title"]
I'm subclassing DetailView:
class ArticleDetailView(DetailView): context_object_name = "article" model = Article template_name = "blog/article.html" def get_object(self): article = super(ArticleDetailView, self).get_object() if not article.published and not self.request.user.has_perms('blog.add_article'): raise Http404 return article
but when I try using DetailView itself (DetailView.as_view(model=Article, template_name...) in URLconf), it doesn't work either. When I comment 'verbose_name = "článek"' part in Meta of Article, it works. When it's there, it returns UnicodeError instead of 404. For existing articles it works.
comment:3 by , 12 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
Thanks for the details. If you declare strings containing non-ascii chars on Python 2, you should either declare from __future__ import unicode_literals
or prefix the string with 'u' (u"článek"
).
comment:4 by , 12 years ago
So, every string in django with utf-8 chars should be u"string"? Because I haven't found it in documentation. And I have never used u"string" in anything and everythink worked...
comment:5 by , 12 years ago
Please see http://docs.python.org/2/howto/unicode.html — this isn't specific to Django.
Django's docs also have this page: https://docs.djangoproject.com/en/dev/ref/unicode/
Or switch to Python 3 right now :)
comment:6 by , 12 years ago
I thought that django decondes every string as UTF-8... But switching to py3 seems like best solution. Thanks. And sorry for false alarm.
Could you please give us the exact Meta definition for your model?