django - String causes rendering exception with utf-8 defined -
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 am harsh because it will change at any time.
lista_menu = '& lt; Ul class = "menu" & gt; \ N \ & lt; Li & gt; & Lt; A href = "'+ + reverse (' profilelogo ', args = (s_loja,)) +'" & gt; '+ \ Loja.nome.title () +' & lt; / A & gt; & Lt; / Li & gt; \ N & lt; Li & gt; & Lt; A href = "'+ + reverse (' index ', arg = (s_loja,)) + \'" & gt; '+' Pagina Principal '+' & lt; / A & gt; & Lt; / Li & gt; The string 'Pagina Principal' causes the following error: An exception was caught in TemplateSyntaxError rendering: ('ASCI', 'P \ xc3 \ Xa1gina principal ', 1, 2,' sequentially not in bounds (128) ') >
If I define the string as Unicode (Eupagina .. . ') It works fine, but I do not understand why utf-8 - - at the top of my code? Edit: If I have a simple tag that returns a variable with a string ('pajina principals') it works fine. So why is this case different which forces me to define the string as unicode?
Edit 2: Full Tag
@ register.simple_tag def menupainel (s_loja): def listapaginas paginfo_menu): lista_menu = '' for paginfo_menu page: lista_menu + '& Lt; Li & gt; & Lt; A href = "'+ \ reverse (' painel_paginfo ', arg = (pagina.loja, pagina.id)) + \'" & gt; '+ Pagina.titulo.title () +' & lt; / A & gt; & Lt; / Li & gt; \ N 'lista_menu +' '& lt; Li class = "opcoes_objecto" & gt; & Lt; A href = "'+ \ reverse (' editpaginfo ', args = (pg., Loja, pg.iid)) + \'" & gt; '+ Pagina.titulo.title () +' & lt; / A & gt; & Lt; / Li & gt; \ N 'lista_menu +' '& lt; Li class = "opcoes_objecto" & gt; & Lt; A href = "'+ \ reverse (' delpaginfo ', args = (pg., Loja, pg.iid)) + \'" & gt; '+ Pagina.titulo.title () +' & lt; / A & gt; & Lt; / Li & gt; \ N 'Return lista_menu Loja = get_object_or_404 (Loja, Slug = s_loja) menu = loja.menus.all () paginfo_sem_menu = PaginaInfo.objects. Filter (Iowa = Loja). Filter (public = true) .Filter (menu = none) lista_menu = '& lt; Ul class = "menu" & gt; \ N \ & lt; Li & gt; & Lt; A href = "'+ + reverse" (profile_logo, args = (s_loja,)) +' "& gt; + \ Loja.nome.title () + '& lt; / A & gt; & Lt; / Li & gt; \ N & lt; Li & gt; & Lt; A href = "'+ + reverse (' index ', arg = (s_loja,)) + \'" & gt; '+ U'Página Principal' + '& lt; / A & gt; & Lt; / Li & gt; \ N 'For menus in the menu: paginfo_menu = menu.paginasinfo.exclude (slug =' index ') If paginfo_menu: lista_menu + =' & lt; Li id = "title" & gt; '+ \ Menu.nome.title () +' & lt; / Li & gt; \ N '+ \ listapaginas (paginfo_menu) if paginfo_sem_menu: lista_menu + = listapaginas (paginfo_sem_menu) lista_menu + =' & lt; / Ul & gt; Return List_Menu
- itemprop = "text">
This is a python thing Simple String Store 1 character per byte. Just use the Unicode version.
Editing:
In Python 3, the normal string became Unicode but Django does not have 3 back ports yet
EDIT2:.
Just for information coding: At the beginning of the file: utf-8
, simply asks the python parser to work with the utf-8 encoded source code instead of the default ASCII It has nothing to do with the internal representation of the string.
Comments
Post a Comment