Saturday, February 23, 2019

Cannot update file to a Django form

I can't seem to upload a file to a Django form.  The form POST is never valid.
Any ideas?

Here is my code...

def upload_file(request):
    if request.method == 'POST':
        form = tummytutoring.forms.UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            open("FTM_file.exe", "w").write(request.FILES['file'])
            return django.shortcuts.redirect("/thank_you")
    else:
        form = tummytutoring.forms.UploadFileForm()
    return django.shortcuts.render(request, 'upload.html', {'form': form})


Here is the template....

  <form action = "." method = "post">
    <div>{{form.file}}</div>

    <div><input type = "submit" value = "Send"/></div>

    {% csrf_token %}
  </form>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fdd912f5-0aed-4e5c-bba7-7df5d334bf16%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment