首页 > 解决方案 > Django 模型表单不保存 || 而是写入地址栏

问题描述

我目前有一个需要提交到数据库的客户详细信息表单。

但是,当我提交表单时,它不会像代码建议的那样保存或重定向。而是将所有字段写入页面的 URL 栏中。

模型.py:

from django.db import models

class newCustomerClass(models.Model):
    customerName = models.CharField("Customer Name",max_length=50 , blank=True)
    addressStreetNo = models.CharField(max_length=50 , blank=True)
    addressStreet = models.CharField(max_length=50 , blank=True)
    addressSuburb = models.CharField(max_length=50, blank=True )
    addressCity = models.CharField(max_length=50, blank=True )
    addressPO = models.CharField(max_length=50 , blank=True)

    contact = models.CharField(max_length=50, blank=True )
    mail = models.CharField(max_length=50, blank=True )
    CellNo = models.CharField(max_length=50, blank=True )
    

表格.py:

from django import forms
from .models import newCustomerClass
from django.forms import ModelForm

class newCustomerForm(ModelForm):
    class Meta:
        model = newCustomerClass
        fields = 'customerName', 'addressStreetNo' ,'addressStreet', 'addressSuburb' ,'addressCity' , 'addressPO'  , 'contact' , 'mail' , 'CellNo'

视图.py:

def newCustomer(request):
    form = newCustomerForm()

    if request.method == 'POST':
        form = newCustomerForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('home')
        else:
            print(form.errors)

    content = {'form':form}
    return render(request, 'main/newCustomer.html' , content)

模板.html:

{% extends "main/base.html"%}

{% block content %}
<br>
<div class="container" style="text-align: center">
    <form>
        {% csrf_token %}

          {{ form.non_field_errors }}

          {{ form.source.errors }}
          {{ form.source }}
        <h1 class="h1">New Customer</h1>

        <br>

        <h5>Customer Name {{ form.customerName }}</h5>

        <br>

        <h5>Street No {{ form.addressStreetNo }}</h5>
        <h5>Street Name {{ form.addressStreet }}</h5>
        <h5>Suburb {{ form.addressSuburb }}</h5>
        <h5>City {{ form.addressCity }}</h5>

        <br>

        <h5>Contact {{ form.contact }}</h5>
        <h5>E-Mail {{ form.mail }}</h5>
        <h5>Cell {{ form.CellNo }}</h5>

        <br>

        <input type="submit" class="btn btn-success">
    </form>
</div>
{% endblock %}

有谁知道是什么原因造成的,我已经尝试过缺少一些字段,并且所有字段都已满,所以我认为表格也无效

标签: pythondjangodjango-modelsdjango-forms

解决方案


问题是您没有进行任何发布请求
更改此

<form>
        {% csrf_token %}

          {{ form.non_field_errors }}

          {{ form.source.errors }}
          {{ form.source }}
        <h1 class="h1">New Customer</h1>

        <br>

        <h5>Customer Name {{ form.customerName }}</h5>

        <br>

        <h5>Street No {{ form.addressStreetNo }}</h5>
        <h5>Street Name {{ form.addressStreet }}</h5>
        <h5>Suburb {{ form.addressSuburb }}</h5>
        <h5>City {{ form.addressCity }}</h5>

        <br>

        <h5>Contact {{ form.contact }}</h5>
        <h5>E-Mail {{ form.mail }}</h5>
        <h5>Cell {{ form.CellNo }}</h5>

        <br>

        <input type="submit" class="btn btn-success">
    </form>

<form method='post' action='the-url-of-newCustomer'>
        {% csrf_token %}

          {{ form.non_field_errors }}

          {{ form.source.errors }}
          {{ form.source }}
        <h1 class="h1">New Customer</h1>

        <br>

        <h5>Customer Name {{ form.customerName }}</h5>

        <br>

        <h5>Street No {{ form.addressStreetNo }}</h5>
        <h5>Street Name {{ form.addressStreet }}</h5>
        <h5>Suburb {{ form.addressSuburb }}</h5>
        <h5>City {{ form.addressCity }}</h5>

        <br>

        <h5>Contact {{ form.contact }}</h5>
        <h5>E-Mail {{ form.mail }}</h5>
        <h5>Cell {{ form.CellNo }}</h5>

        <br>

        <input type="submit" class="btn btn-success">
    </form>

您应该将 url 替换为'the-url-of-newCustomer'您的实际 url,默认情况下您可以将其设为空(这意味着该操作将是当前 url)。


推荐阅读