2

アプリに問題があります。ペットショップアプリ。

2つのフォームを作成しました。最初のフォームでは、ユーザーが独自のストアを作成し、データをモデルに保存できます。2番目のフォームでは、ユーザーが自分のペットをペットショップに追加できます。

最初のフォームは正しく検証されたため成功しましたが、2番目のフォームは検証に成功しませんでした。PetFormには、ユーザーがペットの写真をアップロードできるimage = forms.FileField()というフィールドがあり、検証が失敗したためです。写真はどこにも保存されません。

image = forms.FileField(upload_to = "images /")に引数を入れようとしましたが、エラーが発生しました

__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)
)         

と私のfan.html

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

1 に答える 1

3

ペットモデルの画像を上書きするためです。フォームの画像を削除します。

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>
于 2013-03-05T00:49:10.130 に答える