我必须做这样的事情:
def createpost(request):
if request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid():
titlee = form.cleaned_data['title']
contentt = form.cleaned_data['content']
post = Post(title=titlee,content=contentt,created='2012-01-06',modified='2012-01-06')
post.save()
return render_to_response('createpost.html', {"form": form}, context_instance=RequestContext(request))
有没有办法获得像这样的表格价值post = Post(form) ; post.save()
我不想像这样单独获得价值:titlee = form.cleaned_data['title']
有什么想法吗?
答案 0 :(得分:5)
答案 1 :(得分:1)
我建议你使用模型表格。要阅读相关内容,请转到此处:https://docs.djangoproject.com/en/dev/topics/forms/modelforms/
创建ModelForm后,您的函数应该类似于:
def createpost(request):
if request.method == 'POST':
form = PostForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('/redirectsomewhereelse/')
else:
form = PostForm()
return render_to_response('createpost.html', {"form": form}, context_instance=RequestContext(request))
答案 2 :(得分:0)
如果所有数据都有效,您可以调用form.save()方法。