Django表单没有保存到我的数据库吗?

时间:2018-08-16 03:33:23

标签: django django-forms

我是新手,并且在阅读了django的文档后。我做了表格,但是我的表格没有保存到我的数据库中,也没有显示在我的数据库中。请帮我,我尝试了很多办法,但是仍然不能省。我有为此目的的驱动程序,我想注册他们..

Views.py

def driver_form(request):
    args = {}
    template = Template.objects.get(template_default__exact=1)
    template_page = template.template_alias + str("/rentacar/rentacar_driver_form.html")#sir this ?
    return render(request, template_page, args)




def driver_save(request):
    args = {}
    if request.POST:

        driver_firstname = request.POST.get('driver_firstname')
        driver_lastname = request.POST.get('driver_lastname')

        driver_save_form = DriverForm(request.POST)
        if driver_save_form.is_valid():
            new_driver = driver_save_form.save(commit=False)
            new_driver.driver_firstname = driver_firstname
            new_driver.driver_lastname = driver_lastname
            new_driver.save()
            template = Template.objects.get(template_default__exact=1)
            template_page = template.template_alias + str("/rentacar/rentacar_driver_form.html")
            return render(request, template_page, args)
        else:
            return HttpResponseRedirect('/')
    else:
        return HttpResponseRedirect('/')

rentacar_driver_form

<!DOCTYPE html>
<html>
<head>

<meta charset="utf-8">
<title></title>

</head>
<body>
<div class="container">
<form method="POST" action="/rentacar/driver-save/">
{% csrf_token %}
<label>
  First Name<br>
  <input type="text" name="driver_firstname" required>
</label>
<br>
<label>
  Last Name<br>
  <input type="text" name="driver_lastname" required>
</label>
<br>

<input type="submit" class='btn btn-primary' value="Submit">
</form>
</div>
</body>

</html>

Forms.py

from __future__ import unicode_literals
from django import forms
from rentacar.models import *

class DriverForm(forms.ModelForm):
    class Meta:
        model = BookingApproval
        exclude = (
            'driver_firstname',
            'driver_lastname',
        )

1 个答案:

答案 0 :(得分:1)

您未提供DriverForm的代码。我想这种形式是forms.ModelForm的子类。您的html根本不知道您的DriverForm。您创建了html输入,这些输入未连接到DriverForm。首先,您还应该在views.py中为GET请求实例化表单,将其传递给args并调整收益。

views.py

def driver_save(request):
    args = {}
    if request.POST:

        driver_save_form = DriverForm(request.POST)

        if driver_save_form.is_valid():
            new_driver = driver_save_form.save(commit=False)
            new_driver.save()
            return HttpResponseRedirect('/')
        else:
            return HttpResponseRedirect('/')
    else:
        driver_save_form = DriverForm()
        args['driver_save_form'] = driver_save_form
        template = Template.objects.get(template_default__exact=1)
            template_page = template.template_alias + str("/rentacar/rentacar_driver_form.html")
            return render(request, template_page, args)

比您应该呈现通过args传递给html模板的表单的字段。最简单的方法是使用driver_save_form.as_p将每个字段呈现为段落元素。我相信您可以将表单的action属性留空,因为它无论如何都会发布到相同的URL。

这样,当您访问相关的url时,您的表单将在GET请求中实例化,并且比您提交表单时,它会将POST返回到您的视图,其中POST数据将用于填充DriverForm以及表单将有效,new_driver将保存到数据库。

<!DOCTYPE html>
<html>
  <head>
  <meta charset="utf-8">
  <title></title>
  </head>

  <body>
    <div class="container">
      <form method="POST" action="">
      {% csrf_token %}

      {{ driver_save_form.as_p }}

      <input type="submit" class='btn btn-primary' value="Submit">
      </form>
    </div>
  </body>

</html>

forms.py

class DriverForm(forms.ModelForm):
    class Meta:
        model = BookingApproval
        fields = (
            'driver_firstname',
            'driver_lastname',
        )