2

为什么我的表格没有填写模型中的数据?

这是我的模型.py

class People(models.Model):
    user = models.OneToOneField(User)
    name = models.CharField(max_length=100)
    address = models.CharField(max_length=255)

这是我的forms.py

from django.forms import ModelForm 

class EditForm(ModelForm):
    class Meta:
        model = People
        exclude=('user',)

视图.py

def edit_data(request):
    user = request.user
    people = People.objects.get(user=user)
    form = EditForm(request.POST, instance = people)
    if request.method == 'POST':
        if form.is_valid():
            form.save()
        else:
            print 'Error'
    else:
        form = EditForm()

    return render_to_response('profile.html',{'form':form}, context_instance=RequestContext(request))

profile.html

<form action="/profile/" method="post">{% csrf_token %}
{{ form.as_p }}
</form>
4

1 回答 1

2

问题是你form在你的else子句中重新定义(到你的一个新实例EditForm,它没有instance变量集)。删除else(和它下面的线),你应该很高兴。

于 2012-10-15T15:57:07.387 回答