首页 > 解决方案 > Django 表单总是返回 false:

问题描述

我正在尝试更新条目。但是form.save()返回错误。我无法了解情况。

视图.py

def update(request, id):
customer = Customer.objects.get(id=id)
if request.method == "POST":
    form = customerForm(request.POST, instance=customer)
    if form.is_valid():
        form.save()
        return redirect("/")
    else:
        return render(request, 'index.html')
else:
    form = customerForm()
return render(request, 'edit.html', {'customer': customer})

模型.py

from django.db import models
from django.db import connections

# Create your models here.


class Customer(models.Model):
    # id = models.CharField(max_length=100)
    customer_name = models.CharField(max_length=256)
    phone = models.CharField(max_length=256)
    address = models.CharField(max_length=256)
    city = models.CharField(max_length=256)
    email = models.EmailField()
    product_company = models.CharField(max_length=256)
    product_brand = models.CharField(max_length=256)
    product_model = models.CharField(max_length=256)
    imei = models.CharField(max_length=20)
    reported_issue = models.CharField(max_length=256)
    possible_solution = models.CharField(max_length=256)
    actual_solution = models.CharField(max_length=256)
    estimated_time = models.CharField(max_length=256)
    actual_time = models.CharField(max_length=256)
    current_status = models.CharField(max_length=256)
    additional_notes = models.CharField(max_length=256)
    date_created = models.CharField(max_length=256)
    date_updated = models.CharField(max_length=256)


class Meta:
    db_table = "datagrid"

表格.py

from django import forms
from django.forms import fields, widgets
from customerDetails.models import Customer


class customerForm(forms.ModelForm):
    class Meta:
        model = Customer
        fields = ['id', 'customer_name', 'phone', 'address', 'city', 'email', 'product_company', 'product_brand', 'product_model', 'imei', 'reported_issue',
                  'possible_solution', 'actual_solution', 'estimated_time', 'actual_time', 'current_status', 'additional_notes', 'date_created', 'date_updated']
        widgets = {'customer_name': forms.TextInput(attrs={'class': 'form-control'}),
                   'phone': forms.TextInput(attrs={'class': 'form-control'}),
                   'address': forms.TextInput(attrs={'class': 'form-control'}),
                   'city': forms.TextInput(attrs={'class': 'form-control'}),
                   'email': forms.EmailInput(attrs={'class': 'form-control'}),
                   'product_company': forms.TextInput(attrs={'class': 'form-control'}),
                   'product_brand': forms.TextInput(attrs={'class': 'form-control'}),
                   'product_model': forms.TextInput(attrs={'class': 'form-control'}),
                   'imei': forms.TextInput(attrs={'class': 'form-control'}),
                   'reported_issue': forms.TextInput(attrs={'class': 'form-control'}),
                   'possible_solution': forms.TextInput(attrs={'class': 'form-control'}),
                   'actual_solution': forms.TextInput(attrs={'class': 'form-control'}),
                   'estimated_time': forms.TextInput(attrs={'class': 'form-control'}),
                   'actual_time': forms.TextInput(attrs={'class': 'form-control'}),
                   'current_status': forms.TextInput(attrs={'class': 'form-control'}),
                   'additional_notes': forms.TextInput(attrs={'class': 'form-control'}),
                   'date_created': forms.TextInput(attrs={'class': 'form-control'}),
                   'date_updated': forms.TextInput(attrs={'class': 'form-control'})
                   }

编辑.html

{% extends "base.html" %}

{% block title %}Add New Customer Records{% endblock title %}

{% block content %}
<div class="col-md-12">
    <h4>Update Customer Details</h4>
    <form method="POST" class="post-form" action="/update/{{customer.id}}">
        {% csrf_token %}
        <div class="container">
            <br>
            <div class="form-group row">
                <label class="col-sm-1 col-form-label"></label>
                <div class="col-sm-4">
                    <h3>Update Details</h3>
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Customer Name</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="customer_name" id="id_customer_name" required maxlength="256"
                        value="{{ customer.customer_name}}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Phone</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="phone" id="id_phone" required maxlength="256"
                        value="{{ customer.phone }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Address</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="address" id="id_address" required maxlength="256"
                        value="{{ customer.address }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">City</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="city" id="id_city" required maxlength="256"
                        value="{{ customer.city }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Email</label>
                <div class="col-sm-4">
                    <input type="email" class="form-control" name="email" id="id_email" required maxlength="256"
                        value="{{ customer.email }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Product Company</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="porduct_company" id="id_product_company" required maxlength="256"
                        value="{{ customer.product_company }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Product Brand</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="product_brand" id="id_product_brand" required maxlength="256"
                        value="{{ customer.product_brand }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Product Model</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="product_model" id="id_product_model" required maxlength="256"
                        value="{{ customer.product_model }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">IMEI</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="imei" id="id_imei" required maxlength="256"
                        value="{{ customer.imei }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Reported Issue</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="reported_issue" id="id_reported_issue" required maxlength="256"
                        value="{{ customer.reported_issue }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Possible Solution</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="possible_solution" id="id_possible_solution" required maxlength="256"
                        value="{{ customer.possible_solution }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Actual Solution</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="actual_solution" id="id_actual_solution" required maxlength="256"
                        value="{{ customer.actual_solution }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Estimated Time</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="estimated_time" id="id_estimated_time" required maxlength="256"
                        value="{{ customer.estimated_time }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Actual Time</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="actual_time" id="id_actual_time" required maxlength="256"
                        value="{{ customer.actual_time }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Current Status</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="current_status" id="id_current_status" required maxlength="256"
                        value="{{ customer.current_status }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Additional Notes</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="additional_notes" id="id_additional_notes" required maxlength="256"
                        value="{{ customer.additional_notes }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Date Created</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="date_created" id="id_date_created" required maxlength="256"
                        value="{{ customer.date_created }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-2 col-form-label">Date Updated</label>
                <div class="col-sm-4">
                    <input type="text" class="form-control" name="date_updated" id="id_date_updated" required maxlength="256"
                        value="{{ customer.date_updated }}" />
                </div>
            </div>
            <div class="form-group row">
                <label class="col-sm-1 col-form-label"></label>
                <div class="col-sm-4">
                    <button type="submit" class="btn btn-success btn-lg">Update</button>
                </div>
            </div>
        </div>
    </form>
    {% endblock content %}

标签: pythondjangodjango-modelsdjango-viewsdjango-forms

解决方案


推荐阅读