[英]Why is the value of the "name" (check html code ln.40) not showing when i run the code?
我正在学习 python 并且只是作为初学者做一些实际练习。 所以,在这里我只想在“当前团队”这个短段中显示输入的球员姓名(检查 html 代码 ln. 40 下)。 我知道问题不在于我缺乏经验,但实际上我更喜欢做练习,而不仅仅是理论学习:)
这是 python 代码:
from flask import Flask, render_template, request, flash
app = Flask(__name__)
@app.route('/', methods=['POST', 'GET'])
def home():
if request.method == 'POST':
#app.config['SECRET_KEY'] =
name = request.form.get('name')
if len(name) < 1:
flash('name is too short', category='error')
else:
def add_player():
player = name
print(name)
add_player()
flash('player added', category="success")
return render_template("index.html")
if __name__ == "__main__":
app.run(debug=True)
这是 html 代码(不关心样式):
{% block content %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
crossorigin="anonymus"
/>
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"
crossorigin="anonymus"
/>
<link rel="stylesheet" href="static/main.css" />
<title>Team Players</title>
</head>
<body>
<div calss="firsdiv">
<h1>Add Team Players</h1>
</div>
<form method="POST">
<input type="text" name="name" id="name" placeholder="Enter your name" ></input>
<br />
<div align="relative">
<button type="submit" class="btn btn-primary">Show Down</button>
</div>
</form>
<div class="center">
<p>Current Team</p>
<ul class="list-group list-group-flush" id="players">
<li class="list-group-item">
{{ player }}
<!-- Here i want to show the entered players names down "current Team" when i run the code -->
</li>
</ul>
</div>
</body>
</html>
{% endblock %}
只需将您的 python 代码替换为:-
from flask import Flask, render_template, request, flash
app = Flask(__name__)
@app.route('/')
def index():
return render_template("index.html")
@app.route('/', methods=['POST', 'GET'])
def home():
if request.method == 'POST':
#app.config['SECRET_KEY'] =
name = request.form.get('name')
if len(name) < 1:
flash('name is too short', category='error')
else:
flash('player added', category="success")
return render_template("index.html", player=name)
if __name__ == "__main__":
app.run(debug=True)
如果你想返回当前球队的所有球员,那么你需要将它存储在某个地方,这就是为什么我创建了一个名为 names.txt 的文件,它将存储用户在当前 session 中输入的所有球员,这意味着如果用户关闭或刷新网站,names.txt 文件将被删除,这意味着名称将消失。 我想这就是你所要求的。
python 代码:-
from flask import Flask, render_template, request, flash
import os
app = Flask(__name__)
@app.route('/')
def index():
os.remove('names.txt')
return render_template("index.html")
@app.route('/', methods=['POST', 'GET'])
def home():
if request.method == 'POST':
#app.config['SECRET_KEY'] =
name = request.form.get('name')
if len(name) < 1:
flash('name is too short', category='error')
else:
flash('player added', category="success")
file1 = open("names.txt",'a')
file1.write(name+"\n")
file1.close()
file2 = open("names.txt", 'r')
l = file2.readlines()
file2.close()
s = ','.join(l)
return render_template("index.html", player=s)
if __name__ == "__main__":
app.run(debug=True, port=8000)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.