You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm stuck with validating uploaded files using UploadedFileInput. I seem to have two different problems.
My first problem is that error messages do not show when selecting invalid files. Upon selecting a file that is too large, nothing happens (no errors are shown, no POST request is sent, GUI is not updated). Here I would at least expect a message to display "File size is too big" or so. When selecting a file with an invalid extension, a POST request is sent and the GUI is updated to show the selected file (with or without a valid thumbnail). However, no error message is shown. I expected a message to be shown such as "This file type is not allowed".
As I understand, JSON responses are not processed when performing the file selection. Thus creating specific JSON responses will not work out of the box. Is there another way to show these error messages? For example by making use of the <meta name="error-messages" type_mismatch="File upload still in progress." pattern_mismatch="This is not a valid file." bad_input="File upload failed."> element?
My second problem is somewhat related. When submitting the form with an invalid file (e.g. wrong extension), I always receive the form error {"uploaded_file": [{"message": "Please either submit a file or check the clear checkbox, not both.", "code": "contradiction"}]}. Which seems to be because the input field is made empty/removed from form.cleaned_data if the file is invalid. I would like to display a more useful message, such as that the extension is incorrect. However, I cannot validate the file anymore if it is missing from my data?
The main thing I'm looking for is some documentation. How are these things supposed to be handled? I'm feeling like I just overlooked something perhaps? Any help is greatly appreciated.
I'm using Django 5.2.4, Django CMS 4.1.4, and django-formset 2.0.1
My form class currently looks as follows:
class ContactForm(forms.Form):
name = fields.CharField(widget=widgets.TextInput(attrs={"placeholder": ""}))
email_address = fields.EmailField(widget=widgets.TextInput(attrs={"placeholder": ""}))
subject = fields.CharField(widget=widgets.TextInput(attrs={"placeholder": ""}))
message = fields.CharField(widget=widgets.Textarea(attrs={"placeholder": "","rows":"5"}))
uploaded_file = FileField(
label="Image (optional)",
widget=UploadedFileInput(attrs={
'accept': 'image/jpeg,image/png,image/gif',
'max-size': MAX_UPLOAD_SIZE,
}),
help_text="JPEG, PNG, or GIF. Max file size 5 MB. We accept other formats after initial contact.",
required=False,
)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I'm stuck with validating uploaded files using UploadedFileInput. I seem to have two different problems.
My first problem is that error messages do not show when selecting invalid files. Upon selecting a file that is too large, nothing happens (no errors are shown, no POST request is sent, GUI is not updated). Here I would at least expect a message to display "File size is too big" or so. When selecting a file with an invalid extension, a POST request is sent and the GUI is updated to show the selected file (with or without a valid thumbnail). However, no error message is shown. I expected a message to be shown such as "This file type is not allowed".
As I understand, JSON responses are not processed when performing the file selection. Thus creating specific JSON responses will not work out of the box. Is there another way to show these error messages? For example by making use of the
<meta name="error-messages" type_mismatch="File upload still in progress." pattern_mismatch="This is not a valid file." bad_input="File upload failed.">
element?My second problem is somewhat related. When submitting the form with an invalid file (e.g. wrong extension), I always receive the form error
{"uploaded_file": [{"message": "Please either submit a file or check the clear checkbox, not both.", "code": "contradiction"}]}
. Which seems to be because the input field is made empty/removed fromform.cleaned_data
if the file is invalid. I would like to display a more useful message, such as that the extension is incorrect. However, I cannot validate the file anymore if it is missing from my data?The main thing I'm looking for is some documentation. How are these things supposed to be handled? I'm feeling like I just overlooked something perhaps? Any help is greatly appreciated.
I'm using Django 5.2.4, Django CMS 4.1.4, and django-formset 2.0.1
My form class currently looks as follows:
Beta Was this translation helpful? Give feedback.
All reactions