Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#19935 closed Bug (invalid)

Unicode chars in verbose_name results in UnicodeDecodeError instead of 404 in generic views

Reported by: boloomka@… 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


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:

Change History (6)

comment:1 Changed 5 years ago by Claude Paroz

Could you please give us the exact Meta definition for your model?

comment:2 Changed 5 years ago by boloomka@…

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"})

    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 Changed 5 years ago by Claude Paroz

Resolution: invalid
Status: newclosed

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 Changed 5 years ago by anonymous

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 Changed 5 years ago by Aymeric Augustin

Please see — this isn't specific to Django.

Or switch to Python 3 right now :)

Version 0, edited 5 years ago by Aymeric Augustin (next)

comment:6 Changed 5 years ago by boloomka@…

I thought that django decondes every string as UTF-8... But switching to py3 seems like best solution. Thanks. And sorry for false alarm.

Note: See TracTickets for help on using tickets.
Back to Top