简体   繁体   English

Django使用提交按钮而不使用输入标签发布文本

[英]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. 我有一个django模板,其中有一种形式,可以从用户那里获取输入。

template.html 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 输入的文本很容易通过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. 但是我也想在单击“提交”按钮时从<img src="/media/{{ image }}" > How can I do that ? 我怎样才能做到这一点 ?

in html 在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 在您的html模板表单中添加此行

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

in Django views simply get the image path 在Django视图中只需获取图像路径

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. 因此获取文件路径就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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