Flask/HTML问题:来自HTML中输入文本框的请求在哪里到达Flask中的app.route



我对烧瓶和web编程一般来说都是新手。我正在尝试一个简单的例子。我有一个HTML基础模板,它显示了一个文本框和一个动物的图片。模板用烧瓶渲染。这个想法是,用户可以在文本框中键入新动物的名称,然后图片就会更改为新动物。

我测试了代码。有一个问题,html文本框中给出的输入文本似乎没有进入正确的app.route。或者至少我不知道(因为我在任何地方运行python,服务器中的打印语句都不会显示在控制台上(。

这是代码和模板。请告诉我我做错了什么。谢谢

这是flask_app.py:

from flask import render_template
from flask import request, redirect
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
imgname = "tiger2.png"
return render_template('untitled1.html', title='TIGER', fname=imgname)
@app.route('/', methods=['POST', 'GET'])
def imgshow(animal):
#assert request.method == 'POST'
#print("New request!")
animal = request.form['animal']
if animal.lower() == 'tiger':
imgname = 'tiger2.png'
elif animal.lower() == 'lion':
imgname = 'lion1.png'
elif animal.lower() == 'panther':
imgname = 'panther.png'
else:
imgname = 'lion1.png'
return render_template('untitled1.html', title=animal.upper(), fname=imgname)

这是模板untitled1.html

<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<!-- Serving an Image -->
<h1>Hello, World!</h1>
<form action="">
<label for="animal">Animal: </label>
<input type="text" id="animal" name="animal"><br><br>
</form>

<img src="{{ url_for('static', filename=fname ) }}" alt="Tiger">
</body>
</html>

试试这个:

from flask import render_template
from flask import request, redirect
from flask import Flask
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
imgname = "tiger2.png"
title='TIGER'
else:
variants = {
'tiger': 'tiger2.png',
'lion': 'lion1.png',
'panther': 'panther.png'
}
animal = request.form.get('animal').lower()
imgname = variants.get(animal)
title = animal.upper()
return render_template('untitled1.html', title='TIGER', fname=imgname)

对我来说,最好的方法是只使用GET方法:

from flask import Flask, render_template, request, redirect
app = Flask(__name__)
animals = {'tiger': 'tiger2.png', 
'lion': 'lion1.png', 
'panther': 'panther.png'}

@app.route('/')
def index():
animal = request.args.get('animal', 'tiger')
image = animals.get(animal, 'lion')
return render_template('untitled1.html', title=animal.upper(), fname=image)

当您需要进行一些处理(从数据库中写入数据(,然后重定向到另一个GET路由时,POST方法是最好的。

app.route注册在给定的网址(在您的情况下为(下,将实现下面列出的函数。问题是,您在同一路由下注册了两个函数,这意味着第一个注册被删除。

您不需要第一个app.route。基本上应该说,动物的默认值是老虎。第二个功能应修改如下:

display_map = {
'tiger': 'tiger2.png',
'lion': 'lion1.png',
'panther': 'panther.png'
}
@app.route('/', methods=['POST', 'GET'])
def imgshow(animal):
if request.method == 'POST':
animal = request.form.get('animal', 'lion').lower()
imgname = display_map.get(animal, 'lion1.png')
return render_template('untitled1.html', title=animal.upper(), fname=imgname)
else:
return render_template('untitled1.html', title='TIGER', fname='tiger.png')       

此外,您还需要实际向服务器提交结果。

<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<!-- Serving an Image -->
<h1>Hello, World!</h1>
<form method="POST">
<label for="animal">Animal: </label>
<input type="text" id="animal" name="animal"><br><be>
<input type="submit" name="submit" value="submit">
</form>

<img src="{{ url_for('static', filename=fname ) }}" alt="Tiger">
</body>
</html>

最新更新