我的所有CreateView都运行正常,但这不是(虽然这是UpdateView)。检查后,试图找出差异,我找不到。必须是我仍然缺少的东西。
模板:
<form method="POST">{% csrf_token %}
{{ form.non_field_errors }}
{{ form.errors }}
{{ form.make }}
{{ form.make.errors }}
{{ form.model}}
{{ form.model.errors }}
{{ form.year}}
{{ form.year.errors }}
<button type="submit"> Save</button>
<a href="javascript:close_window();"> Close</a>
</form>
URLS.py中的Url paterns:
url(r'^update/(?P<pk>\w+)/$',UpdateView.as_view(
model=Vehicles,
form_class=VehiclesForm,
success_url="/main/",
template_name="vehicle_detail.html")),
型号:
from django.db import models
class Vehicles(models.Model):
stock = models.CharField(max_length=10, blank=False, db_index=True)
vin = models.CharField(max_length=17, blank=False, db_index=True)
vinlast8 = models.CharField(max_length=8, blank=False, db_index=True)
make = models.CharField(max_length=15, blank=False)
model = models.CharField(max_length=15, blank=False)
year = models.CharField(max_length=4, blank=False)
deal = models.IntegerField(blank=False, db_index=True, null=True)
sold = models.DateField(blank=True, null=True, db_index=True)
origin = models.ForeignKey('origins.Origins', db_column='origin')
#origin = models.CharField(max_length=10, blank=True)
bank = models.ForeignKey('banks.Banks', db_column='bank', null=True)
vehtype = models.ForeignKey('vehtypes.Vehtypes', db_column='vehtype', verbose_name='Veh Type')
status = models.ForeignKey('status.Status', db_column='status')
imported = models.DateField()
clerk = models.CharField(max_length=10, blank=False, db_index=True, verbose_name='Clerk')
completed = models.DateField(blank=True, null=True)
registry = models.IntegerField(blank=True, verbose_name='Reg #', null=True)
plate = models.CharField(blank=True, null=True, max_length=10)
tagno = models.IntegerField(blank=True, null=True, verbose_name='Tag #')
tag_exp = models.DateField(blank=True, null=True, verbose_name='Tag Exp')
unit_linked = models.IntegerField(blank=False, verbose_name='Link')
salesperson = models.CharField(max_length=20, blank=False, verbose_name='S/person', null=True)
agent = models.ForeignKey('agents.Agents', db_column='agent', blank=True, null=True)
tradein1 = models.CharField(max_length=10, blank=True, null=True, verbose_name='stock')
tr1vin = models.CharField(max_length=17, blank=True, db_index=True, null=True, verbose_name='vin')
tr1make = models.CharField(max_length=15, blank=True, null=True, verbose_name='make')
tr1model = models.CharField(max_length=15, blank=True, null=True, verbose_name='model')
tr1year = models.CharField(max_length=4, blank=True, null=True, verbose_name='year')
tradein2 = models.CharField(max_length=10, blank=True, null=True, verbose_name='stock')
tr2vin = models.CharField(max_length=17, blank=True, db_index=True, null=True,verbose_name='vin')
tr2make = models.CharField(max_length=15, blank=True, null=True,verbose_name='make')
tr2model = models.CharField(max_length=15, blank=True, null=True,verbose_name='model')
tr2year = models.CharField(max_length=4, blank=True, null=True,verbose_name='year')
lhtrade1 = models.CharField(max_length=15, blank=True, null=True, verbose_name='L/holder')
lh1docreq = models.DateField(blank=True, null=True, verbose_name='D/Requested')
lh1docrec = models.DateField(blank=True, null=True, verbose_name='D/Received')
lhtrade2 = models.CharField(max_length=15, blank=True, null=True, verbose_name='L/holder')
lh2docreq = models.DateField(blank=True, null=True, verbose_name='D/Requested')
lh2docrec = models.DateField(blank=True, null=True, verbose_name='D/Received')
cust1name = models.CharField(max_length=40, blank=True, db_index=True, null=True,
verbose_name='name')
cust1lic = models.CharField(max_length=20, blank=True, db_index=True, null=True,
verbose_name='license')
cust1addr = models.CharField(max_length=40, blank=True, null=True, verbose_name='address')
cust1city = models.CharField(max_length=15, blank=True, null=True, verbose_name='city')
cust1state = models.CharField(max_length=2, blank=True, null=True, verbose_name='state')
cust1zip = models.CharField(max_length=10, blank=True, null=True, verbose_name='zipcode')
cust1email = models.EmailField(blank=True, null=True, verbose_name='email')
cust1tel1 = models.CharField(max_length=15, blank=True, verbose_name='Tel. 1', null=True)
cust1tel2 = models.CharField(max_length=15, blank=True, verbose_name='Tel. 2', null=True)
cust1ssn = models.CharField(max_length=12, blank=True, db_index=True, null=True,verbose_name='SSN')
cust2name = models.CharField(max_length=30, blank=True, db_index=True, null=True,
verbose_name='name')
cust2lic = models.CharField(max_length=20, blank=True, db_index=True, null=True,
verbose_name='license')
cust2addr = models.CharField(max_length=25, blank=True, null=True, verbose_name='address')
cust2city = models.CharField(max_length=15, blank=True, null=True, verbose_name='city')
cust2state = models.CharField(max_length=2, blank=True, null=True, verbose_name='state')
cust2zip = models.CharField(max_length=10, blank=True, null=True, verbose_name='zipcode')
cust2email = models.EmailField(blank=True, null=True, verbose_name='email')
cust2tel1 = models.CharField(max_length=15, blank=True, verbose_name='Tel. 1', null=True)
cust2tel2 = models.CharField(max_length=15, blank=True, verbose_name='Tel. 2', null=True)
cust2ssn = models.CharField(max_length=12, blank=True, db_index=True, null=True,verbose_name='SSN')
class Meta:
db_table = 'vehicles'
def __unicode__(self):
return self.stock
def save(self, *args, **kwargs):
self.stock = self.stock.upper()
return super(Vehicles, self).save(*args, **kwargs)
形式:
from django import forms
from models import Vehicles
class VehiclesForm(forms.ModelForm):
class Meta:
model = Vehicles
这是一个模型形式。 Firebug和控制台确实报告POST实际发生,但记录没有更新。
欣赏任何你意识到这是错误的。
答案 0 :(得分:2)
因为,没有答案......
首先,由于您没有以任何方式修改表单,因此您无需指定它 - 默认情况下,ModelForm隐含在UpdateView中。所以,你不需要这个:
form_class=VehiclesForm
然后,在您的模型中(这是巨大的)您不需要blank=False
- 它也是默认值。
我认为你的问题在于现场验证程序。你的模板html-form只渲染了一些字段,但你的模型有更多(重要的是 - 它们不是可选的,它们没有默认值)。 因此,您根本看不到验证错误。
如果你像这样渲染整个表格:
<form method="POST">{% csrf_token %}
{{ form.as_p }}
<button type="submit"> Save</button>
<a href="javascript:close_window();"> Close</a>
</form>
...您将看到验证错误!
PS:如果你只想渲染那些字段,你必须:
1)使用这些字段的列表指定fields
属性:
# django doc example
class AuthorUpdate(UpdateView):
model = Author
fields = ['name']
template_name_suffix = '_update_form'
2)确保您提供所有需要的数据(默认或通过表单字段显式)以保存模型实例。
3)阅读Django文档 - 它非常详细和细致,祝你好运!
答案 1 :(得分:0)
对我来说,我必须改变
<form action="." .....>
到
<form action="" ..... >