海。我想为我的数据库修改一条记录。我有:
模型.py
class People(models.Model):
first_name = models.CharField(max_length = 50)
last_name = models.CharField(max_length = 50)
email = models.EmailField(blank = True)
grade = models.CharField(max_length = 2)
def __unicode__(self):
return '%s %s' % (self.first_name, self.last_name)
class PeopleForm(ModelForm):
class Meta:
model = People
fields = ['first_name', 'last_name', 'email', 'grade']
视图.py
def modify(request, person_pk):
title = 'Creating new program'
template = 'modify.html'
data = People.objects.get(pk = person_pk)
form = PeopleForm(instance=data)
context = {'form': form}
if request.POST:
form = PeopleForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('/peoples/'))
else:
return render_to_response(template, context, context_instance=RequestContext(request))
return render_to_response(template, context, context_instance=RequestContext(request))
网址.py
url(r'^modify/(?P<person_pk>.*)$', views.modify, name='modify'),
表格.py
class PeopleForm(forms.ModelForm):
class Meta:
model = People
fields = ('first_name', 'last_name', 'email', 'grade')
和模板
<form action="/peoples/" method="post">{% csrf_token %}
<ul>
{{form.as_ul}}
</ul>
<input type="submit" name="submit" value="Modify">
</form>
我不知道为什么当我单击修改按钮时,我完成了字段,当我按下按钮到上一页时,数据库中的输入字段没有更新。你有什么建议吗?