简体   繁体   中英

Django post text using submit button without using input tag

I have a django template in which there is a form in which I am getting input from the user.

template.html

{% extends 'header.html' %}
{% load staticfiles %}

{% block content %}

    <h3> Questtion</h3>


    <img src="/media/{{ image }}" >
    <br> <br>
    <h3> Responses </h3>
<form  method="post" action="/tools/submit">
    {% csrf_token  %}
    <input type="text" name="first_response" placeholder="First Response"  size="40" required>
    <input type="text" name="second_response" placeholder="Second Response"  size="40" required> 
    <input type="text" name="third_response" placeholder="Third Response"  size="40" required> 
    <input type="text" name="fourth_response" placeholder="Fourth Response"  size="40" required>
    <input type="text" name="fifth_response" placeholder="Fifth Response"  size="40" required>

    <button type="submit" >Submit</button>
</form>   

{% endblock %}

The text from input is easy to get with the following method in views.py

def post_form(request):
    if request.method == 'POST':
        first_response = request.POST.get('first_response')
        second_response = request.POST.get('second_response')
        third_response = request.POST.get('third_response')
        fourth_response = request.POST.get('fourth_response')
        fifth_response = request.POST.get('fifth_response')
        image_name = # HOW TO GET {{ image }} name here ?

But I want to also post {{ image }} from <img src="/media/{{ image }}" > also when the submit button is clicked. How can I do that ?

in html

<form  method="post" action="/tools/submit" enctype="multipart/form-data">

<input type="file" name="image" placeholder="Image Response"  size="40" required>

in view

fifth_response = request.FILES.get('image')

Inside your html template form add this line

<input type="hidden" name="question" value="/media/{{ image }}">

in Django views simply get the image path

fifth_response = request.POST.get('question')

since the image is already on your server i don't think you would like to have it as file. so getting file path is enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM