我见过很多关于django验证器没有给出正确答案的问题,但我有一个不同的问题。尽管从Django文档示例中复制,但我甚至都没有触发。这就是我所拥有的:
models.py
def content_file_name(instance, filename):
ext = ''.join(filename.split())[:-4]
foldername = "%s/%s" % (uuid.uuid4(), ext)
return '/'.join(['documents', str(foldername), filename])
class Document(models.Model):
docfile = models.ImageField(upload_to=content_file_name)
class DocumentImage(models.Model):
imagefile = models.ImageField(upload_to=content_file_name)
image = models.ForeignKey(Document, related_name='Image', null=True, on_delete=models.CASCADE)
views.py
def documentlist(request):
# Handle file upload
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = form.save()
newdoc.create_documentfiles()
messages.add_message(request, messages.INFO, "Saved")
return HttpResponseRedirect(reverse('list'))
else:
form = DocumentForm() # A empty, unbound form
# Load documents for the list page
documents = Document.objects.all()
# Render list page with the documents and the form
return render(
request,
'list.html',
{'documents': documents, 'form': form}
)
forms.py
class DocumentForm(forms.ModelForm):
class Meta:
model = Document
fields = ('docfile',)
def clean_image(self):
file = self.cleaned_data.get('docfile')
if file:
if imghdr.what(file.read()) != "gif":
raise forms.ValidationError("Please upload a .gif file")
print('complete');
file.seek(0)
return file
我甚至试过像这样设置 forms.py :
def clean_image(self):
print('test')
raise forms.ValidationError("Please upload a .gif file")
我仍然没有在控制台中打印ValidationError
或test
。有人遇到过这样的问题吗?
答案 0 :(得分:2)
您的验证功能名称为clean_image
,但表单上没有image
字段
fields = ('docfile',)
所以它没有被调用。如果您要验证clean_docfile
字段
docfile
在表单子类上调用
clean_<fieldname>()
方法 - 其中<fieldname>
替换为表单字段属性的名称。此方法执行特定于该特定属性的任何清理,与其所属的字段类型无关。此方法未传递任何参数。您需要在self.cleaned_data中查找该字段的值,并记住此时它将是一个Python对象,而不是表单中提交的原始字符串(它将在cleaning_data中,因为一般字段clean()方法,上面,已经清理了一次数据。)
https://docs.djangoproject.com/en/1.11/ref/forms/validation/
答案 1 :(得分:1)
您的干净方法名称应重命名为
Sub CleanUpExport()
Dim aFrame As Frame
Dim p As Paragraph
Dim l As Single
For Each aFrame In ActiveDocument.Frames
aFrame.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
l = aFrame.HorizontalPosition
For each p In aFrame.Range.Paragraphs
p.LeftIndent = l
Next p
aFrame.Delete
Next aFrame
End Sub