Django ModelForm is_valid method always false

hiz5n14c  于 2023-05-19  发布在  Go
关注(0)|答案(1)|浏览(127)

我在使用Django ModelForm时遇到一个错误。方法form.is_valid总是false,我不明白为什么。在把所有的值放到表单中之后,它不会进入方法内部,然后当我按下提交按钮时什么也没有发生。
这是我的代码。

**models.py www.example.com

class Product(models.Model):
    image= models.ImageField(blank=True, null=True)
    name= models.CharField(max_length=200)
    type=models.CharField(max_length=30)
    product_code=models.CharField(max_length=20, unique=True)
    productor= models.CharField(max_length=50) 
    color= models.CharField(max_length=40)
    size=models.CharField(max_length=100)
    weight=models.FloatField(default=0)
    full_price= models.FloatField(default=0)
    discount=models.PositiveIntegerField(default=0)
    final_price=models.FloatField(default=0)
    quantity= models.PositiveIntegerField(default=0)
    supplier= models.ForeignKey(User,on_delete=models.CASCADE) #Utente staff che ha inserito il prodotto
    recensioni = models.ManyToManyField(Recensione)
    
    def __str__(self):
        return self.nameclass   
                                                                                      
Computer(Product):
    display_size= models.FloatField(default=0)
    display_resolution= models.CharField(max_length=20)
    cpu= models.CharField(max_length=50)
    ram= models.PositiveIntegerField(default=0)
    disk_size= models.FloatField(default=0)
    disk_type= models.CharField(max_length=20) 
    operating_system= models.CharField(max_length=100)
    graphic_card= models.CharField(max_length=50) 
    battery_autonomy= models.FloatField(default=0)
    additional_function= models.CharField(max_length=1000)

**forms.py www.example.com

class ComputerForm(forms.ModelForm):

    class Meta:
        model=Computer
        fields=['image','name','product_code','productor','color','size','weight','full_price','discount','quantity',
                'display_size',
                'display_resolution',
                'cpu',
                'ram',
                'disk_size',
                'operating_system',
                'graphic_card',
                'battery_autonomy',
                'additional_function']

**views.py www.example.com

#Aggiunta nuovo Prodotto
@login_required
def add_product(request,category):
    templ="products/product_form.html"
    
    form=ComputerForm()
    if request.method == "POST":
        form=ComputerForm(request.POST, request.FILES )

        #Controllo campi form siano validi
        if form.is_valid():
            print("Valid")
            form.save()

            return redirect("products:all_products")

    return render(request,template_name=templ,context={"form":form})

产品表单. html

<div class="col-4" style= "width:30%;right:+20px;">
                <h3> <b> Dati prodotto: </b> </h3>
                
                <form method='POST'>
                    {% csrf_token %}
                    {% crispy form %}
                    <br>
                    <input type="submit" class="btn btn primary" style="background-color:blue;color:white;" value="Aggiungi Prodotto">
                </form>

            </div>
tzcvj98z

tzcvj98z1#

主要的问题应该是crispy表单生成了一个新的for标记,该标记默认为GET请求。根据官方文档制作一个表单助手对象。确保包含self.helper.form_tag = False。这应该能解决你的问题。以下是文档:https://django-crispy-forms.readthedocs.io/en/latest/form_helper.html

**注意:**如果您的表单中有文件或图片上传,您应该像这样在HTML表单标签中添加enctype="multipart/form-data"

<form method='POST' enctype="multipart/form-data">
    {% csrf_token %}
    {% crispy form %}
    <br>
    <input type="submit" class="btn btn primary" style="background-color:blue;color:white;" value="Aggiungi Prodotto">
</form>

这是必要的,如果你想让用户上传文件,如图像和形式,我假设你这样做。

相关问题