为什么我无法将记录保存到数据库?

时间:2019-06-17 15:58:36

标签: python django

我有一个过去可以很好地工作的表格,但是在我更改了模型中的某些数据并添加了图像字段之后,它是行不通的。

从管理员我可以添加新记录,但是我认为表单ID有问题。

当我在Google Chrome浏览器中检查管理页面时,表单ID为porumbei_form     并且当我检查模板时,表单ID没有显示。

#My view 
@login_required(login_url='/login/')
def porumbelnou(request):
    if request.method == "POST":
        form = AdaugaPorumbel(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/porumbei/')
    else:
        form = AdaugaPorumbel()
    context = {
        'form': form,
    }
    template = loader.get_template("adaugare_porumbel.html")
    return HttpResponse(template.render(context, request))

#My form
class AdaugaPorumbel(forms.ModelForm):
    class Meta:
    model = Porumbei
    fields = ['data_adaugare', 'serie_inel', 'anul', 'culoare', 'crescator', 'culoare_ochi', 'sex', 'ecloziune',
              'rasa', 'linie', 'nume', 'tata',
              'mama', 'compartiment', 'status', 'data', 'vaccinat', 'info', 'imagine', 'imagine_ochi']
    widgets = {
        'ecloziune': forms.DateInput(format='%d/%m/%Y',
                                     attrs={'class': 'form-control', 'type': 'date'}),
        'data': forms.DateInput(format='%d/%m/%Y',
                                attrs={'class': 'form-control', 'type': 'date'}),
        'vaccinat': forms.DateInput(format='%d/%m/%Y',
                                    attrs={'class': 'form-control', 'type': 'date'}),

        'info': forms.Textarea(attrs={'class': 'form-control mt-15', 'rows': '3',
                                      'placeholder': 'Vor apărea în pedigree'}),
    }

#My model

class Porumbei(models.Model):
    id_porumbel = models.AutoField(primary_key=True)
    data_adaugare = models.DateTimeField(default=datetime.now())
    crescator = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    serie_inel = models.CharField(max_length=25, null=False, blank=False, unique=True)
    anul = models.CharField(max_length=4, null=False, blank=False)
    culoare = models.ForeignKey(CuloriPorumbei, on_delete=models.CASCADE, null=False, blank=False, )
    culoare_ochi = models.ForeignKey(CuloriOchi, on_delete=models.CASCADE, null=False, blank=False,)
    sex = models.ForeignKey(Gender, on_delete=models.CASCADE)
    ecloziune = models.DateField(null=True, blank=True)
    rasa = models.CharField(max_length=50, null=True, blank=True)
    linie = models.CharField(max_length=50, null=True, blank=True)
    nume = models.CharField(max_length=50, null=True, blank=True)
    tata = models.CharField(max_length=25, null=True, blank=True)
    mama = models.CharField(max_length=25, null=True, blank=True)
    compartiment = models.ForeignKey(Compartimente, on_delete=models.CASCADE, null=False, blank=False)
    status = models.ForeignKey(StatusPorumbei, on_delete=models.CASCADE, null=False, blank=False)
    data = models.DateField(null=True, blank=True)
    vaccinat = models.DateField(null=True, blank=True)
    info = models.CharField(max_length=100, null=True, blank=True)
    imagine = models.ImageField(null=True, blank=True, upload_to='imagini')
    imagine_ochi = models.ImageField(null=True, blank=True, upload_to='imagini/') 

    class Meta:
        verbose_name = "Porumbel"
        verbose_name_plural = "Porumbei"
        ordering = ['-anul']

    def __str__(self):
        return self.serie_inel

#My template
<form method="post" enctype="multipart/form-data" action="">{% csrf_token %}
<div class="form-group row">
    <label for="id_serie_inel" class="col-sm-2 col-form-label">Serie inel</label>
    <div class="col-sm-4">
        {{ form.serie_inel|add_class:"form-control" }}
    </div>
    <label for="id_anul" class="col-sm-2 col-form-label">Anul</label>
    <div class="col-sm-4">
        {{ form.anul|add_class:"form-control" }}
</div>
</div>

<button type="submit" class="btn btn-success btn-wth-icon icon-wthot-bg btn-block btn-lg">
    <span class="icon-label">
        <i class="fa fa-save"></i>
    </span>
    <span class="btn-text">Salvează</span>
</button>

0 个答案:

没有答案