2👍
✅
Doing it in the view (as you’ve shown) is the right way to do this. Most likely you’re having problems because you’ve left username
as a field on the form, and because the FK model field doesn’t have blank=True
set the form requires the field to be provided. You should explicitly declare just the subset fields that you want to accept user input for in the form’s Meta
class.
class UploadedTextFileForm(ModelForm):
class Meta:
model = UploadedTextFile
fields = ['file', 'filename']
I am not sure why you’re rendering a different template when the form is not valid, but no matter what you’re not providing the form object in the context. This means that you’ll never see any errors the form detects, which is probably what’s happening with this code – you’re not seeing the error that username
is not provided.
Source:stackexchange.com