2013-03-04 55 views
2

我的應用程序有問題。 一個寵物商店的應用程序。Django錯誤forms.FileField()

我創建了2個表單。第一種形式允許用戶創建他們自己的商店並將數據保存到我成功的模型中,第二種形式允許用戶將他們自己的寵物添加到寵物商店。

第一種形式是成功的,因爲它得到了正確驗證,但我的第二種形式沒有成功驗證,因爲在PetForm中,我有一個名爲image = forms.FileField()的字段,用戶可以在其中上傳他們寵物的圖片,驗證失敗,因爲圖片沒有保存在任何地方。

我試圖把爭論變成圖像= forms.FileField(upload_to =「圖片/」) ,但我收到

__init__() got an unexpected keyword argument 'upload_to' 

我閱讀文檔,此刻一個錯誤,它的狀態'當我在表單中使用FileField時,還必須記住將文件數據綁定到表單。'。

我無法理解綁定文件數據。

有人可以幫助我!

我forms.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') 

我的models.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 

這是我的views.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) 
)   

and My fan.html

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

回答

2

因爲您覆蓋了您的寵物模型圖像。刪除表單中的圖像。

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. 

當您上傳圖片不要忘記形式

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

我得告訴你一件事添加「的multipart/form-data的」。我刪除它,但它不會像管理面板那樣保存動物圖片。 – donkeyboy72 2013-03-05 02:16:14

+0

謝謝你幫助我:] – donkeyboy72 2013-03-05 02:18:45

+0

因爲你的表單缺少允許圖像保存的東西:) – catherine 2013-03-05 02:19:29