[英]Django post text using submit button without using input tag
我有一个django模板,其中有一种形式,可以从用户那里获取输入。
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 %}
输入的文本很容易通过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 ?
但是我也想在单击“提交”按钮时从<img src="/media/{{ image }}" >
。 我怎样才能做到这一点 ?
在HTML
<form method="post" action="/tools/submit" enctype="multipart/form-data">
<input type="file" name="image" placeholder="Image Response" size="40" required>
鉴于
fifth_response = request.FILES.get('image')
在您的html模板表单中添加此行
<input type="hidden" name="question" value="/media/{{ image }}">
在Django视图中只需获取图像路径
fifth_response = request.POST.get('question')
由于该图像已经在您的服务器上,因此我不希望您将其作为文件保存。 因此获取文件路径就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.