开发者

Django ModelForms: Trying to save a form with a foreign key

开发者 https://www.devze.com 2023-03-31 05:46 出处:网络
Django will just go to the else condition. here\'s the code: models.py class StakeholderProfile(models.Model):

Django will just go to the else condition.

here's the code:

models.py

class StakeholderProfile(models.Model):
    types = models.ForeignKey(Stakeholder)
    name = models.CharField(blank=False, max_lengt开发者_Python百科h=50)

forms.py

class SPForm(forms.ModelForm):
    class Meta:
        model = StakeholderProfile
        exclude = ('key_contact_person',)

views.py

def profile(request):
    stakeholderprofile = StakeholderProfile.objects.all()

    if  request.method == 'POST':
        form = SPForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/profile/')
    else:
        form = SPForm()
    return render_to_response('profile.html',{'form':form,'sp':stakeholderprofile})

I really need your help sir/maam.


You are excluding a field that doesn't exist in StakeHolderProfile. Also be sure you added method='POST' in your form tag.

0

精彩评论

暂无评论...
验证码 换一张
取 消