I have the following setup in Django. A text input validated by CharField and a FileField for an image upload. The desired response for when a field is empty should be that the data originally on the form is present and all the user needs to do is fill in the missing data. I've listed the two situations that might require validation and the current state of how the app responds:
- The text is present, but the image is forgotten. The result after reload: The text that was originally in the post is still present and all the user has to do is upload the file. This works as desired. Excellent
- The text is forgotten, but the image is present. The result after reload: The image is no longer present and requires the user to upload it again, which is undesirable.
How does one hold onto the file data after validation?
view.py
def signin(request, template="signin.html"):
c['form'] = SignInForm()
if request.method == 'POST':
c['form'] = SignInForm(request.POST, request.FILES)
if c['form'].is_valid():
#TODO: Commit data
return redirect("somwhere_else.html")
return render(request, template, c)
forms.py
class SignInForm(forms.Form):
name = forms.CharField(max_length=50,required=True)
photo_input = forms.FileField(required=True)