开发者

String causes rendering exception with utf-8 defined

开发者 https://www.devze.com 2022-12-13 02:53 出处:网络
One of my template tags should return a list of links; most of the elements get their name from the database with the exception of one, which I\'ll hardcode because it will never change.

One of my template tags should return a list of links; most of the elements get their name from the database with the exception of one, which I'll hardcode because it will never change.

lista_menu = '<ul class="menu">\n\
<li><a href="' + reverse('profileloja', args=(s_loja,)) + '">' + \
loja.nome.title() + '</a></li>\n<li><a href="' + reverse('index', args=(s_loja,)) + \
'">' + 'Página principal' + '</a></li>\n'

The string 'Página principal' causes the following error:

TemplateSyntaxError at /teste/painel/

Caught an exception while rendering: ('ascii', 'P\xc3\xa1gina principal', 1, 2, 'ordinal not in range(128)')

If I define the string as unicode(u'Página...') it works fine, but I don't understand why. Shouldn't django know how to work with this after I define # -- coding: utf-8 -- on the top of my code?

Edit: if I define a simple tag that returns a variable with the same string('Página principal') it works fine. So why is this case different that forces me to define the string as unicode?

Edit2: FULL TAG

@register.simple_tag
def menupainel(s_loja):
    def listapaginas(paginfo_menu):
        lista_menu = ''
        for pagina in paginfo_menu:
            lista_menu += '<li><a href="' + \
                reverse('painel_paginfo', args=(pagina.loja, pagina.id))+ \
                '">' + pagina.titulo.title() + '</a></li>\n'
            lista_menu += '<li class="opcoes_objecto"><a href="' + \
                          reverse('editpaginfo', args=(pagina.loja, pagina.id)) + \
                          '">' + pagina.titulo.title() + '</a></li>\n'
            lista_menu += '<li class="opcoes_objecto"><a href="' + \
                          reverse('delpaginfo', args=(pagina.loja, pagina.id)) + \
                          '">' + pagina.titulo.title() +'</a></li>\n'
            return lista_menu
    loja = get_object_or_404(Loja, slug=s_loja)  
    menus = loja.menus.all()
    paginfo_sem_menu = PaginaInfo.objects.filter(loja=开发者_如何学JAVAloja).\
                       filter(publico=True).filter(menu=None)
    lista_menu = '<ul class="menu">\n\
<li><a href="' + reverse('profileloja', args=(s_loja,)) + '">' + \
loja.nome.title() + '</a></li>\n<li><a href="' + reverse('index', args=(s_loja,)) + \
'">' + u'Página principal' + '</a></li>\n'      
    for menu in menus:
        paginfo_menu = menu.paginasinfo.exclude(slug='index')
        if paginfo_menu:
            lista_menu += '<li id="titulo">' + \
                          menu.nome.title() + '</li>\n' + \
                          listapaginas(paginfo_menu)
    if paginfo_sem_menu:
        lista_menu += listapaginas(paginfo_sem_menu)
    lista_menu += '</ul>'
    return lista_menu


This is a python thing. Ordinary strings store 1 character per byte. Just use unicode version.

Edit:

In Python 3, ordinary strings became unicode. But django isn't ported to 3 yet.

Edit2:

Just for information the coding: utf-8 at the beginning of the file, just tells the python parser to work with utf-8 encoded source code instead of the default ASCII. It has nothing to do with internal representations of strings.


You should use the Unicode string prefix:

u'Página principal'
0

精彩评论

暂无评论...
验证码 换一张
取 消