我有一个自定义表单,每当我获取要保存在数据库中的表单值时,它都会显示错误(applicationform() 收到意外的关键字参数“job_title”)并且这些值不会保存在表中。
views.py:-
def applicationvalue(request):
if request.method == 'POST':
getjobtitle = request.POST['jobtitle']
getintable = applicationform(job_title=getjobtitle)
getintable.save()
print getjobtitle
return HttpResponse(getintable)
else:
return render_to_response('registration/applicationform.html')
我的表格是:-
<form method="POST" action="#" class="form-horizontal" id="applicationform" name="appform">
<input type="text" id="u_jobtitle" class="input-xlarge" name="jobtitle" value=" " />
<button class="btn btn-gebo" type="submit" name="usubmit">Save changes</button>
每当我从表单中获取值以将值保存在表字段“job_title ”中时,它都会显示错误:-
applicationform() 收到意外的关键字参数“job_title”
最佳答案
将 html 中的 input
字段名称更改为 job_title
<input name="job_title" type="text" id="u_jobtitle" class="input-xlarge" value=" " />
-------------^ changed
然后在 View 中执行
def applicationvalue(request):
if request.method == 'POST':
#Dont need this
#getjobtitle = request.POST['jobtitle']
#---------------------------Use request.POST
getintable = applicationform(request.POST)
getintable.save()
print getjobtitle
return HttpResponse(getintable)
else:
return render_to_response('registration/applicationform.html')
如果您使用相同的表单来渲染 html 而不是手动编码,效果会更好。
关于python - 使用 django 将值保存在数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18437053/