简体   繁体   English

通过 Python 函数从 HTML 表单传递用户输入以在 Flask 中定义变量

[英]Passing user input from HTML form through a Python function to define variables in Flask

I am trying to create a site in Flask that:我正在尝试在 Flask 中创建一个站点:

  • Takes a user's input接受用户的输入
  • Checks if the user's input matches a key in a dictionary检查用户的输入是否与字典中的键匹配
  • Returns a page with all of the corresponding values返回包含所有相应值的页面

So far I have this:到目前为止,我有这个:

@app.route('/')
def home():
    return render_template("index.html")

and the index.html:和 index.html:

<!DOCTYPE html>
<form action = "/">
    WHAT ANIMAL DO YOU WANT? <br>
    <input type = "text" name = "animal"><br>
    <input type = "submit" value = "submit">
</form>

I've also imported a dictionary (animal_dict) that I created that looks something like:我还导入了一个我创建的字典 (animal_dict),它看起来像:

animal_dict = {
1: {'Name':'Dog', 'Kingdom':'Animalia', 'Species':'C. lupus'}, 
2: {'Name':'Cat', 'Kingdom':'Animalia', 'Species':'F. catus'}, 
3: {'Name':'Squirrel', 'Kingdom':'Animalia', 'Species':'E. sibiricus'}, 
4: {'Name':'Brown Trout', 'Kingdom':'Animalia', 'Species':'S. trutta'}
}

My approach was to create a page that would check if the user's input matches one of the animals in the dictionary, and then define variables that I could use in a generate page of the animal's information:我的方法是创建一个页面来检查用户的输入是否与字典中的一种动物相匹配,然后定义我可以在动物信息的生成页面中使用的变量:

@app.route('/animalpage')
def animalpage():
    for i in animal_dict:
        if request.form['animal'].title() == animal_dict[i]['Name']:
            animal_name = animal_dict[i]['Name']
            animal_kingdom = animal_dict[i]['Kingdom']
            animal_species = animal_dict[i]['Species']
    return render_template('animal_page.html', name = animal_name, kingdom = animal_kingdom, species = animal_species)

and the animal_page.html would look like:和animal_page.html 看起来像:

<!DOCTYPE html>
<title> Here's the Pokemon Data! </title>
    <body>
        <p><h1>{{ name }}</h1></p>
        <p><h1>{{ kingdom }}</h1></p>
        <p><h1>{{ species }}</h1></p>
        <a href="/"> Click me to go back to the home page! </a>
    </body>

But when I run the site, I get an error "UnboundLocalError: local variable 'animal_name' referenced before assignment"但是当我运行该站点时,出现错误“UnboundLocalError:分配前引用的局部变量‘animal_name’”

I think it might be because the return for the animal_page.html with all those defined variables runs before the for loop.我认为这可能是因为带有所有这些定义变量的animal_page.html 的返回在for 循环之前运行。 But I'm not sure how to clean it up so it works.但我不确定如何清理它以使其有效。 I'm also importing the animal_dict from another .py file, but would it be better to put it in this app.py?我也在从另一个 .py 文件中导入 animal_dict,但是将它放在这个 app.py 中会更好吗?

Any help would be appreciated.任何帮助,将不胜感激。 I can also elaborate more if need be.如果需要,我也可以详细说明。 Thank you!谢谢!

尝试将return语句放在if条件中。

Your condition is failing in the if statement.您的条件在 if 语句中失败。 Try removing the .title() and then give it a try尝试删除 .title() 然后试一试

if request.form['animal'] == animal_dict[i]['Name']:如果 request.form['animal'] == animal_dict[i]['Name']:

Also change the action to '/animalpage'还将操作更改为“/animalpage”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM