Python 为什么添加引导会阻止我的Flask应用程序工作?
我第一次学习Flask,所以我创建了一个基本的应用程序,将名字添加到无序列表中。插入的名称在输入字段中输入。基本上,当我没有添加引导时,它工作得很好,但当我添加引导时,当我单击按钮时,它什么也没做 无引导时的代码Python 为什么添加引导会阻止我的Flask应用程序工作?,python,flask,bootstrap-4,Python,Flask,Bootstrap 4,我第一次学习Flask,所以我创建了一个基本的应用程序,将名字添加到无序列表中。插入的名称在输入字段中输入。基本上,当我没有添加引导时,它工作得很好,但当我添加引导时,当我单击按钮时,它什么也没做 无引导时的代码 <form action="{{ url_for('index') }}" method="POST"> <input type="text" name="name" placeholder="Enter your name" style="bord
<form action="{{ url_for('index') }}" method="POST">
<input type="text" name="name" placeholder="Enter your name" style="border-radius: 20px;">
<button>Click Me!</button>
</form>
<ul>
{% for item in task %}
<li> {{ item }} </li>
{% endfor %}
</ul>
将
更改为
。您可以添加任何引导类来设置此提交输入的样式,就像按钮一样。您的问题可能不是引导
,而是type=“button”
如果在
中使用type=“button”
,即使没有引导
,也会出现同样的问题
您必须删除它或使用type=“submit”
在
中,只能使用
将表单发送到服务器type=“submit”
清除表单(不发送到服务器)type=“reset”
创建可点击按钮,但它不会发送到服务器,您可以分配JavaScript代码,在点击后运行某些功能type=“button”
顺便说一句:我不确定,但可能
Bootstrap
有一些JavaScript文件,当您使用type=“button”
时,您必须在HTML中链接这些文件以更改按钮行为
最小工作示例
from flask import Flask
from flask import render_template_string, request
app = Flask(__name__)
@app.route("/", methods=["GET","POST"])
def index():
if request.method=="POST":
name = request.form.get("name")
print(name)
return render_template_string("""<form action="{{ url_for('index') }}" method="POST">
<input type="text" name="name" placeholder="Enter your name">
<br>
<button>Button without Type</button>
<br>
<button type="submit">Button type="Submit"</button>
<button type="reset">Button type="Reset"</button>
<button type="button">Button type="Button"</button>
<button type="button" onclick="alert('Clicked')">Button type="Button" with JavaScript</button>
<br>
<input type="submit" value='Input type="Submit"'/>
<input type="reset" value='Input type="Reset"'/>
<input type="button" value='Input type="Button"'/>
<input type="button" onclick="alert('Clicked')" value='Input type="Button" with JavaScript"'/>
</form>""")
app.run()
从烧瓶导入烧瓶
从flask导入渲染模板字符串,请求
app=烧瓶(名称)
@app.route(“/”,methods=[“GET”,“POST”])
def index():
如果request.method==“POST”:
name=request.form.get(“name”)
印刷品(名称)
返回渲染模板字符串(“”)
无类型按钮
按钮类型=“提交”
按钮类型=“重置”
按钮类型=“按钮”
按钮类型=“按钮”与JavaScript
""")
app.run()
Doc:,当您单击按钮时,浏览器控制台中是否出现JavaScript错误?您确定应该是
type=“button”
?可能是type=“submit”
?即使没有引导
from flask import Flask
from flask import render_template,request
app = Flask(__name__)
task=[]
@app.route("/",methods=["GET","POST"])
def index():
if request.method=="POST":
name = request.form.get("name")
task.append(name)
return render_template("index.html",task=task)
from flask import Flask
from flask import render_template_string, request
app = Flask(__name__)
@app.route("/", methods=["GET","POST"])
def index():
if request.method=="POST":
name = request.form.get("name")
print(name)
return render_template_string("""<form action="{{ url_for('index') }}" method="POST">
<input type="text" name="name" placeholder="Enter your name">
<br>
<button>Button without Type</button>
<br>
<button type="submit">Button type="Submit"</button>
<button type="reset">Button type="Reset"</button>
<button type="button">Button type="Button"</button>
<button type="button" onclick="alert('Clicked')">Button type="Button" with JavaScript</button>
<br>
<input type="submit" value='Input type="Submit"'/>
<input type="reset" value='Input type="Reset"'/>
<input type="button" value='Input type="Button"'/>
<input type="button" onclick="alert('Clicked')" value='Input type="Button" with JavaScript"'/>
</form>""")
app.run()