2
您好我正在創建一個使用模型表單編輯模型的視圖。Django意外的關鍵字參數「實例」
我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'
錯誤?
我不明白爲什麼,因爲你可以將實例傳遞給模型表單,不是嗎?
任何幫助將不勝感激。
哪個版本的django還是你用的? – dm03514
使用Django 1.4 –