0

This is my views.py

from django.shortcuts import render

# Create your views here.
from forms import DocumentForm
from models import Document

def SaveDocument(request):
    saved = False
    if request.method == "POST":
        #Get the posted form
        MyDocumentForm = DocumentForm(request.POST, request.FILES)

        if MyDocumentForm.is_valid():
            print 'It enters here'
            document = Document()
            document.name = MyDocumentForm.cleaned_data["name"]
            document.document = MyDocumentForm.cleaned_data["document"]
            print document.document
            document.save()
            saved = True
        else:
            print 'Fails'
    else:
        MyDocumentForm = DocumentForm()

    return render(request, 'saved.html', locals())

profile.html

<html>
   <body>

      <form name = "form" enctype = "multipart/form-data" 
         action = "{% url "SaveDocument" %}" method = "POST" >{% csrf_token %}

         <div style = "max-width:470px;">
            <center>  
               <input type = "text" style = "margin-left:20%;" 
               placeholder = "Name" name = "name" />
            </center>
         </div>

         <br>

         <div style = "max-width:470px;">
            <center> 
               <input type = "file" style = "margin-left:20%;" 
                  placeholder = "document" name = "document" />
            </center>
         </div>

         <br>

         <div style = "max-width:470px;">
            <center> 

               <button style = "border:0px;background-color:#4285F4; margin-top:8%; 
                  height:35px; width:80%; margin-left:19%;" type = "submit" value = "Login" >
                  <strong>Login</strong>
               </button>

            </center>
         </div>

      </form>

   </body>
</html>

The code works perfectly fine but it does not seem to save the file to disk. Printing the document shows that there is a document.

May i know what i did wrongly? i was following this example with minor changes

Need a minimal Django file upload example

in this line document.document = MyDocumentForm.cleaned_data["document"]

Community
  • 1
  • 1
aceminer
  • 4,089
  • 9
  • 56
  • 104

0 Answers0