嗨,我正在创建一个使用模型表单编辑模型的视图。
我的 forms.py 看起来像:
from user_profile.models import Project
from django.forms import ModelForm
def ProjectForm(ModelForm):
class Meta:
model = Project
还有我的views.py:
from django.shortcuts import render_to_response
from django.template import RequestContext
from user_profile.models import Project
from user_profile.forms import ProjectForm
def EditProject(request, project_id=None):
"""
This view allows a user to edit details of a project.
"""
title = u'Unirac - Edit Project'
project = Project.objects.get(id=4)
if request.method == 'POST':
form = ProjectForm(request.POST)
if form.is_valid():
# save form
form.save()
else:
return render_to_response('edit_project.html', {
'title': title,
'form': form,
}, RequestContext(request))
else:
form = ProjectForm(instance=project)
return render_to_response('edit_project.html', {
'title': title,
'form': form,
}, RequestContext(request))
但我得到一个ProjectForm() got an unexpected keyword argument 'instance'
错误?
我不知道为什么,因为您可以将实例传递给模型表单,不是吗?
任何帮助将非常感激。