【问题标题】:save values in database using django使用 django 将值保存在数据库中
【发布时间】:2013-08-26 05:12:19
【问题描述】:

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

【问题讨论】:

    标签: python database django error-handling


    【解决方案1】:

    在您的 html 中将 input 字段名称更改为 job_title

    <input name="job_title" type="text" id="u_jobtitle" class="input-xlarge" value=" " />
    -------------^ changed 
    

    然后在视图中做

    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而不是手动编码会更好。

    【讨论】:

      【解决方案2】:

      applicationform 构造函数应该将request.POST 作为参数。 但在我看来,您没有以“正确”的方式使用 django 表单。我认为您的观点不遵循 django 使用表单的哲学。

      在你的情况下,你应该有一个模型:

      from django.db import models
      
      class Application(models.Model):
          job_title = models.CharField(max_length=100)
      

      基于这个模型,你可以声明一个ModelForm:

      from django import forms
      from .models import ApplicationModel
      
      class ApplicationForm(forms.ModelForm):
      
          class Meta:
              model = ApplicationModel
              fields = ('job_title',)
      

      那么你就可以在你的视图中使用这个表单了

      def applicationvalue(request):
          if request.method == 'POST':
      
              form = ApplicationForm(request.POST)
              if form.is_valid():
                  #This is called when the form fields are ok and we can create the object
                  application_object = form.save()
      
                  return HttpResponse("Some HTML code") # or HttResponseRedirect("/any_url")
      
          else:
              form = ApplicationForm() 
      
          #This called when we need to display the form: get or error in form fields
          return render_to_response('registration/applicationform.html', {'form': form})
      

      最后,您应该有一个 registration/applicationform.html 模板,其内容类似于:

      {% extends "base.html" %}
      
      {% block content %}
      <form action="" method="post">{% csrf_token %}
         <table>
            {{form.as_table}}
         </table>
         <input type="submit" value="Add">
      </form>
      {% endblock %}
      

      希望对你有帮助

      【讨论】:

      • 您对@Ranjeet 和 Rohan 使用表单的方式非常正确。
      猜你喜欢
      • 2017-12-01
      • 2013-02-28
      • 2015-06-21
      • 1970-01-01
      • 2017-05-13
      • 2021-08-25
      • 1970-01-01
      • 2016-07-07
      • 2015-12-10
      相关资源
      最近更新 更多