1

I have a file upload form which I can not get to validate successfully. The form loads fine and I can upload a file but after 'submit' I can not get past Fileform.is_valid() in the view. I have mostly copies this view and the model and form model from the Django 1.9 File Uploads documentation but I must be missing something.

The view.py is,

def file_sharing_form(request):
if request.method == "POST":
    file = FileForm(request.POST, request.FILES)
    if file.is_valid():
        fform = file.save(commit=False)
        fform.author = request.user
        fform.pub_date = timezone.now()
        fform.submitted_date = timezone.now()
        fform.approved = False
        fform.save()
        # email admin
        admin_email = User.objects.all().filter(is_superuser = True)
        subject = 'File submitted to QQIresources, awaiting approval'
        to_email = admin_email[0].email
        from_email = request.user.email
        message = 'A file has been submitted to QQIresources by ' + str(request.user) + ' and is awaiting admin approval. \n \n Title: ' + str(fform.title) + '\n Author: ' + str(fform.author) + '\n Description: ' + str(fform.description)
        send_mail(subject, message, from_email, [to_email])
        return redirect('init')
fileform = FileForm()
return render(request, 'file_form.html', {'fileform': fileform})
2
  • I know the variable names are close but I think what I am using is correct Fileform = FileForm(.... Lower case 'f' for form in the first and uppercase 'F' in Form for the second. Thanks Commented May 30, 2016 at 16:09
  • I will change it to make them more distinct. But this is not the problem, thanks Commented May 30, 2016 at 16:13

1 Answer 1

6

I guess you have missed enctype="multipart/form-data" in your html. Please add this in your form definition for any type of file upload.

<form action="{% url 'your_url' %}" enctype="multipart/form-data">
    <input type="file">
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

I did read about enctype="multipart/form-data" in the documentation but I thought it would be handles in the form. Obviously not, working now thanks
You are Welcome :) @ofey

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.