django中的“提交”按钮刷新页面而不保存

时间:2019-10-10 08:54:41

标签: html django django-templates

我试图通过表单将数据保存在ORM中,但是当我单击“提交”按钮时,它所做的只是用相同的数据刷新页面。

HTML

{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% crispy form %}
{% load widget_tweaks %}
{% block content %}
  <h2 class="mb-3">Add a Warehouse</h2>
  <div class="row">

    <form method="post" class="col-md-12 proct-form" novalidate>
        {% csrf_token %}
        <div class="form-group col-md-4 mb-0">
        {{ form.owner|as_crispy_field }}
        </div>

        <div class="form-group col-md-4 mb-0">
        {{ form.warehouse_name|as_crispy_field }}
        </div>
        <div class="form-group col-md-4 mb-0">
        {{ form.warehouse_email|as_crispy_field }}
        </div>
        <div class="form-group col-md-4 mb-0">
        {{ form.warehouse_contact|as_crispy_field }}
        </div>
        <div class="form-group col-md-4 mb-0">
        {{ form.warehouse_city|as_crispy_field }}
        </div>
        <div class="form-group col-md-4 mb-0">
        {{ form.warehouse_pincode|as_crispy_field }}
        </div>
        <div class="form-group col-md-4 mb-0">
        {{ form.warehouse_state|as_crispy_field }}
        </div>
        <div class="form-group col-md-4 mb-0">
        {{ form.warehouse_pan|as_crispy_field }}
        </div>
        <div class="form-group col-md-4 mb-0">
        {{ form.warehouse_gst|as_crispy_field }}
        </div>

        <div class="form-group col-md-12 mb-0">
        <button type="submit" class="btn btn-success">Save</button>
        <a href="{% url 'employee:warehouse_table' %}" class="btn btn-outline-secondary" role="button">Nevermind</a>
        </div>
      </form>
    </div>

{% endblock %}

Views.py

@method_decorator([login_required, employee_required], name='dispatch')
class WarehouseFormView(CreateView):
    model = Warehouse
    fields = "__all__"
    template_name = 'packsapp/employee/warehouseForm.html'

    def form_valid (self, form):
        product = form.save(commit=False)
        product.save()
        messages.success(self.request, 'The Warehouse was created with success!')
        return redirect('employee:warehouse_table')

我的views.py是否存在问题,还是需要更改模板? 应用程序中的所有其他表单都以相同的方式创建,并且可以正常工作

0 个答案:

没有答案