python - 使用 django 将值保存在数据库中

标签 python database django error-handling

我有一个自定义表单,每当我获取要保存在数据库中的表单值时,它都会显示错误(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/

相关文章:

python - 投票系统和关注功能的 Django 模型

python - Pandas:根据包含特定值的字符串有效地子集 DataFrame

python - 如何将 Django 过滤器参数作为参数传递给其他函数

java - 类结构应该匹配 OOP 中的表结构吗?

后台 worker 中的.net数据读取器

sql - 在表达式上使用 LIKE 从表中选择加密项目

python - 我们如何在 django-hstore 中查询嵌套字典?

django - 如何在 django 管理界面中添加带有超链接的自定义列?

python - 将 Matlab 代码翻译为 Python

python - Pandas 基于索引/列组合合并 DataFrame