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=loja).\
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