Python Django:空表单错误
当我试图从数据库中更新一些记录时,我遇到了一个小问题。 奇怪的是Python Django:空表单错误,python,django,forms,Python,Django,Forms,当我试图从数据库中更新一些记录时,我遇到了一个小问题。 奇怪的是表单。如果表单无效,则错误为空(我不明白为什么) 这是你的电话号码 形式 模型 更新视图 我的代码怎么了 我已经尝试为大多数模型字段设置null属性,从基于类的视图切换到标准视图,但我无法更新记录 苏丹 谢谢如果没有POST数据,则request.POST或None为None,因此表单未绑定 未绑定表单始终无效,但没有任何错误 在您的情况下,您可能需要将else:子句更改为elif请求。POST: 有关更多详细信息,请参阅上的文档。
表单。如果表单无效,则错误为空(我不明白为什么)
这是你的电话号码
形式
模型
更新视图
我的代码怎么了
我已经尝试为大多数模型字段设置null
属性,从基于类的视图切换到标准视图,但我无法更新记录
苏丹
谢谢如果没有POST
数据,则request.POST或None
为None
,因此表单未绑定
未绑定表单始终无效,但没有任何错误
在您的情况下,您可能需要将else:
子句更改为elif请求。POST:
有关更多详细信息,请参阅上的文档。非常感谢,Django的文档对我来说就足够了。
class PetitionUpdateForm(forms.ModelForm):
owner = forms.ModelChoiceField(
label=_('Petition creator'),
queryset=User.objects.all(),
widget=forms.HiddenInput()
)
class Meta:
fields = ('title', 'petition_text', 'description',
'category', 'num_signs', 'date_to', 'owner',)
model = Petition
class Petition(models.Model):
PETITION_STATUSES = (
('N', _('New petition')), # New one
('M', _('Moderation')), # On moderation
('R', _('Rejected')), # Failed petition
('S', _('Success')) # Succeeded petition
)
title = models.CharField(max_length=512)
slug = models.SlugField(max_length=512, editable=False, blank=True)
description = models.TextField()
petition_text = models.TextField(blank=True, null=True)
petition_picture = models.ImageField(upload_to=get_upload_path, blank=True)
petitioning = models.ManyToManyField(PetitionTarget, editable=False)
signs = models.ManyToManyField(User, editable=False, related_name='petition_signs')
num_signs = models.IntegerField(max_length=11, default=100, blank=True)
category = models.ForeignKey(Category, blank=True, null=True, related_name='petition_category')
date_to = models.DateTimeField(blank=True, null=True)
videos = models.ManyToManyField(Video, editable=False)
photos = models.ManyToManyField(Photo, editable=False)
audios = models.ManyToManyField(Audio, editable=False)
documents = models.ManyToManyField(Document, editable=False)
created = models.DateTimeField(auto_now_add=True, editable=False)
changed = models.DateTimeField(auto_now=True, editable=False)
status = models.CharField(max_length=1, choices=PETITION_STATUSES, default='M', blank=True)
owner = models.ForeignKey(User, related_name='petition_owner')
def __unicode__(self):
return u'{0}: {1}'.format(_('Petition'), self.title)
@login_required
@render_to('petition/edit-petition.html')
def update_petition(request, slug):
p = get_object_or_404(Petition, slug=slug)
form = PetitionUpdateForm(request.POST or None, instance=p)
import pdb
pdb.set_trace()
if form.is_valid():
form.save()
messages.success(request, _('Petition saved'))
else:
print form.errors # errors are empty
messages.success(request, _('Some error happened'))
return {'form': form, 'petition': p}