1

Currently trying to create a small file upload system similar to what was done here I've been trying to get my code to let the user upload any file from their local PC and then let it be copied to my server. albeit with no luck, the example I was following has examples up to 1.9 Django and I can't see much variation with that and 1.10. I've attached my views.py for the project currently the website i have will not upload any type of file and im not sure why

    from django.shortcuts import render

# Create your views here.
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.shortcuts import render
from django.core.urlresolvers import reverse
from .forms import docform
from .models import Document


def upload(request):
    if request.method == 'POST':
        form = docform(request.FILES)
        if form.is_valid():
            newdoc = Document (newfile=request.FILES['newfile'])
            newdoc.save()

        return HttpResponseRedirect(reverse('upload'))
    else:
        form = docform()

    return render( request,'upload.html',)

Attaching upload.html

<!DOCTYPE html>
    <html>
        <head>
        File upload system
        </head>
        <body>
           <form action="{% url "upload" %}" method="post" enctype="multipart/form-data">
                {% csrf_token %}
                <input type="file" name=myfile" />

                <p><input type="submit" value="upload"/></p>
            </form>
        </body>

    </html>
Community
  • 1
  • 1
102254563
  • 77
  • 1
  • 1
  • 9
  • Make sure you add `enctype="multipart/form-data"` to your `
    ` tag in your template. For example `
    `. If that doesn't work, please share your template code.
    – elethan Dec 06 '16 at 12:57
  • @elethan Please find attached. – 102254563 Dec 06 '16 at 13:14
  • You have a typo in the name attribute of your file input tag. Also, please describe exactly what is happening in your post? What happens when you try to submit a file? Do you get an error message? Do you have any output in the console? – elethan Dec 06 '16 at 13:27
  • @elethan using the run server command the HTTP page will open at 127.0.0.1, with my upload fileField and an upload buttons selecting the document is all fine but once i push upload the web page almost refreshes doing nothing and not uploading the file as expected. the console runs these commands [06/Dec/2016 13:29:59] "GET /upload/upload/ HTTP/1.1" 200 441 [06/Dec/2016 13:30:03] "POST /upload/upload/ HTTP/1.1" 302 0 [06/Dec/2016 13:30:03] "GET /upload/upload/ HTTP/1.1" 200 441 – 102254563 Dec 06 '16 at 16:19

0 Answers0