【问题标题】:Django error in forms.FileField()forms.FileField() 中的 Django 错误
【发布时间】:2013-03-04 22:06:44
【问题描述】:

我的应用有问题。 宠物商店应用。

我创建了 2 个表单。第一种形式允许用户创建自己的商店并将数据保存到我成功创建的模型中,第二种形式允许用户将自己的宠物添加到宠物商店。

第一个表单成功了,因为它得到了正确验证,但我的第二个表单没有成功验证,因为在 PetForm 中,我有一个名为 image = forms.FileField() 的字段,用户可以在其中上传他们宠物的图片和验证失败,因为图片没有保存在任何地方。

我尝试将参数放入 image = forms.FileField(upload_to="images/") 但我收到一个错误

__init__() got an unexpected keyword argument 'upload_to'

我现在正在阅读文档,上面写着“当您在表单中使用 FileField 时,您还必须记住将文件数据绑定到表单。”。

我无法理解绑定文件数据。

谁能帮帮我!

我的表单.py

from django import forms
from pet.models import Store
from pet.models import Pet

class StoreForm(forms.ModelForm):
    name = forms.CharField(max_length =20,widget =forms.Textarea)
    number = forms.CharField(max_length =20,widget =forms.Textarea)
    address = forms.CharField(max_length = 20,widget = forms.Textarea)

    class Meta: 
        model = Store
        fields = ('name','number','address')

class PetForm(forms.ModelForm):
    animal =forms.CharField(max_length = 20,widget = forms.Textarea)
    description =forms.CharField(max_length =20, widget = forms.Textarea)
    owner = forms.ModelChoiceField(queryset=Store.objects.all())
    image = forms.FileField()

    class Meta:
        model = Pet
        fields = ('animal','description','owner','image')

我的模型.py

from django.db import models

class Store(models.Model):
    name = models.CharField(max_length = 20)
    number = models.BigIntegerField()
    address =models.CharField(max_length = 20)
    def __unicode__(self):
        return self.name

class Pet(models.Model):
    animal = models.CharField(max_length =20)
    description = models.TextField()
    owner = models.ForeignKey(Store)
    image = models.FileField(upload_to="images/",blank=True,null=True)

    def __unicode__(self):
        return self.animal

这是我的观点的一部分。py

从 django.core.files.uploadedfile 导入导入 SimpleUploadedFile

def fan(request): # this is the function that saves my form into my models.
    form = PetForm(request.POST or None)
    if form.is_valid():
        dad = form.save(commit=False)
        dad.save()
        if 'cat' in request.POST:
            cat = request.POST['next']
        else:
            cat = reverse('world:index')
        return HttpResponseRedirect(cat)
    return render_to_response(
        'fan.html',
        {'form':PetForm()},
        context_instance = RequestContext(request)
)         

还有我的fan.html

<form method="POST" "action">{% csrf_token %}
    <ul>
        {{ form.as_ul }}
    </ul>
    <input type = "submit" value= "Add Pets to Store" />
</form>

【问题讨论】:

    标签: django


    【解决方案1】:

    因为您覆盖了您的宠物模型图像。删除表单中的图像。

    class PetForm(forms.ModelForm):
        animal =forms.CharField(max_length = 20,widget = forms.Textarea)
        description =forms.CharField(max_length =20, widget = forms.Textarea)
        owner = forms.ModelChoiceField(queryset=Store.objects.all())
    
        class Meta:
            model = Pet
            fields = ('animal','description','owner','image')
    
    //It's not necessary to defined again model field in the form. Once you call the model 
    //in the form it's understood what you want to show. You can only defined the model 
    //field again if you want something to add or embed in that field. Like for example you
    //want to change the charfield in to textarea or you defined a queryset like you did 
    //above. Erase your max_length because you are already defined that in the model.
    

    上传图片时不要忘记在表单中添加“multipart/form-data”

    <form method="POST" enctype="multipart/form-data" "action" >
        {% csrf_token %}
        <ul>
            {{ form.as_ul }}
        </ul>
        <input type = "submit" value= "Add Pets to Store" />
    </form>
    

    【讨论】:

    • 我要告诉你一件事。我删除了它,但它不会像管理面板那样保存动物的图片。
    • 因为您的表单缺少允许保存图像的功能:)
    • 我把它修好了。谢谢好友
    • 如果你有空,你能帮我解答一个问题吗? :] 如果你不忙。 :]
    猜你喜欢
    • 2012-04-20
    • 2011-05-20
    • 2012-03-20
    • 2021-12-19
    • 2011-02-12
    • 1970-01-01
    • 2013-07-09
    • 2017-07-25
    • 1970-01-01
    相关资源
    最近更新 更多