Ich habe viele Fragen über Django Validatoren nicht die richtigen Antworten geben, aber ich habe ein anderes Problem. Meins wird nicht einmal ausgelöst, obwohl es aus dem Django-Beispiel kopiert wurde. Hier ist, was ich habe:Django Formular Validierung wird nicht ausgelöst
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
Ich habe sogar versucht, die forms.py wie diese Einstellung:
def clean_image(self):
print('test')
raise forms.ValidationError("Please upload a .gif file")
Und ich bekomme immer noch weder ValidationError
noch test
in der Konsole gedruckt. Hat jemand auf ein solches Problem gestoßen?
Oh, wusste nicht, sie korreliert wurden umbenannt werden. Ich danke dir sehr! –