I am creating a view based on Classes with django within it there is the message option, it is here where it contains an error when recording the data it generates:
UnicodeDecodeError at /clientes/administrar_clientes
'ascii' codec can't decode byte 0xc3 in position 23: ordinal not in range(128)
The code I use is:
class CrearCliente(SuccessMessageMixin, CreateView):
model = Cliente
form_class = ClienteForm
template_name = "administrador/crear_cliente.html"
success_url = reverse_lazy("clientes:administrar_clientes")
success_message = "%(nombres)s creado con éxito."
def get_context_data(self, **kwargs):
context = super(CrearCliente, self).get_context_data(**kwargs)
codigo_definida = settings.CODIGO_INICIAL_AUTOMATICO
try:
cliente = Cliente.objects.all().latest('id')
codigo_existente = int(cliente.codigo)
codigo_nuevo = codigo_existente + 1
except Cliente.DoesNotExist:
codigo_nuevo = codigo_definida
context['secuencia'] = str(codigo_nuevo).zfill(7)
return context
As I notice the error is because within the message it has a tilde and this causes the error, also within views.py I have added the utf encoding:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
How can I correct this error try the following:
success_message = unicode("%(nombres)s creado con éxito.")
But I have the same error, thanks in advance.
This is more of a Python bug than a Django bug. You have to decode the text:
Use
decode
for this:So in your code you can test by changing this line: