0

I'm using Django to develop a website and I want to upload files using dropzone box in HTML template and then send it to a view function and send some message if the file was successfully uploaded This is the code in the HTML template(inputFile_pannel.html):

  <p>{{mess}}</p>
    <form   action="{% url 'upload_file' %}" enctype="multipart/form-data" class="dropzone" id="myDropzone" >
     {% csrf_token %}

       </form>

and This is the view function(uplosd_file):

@login_required
def upload_file(request):
uploaded_file_url=''
mess=''
if request.method == 'POST' and request.FILES['file']:
    myfile = request.FILES['file']
    fs = FileSystemStorage()
    filename = fs.save(myfile.name, myfile)
    uploaded_file_url = fs.url(filename)
    mess='The file is uploade successfully, you can check the status in your pannel'
    return render(request, 'app/inputFile_pannel.html', {
        'mess':mess
    })

return render(request, 'app/inputFile_pannel.html', {
    'mess':mess
})

But the mess variable doesn't appear on the web page, could anybody say what's the problem?

kbjango
  • 1
  • 4
  • Seems like your if statement isn't firing off properly. Are you sure that the code inside the if is running? – Hanny May 01 '19 at 20:22
  • Yes, it is running cause the file is uploaded and saved in the path – kbjango May 01 '19 at 20:37
  • even I tried to redirect the page to an other web page if statement is running true but it doesn't work:( the file is uploaded correctly but the rest of block doesn't work – kbjango May 01 '19 at 21:33
  • I'm really confused it may be because of dropzone options, and a postback causes this problem, anybody has any idea? – kbjango May 02 '19 at 00:26
  • So if you change `mess=''` to `mess='something'`, does 'something' show up in the template? – Hanny May 02 '19 at 00:29
  • yes it shows up !! I think there should be something in dropzone javascripts, it's my first time that I use dropzone, I don't have such problem with other in other input forms that are not included dropzone file – kbjango May 02 '19 at 02:19
  • Then you have confirmed, your if statement is not running. That's why the message isn't showing up in your template. You will have to troubleshoot why that is a bit more. I know nothing about drop one, so I can't help there, sorry. – Hanny May 02 '19 at 02:22
  • Ok ,Thank you so far for your answer, the if block is running because the save statements are executed and file are uploaded, I think may be dropzone causes the page to be refreshed twice and in the second post the mess variable change – kbjango May 02 '19 at 02:32
  • Check the dropzone docs. They will tell you exactly what's up. – Hanny May 02 '19 at 10:38

1 Answers1

0
<form   action="{% url 'upload_file' %}" enctype="multipart/form-data" class="dropzone" id="myDropzone" **method="POST"**>

You have to make your method POST... otherwise your request will be GET.

Wils
  • 1,178
  • 8
  • 24
  • when I change the option of forceFallback:false, it works properly, but it is not dropzone any more the shape of dropzone changes to file regular input and submit button – kbjango May 02 '19 at 13:43
  • it seems the first render doesn't run because when I change the template page, it still stays in the same template, even if I put the second render in else block(if it doesn't a post method) it doesn't work:( – kbjango May 02 '19 at 16:58