aaa
aaa

Reputation: 89

display data in html from django

I tried to display the data (the result return back from upload_file in views.py) in HTML from django, but it didn't show anything after clicking submit button, only jump back to the initial ui.

undex.html

<!DOCTYPE html>
    <head>
      <title>Detect Pothole</title>
    </head>     
    <body>
        <p>upload image</p>     
        <form method="post" enctype="multipart/form-data">{% csrf_token %}
          <input type="file" name="img" accept="image/*" onchange="loadFile(event)"></input>
          <img id="output"/>
          <script> <!--view selected image -->
                var loadFile = function(event) {
                    var output = document.getElementById('output');
                    output.src = URL.createObjectURL(event.target.files[0]);
                 };
          </script>
          <input method='POST' type="Submit" name="submit" value="submut"></input>

        </form>
        {% if submitbutton == "Submit" %}
            {% for res in result %}
            {{res}}

            {% endfor %}
        {% endif %}
    </body>
</html>

view.py

def upload_file(request):
    submitbutton= request.POST.get("submit")
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['img'])
            result=process('a.png')  ## will return 0 or 1
            print(result)
            context= {'form': form, 'submitbutton': submitbutton,'result':result} 
            return render(request, 'index.html', context)                       
    else:   
        form = UploadFileForm()
    return render(request, 'ui.html', {'form': form})

Upvotes: 0

Views: 71

Answers (2)

Tiego A. Nascimento
Tiego A. Nascimento

Reputation: 219

Just check this line in your undex.html:

<input method='POST' type="Submit" name="submit" value="submut"></input>

value is set as "submut" and it must be "submit".

Check this line too:

{% if submitbutton == "Submit" %} 

"Submit" must be "submit" as the value in your input

Upvotes: 1

Nj Nafir
Nj Nafir

Reputation: 568

You say, your template filename is undex.html and you'r rendering index.html check it

Upvotes: 0

Related Questions