Python Flask从HTML选择表单中获取项目 [英] Python Flask get item from HTML select form
问题描述
ValueError:View函数没有返回响应 code>
请问有人能帮我一下吗?我已经尝试了每一个request.get的变体,我可以在网上找到。另外,如果我指定我的表单应该使用后,它使用得到 - 任何人都知道这是为什么?
我是新来的瓶子,所以原谅我的无知!
预先感谢。
python文件(routes.py)
from flask import Flask,render_template,request
import os
app = Flask(__ name__)
musicpath = os.listdir( rC:\ Users \Oscar \ Music \iTunes \iTunes Media \Music)
lsize = str(len(musicpath))
looper = len(musicpath)
$ b @ app.route('/')
def home():
return render_template('home.html',lsize = 20,looper = looper,musicpath = musicpath)
@ app.route('/ pop',methods = ['POST','GET'])
def pop():
request.method ==GET:
text = request.args.get('som')
返回文本
#Have尝试了request.get的每个变体
@ app.route('/ about')
def about():
name =Hello!
return render_template('about.html',name = name)
$ b if __name__ =='__main__':
app.run(debug = True)
html文件(home.html)
{%extendslayout.html%}
{%block content%}
< div class =jumbo>
< h2>一个音乐应用程序!< h2>
< / div>
< div>
{%if lsize%}
< form action =/ pop>
< select id =somsize =20>
{%for i in range(looper):%}
< option value ={{i}}> {{musicpath [i]}}< / option>
{%endfor%}
< / select>
< / form>
{%endif%}
< / div>
< a href ={{url_for('pop')}}>选择,< / a>
{%endblock%}
解决方案 <问题是你的HTML表单没有名字。
request.args.get(som)
需要名称为som的HTML表单输入
< select name =somid =somsize =20>
只需更改该行并添加一个名称即可。表单对 id
属性不感兴趣。
Im having trouble getting anything from the shown HTML form
I always get "ValueError: View function did not return a response"
Can somebody help me out here please? I have tried every variation of request.get that I can find on the web. Also if I specify my form should use post it uses get anyway - anybody know why this is?
Im new to flask so forgive my ignorance!
Thanks in advance.
The python file (routes.py)
from flask import Flask, render_template, request
import os
app = Flask(__name__)
musicpath = os.listdir(r"C:\Users\Oscar\Music\iTunes\iTunes Media\Music")
lsize = str(len(musicpath))
looper = len(musicpath)
@app.route('/')
def home():
return render_template('home.html', lsize=20, looper=looper, musicpath=musicpath)
@app.route('/pop', methods=['POST', 'GET'])
def pop():
if request.method == "GET":
text = request.args.get('som')
return text
#Have tried every variation of request.get
@app.route('/about')
def about():
name = "Hello!"
return render_template('about.html', name=name)
if __name__ == '__main__':
app.run(debug=True)
The html file (home.html)
{% extends "layout.html" %}
{% block content %}
<div class="jumbo">
<h2>A Music app!<h2>
</div>
<div>
{% if lsize %}
<form action="/pop">
<select id="som" size="20">
{% for i in range(looper):%}
<option value="{{i}}">{{ musicpath[i] }}</option>
{% endfor %}
</select>
</form>
{% endif %}
</div>
<a href="{{ url_for('pop') }}">Select,</a>
{% endblock %}
解决方案 The Problem is that you HTML form does not have a name.
request.args.get("som")
needs an HTML form input with the name "som"
<select name="som" id="som" size="20">
Just change that line and add a name. The form is not interested in the id
attribute.
这篇关于Python Flask从HTML选择表单中获取项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!