when im trying to create an objet 'Comprobante' throws me the error:
KeyError at /caja_chica/comprobantes/crear/
'comprobante'
Request Method: POST
Request URL: http://127.0.0.1:8000/caja_chica/comprobantes/crear/
Django Version: 3.2.5
Exception Type: KeyError
Exception Value:
'comprobante'
here is my model Comprobante:
class Comprobante(models.Model):
TIPO_A = 1
TIPO_B = 2
TIPO_C = 3
TIPO_T = 4
TIPO_X = 5
TIPO_CHOICES = (
(TIPO_A, 'A'),
(TIPO_B, 'B'),
(TIPO_C, 'C'),
(TIPO_T, 'T'),
(TIPO_X, 'X'),
)
caja_chica = models.ForeignKey(CajaChica, on_delete=models.PROTECT)
lote = models.ForeignKey(Lote, on_delete=models.DO_NOTHING, null=True, blank=True, related_name='lote_comprobantes')
numero = models.CharField(max_length=13)
tipo = models.PositiveSmallIntegerField(choices=TIPO_CHOICES, default=TIPO_T)
ente = models.ForeignKey(Ente, on_delete=models.PROTECT)
fecha = models.DateField()
partida_presupuestaria = models.ForeignKey(PartidaPresupuestaria, on_delete=models.PROTECT)
detalle = models.CharField(max_length=100)
importe = models.DecimalField(max_digits=7, decimal_places=2)
history = HistoricalRecords()
def __str__(self):
return self.numero
#def save(self, *args, **kwargs):
# super(Comprobante, self).save(*args, **kwargs)
# self.caja_chica.disponible -= self.importe
# self.caja_chica.save()
def get_absolute_url(self):
"""
Devuelve la url para acceder a una instancia particular de Comprobante.
"""
return reverse('caja_chica:comprobante_update', args=[str(self.id)])
here is my form:
class ComprobanteForm(forms.ModelForm):
class Meta:
model = Comprobante
fields = ('__all__')
my view using CreateView:
class ComprobanteCreate(SuccessMessageMixin, CreateView):
model = Comprobante
fields = '__all__'
success_url = reverse_lazy('caja_chica:comprobante_list')
success_message = "%(comprobante)s fue creado exitosamente"
and my template of comprobante_form:
{% extends "caja_chica/base.html" %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url 'caja_chica:inicio' %}">Inicio</a> >
<a href="{% url 'caja_chica:comprobante_list' %}">Comprobantes</a> >
{% if comprobante.id %} {{ comprobante }} {% else %} Agregar {% endif %}
</div>
{% endblock %}
{% block content %}
<h1>{% if comprobante.id %} Modificar {% else %} Agregar {% endif %} Comprobante </h1>
<form action="" method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Guardar" />
</form>
{% endblock %}
i think is a proble of the view, but i don´t understand the problem and i don´t know where it is
I tried passing a context by a function in the view like this:
def get_context_data(self, **kwargs):
context = super().get_context_data( **kwargs)
context['comprobante'] = None
print(context)
return context
The success_message
does not interpolate the context, but the cleaned_data
of the form, so in this case all the fields of the model.
You thus can work with:
class ComprobanteCreate(SuccessMessageMixin, CreateView):
model = Comprobante
fields = '__all__'
success_url = reverse_lazy('caja_chica:comprobante_list')
success_message = '%(numero)s fue creado exitosamente'