将来自HTML表单的用户输入作为Python脚本的变量 [英] Taking user input from HTML form as a variable for Python script
问题描述
我正在Python/Flask中创建一个Web应用程序,以通过使用tweepy的twitters API显示推文.我已经建立了HTML表单,并获得了使用特定输入查找推文的脚本,目前,这是硬编码的.我希望用户输入主题标签或类似标签,然后在提交时运行脚本,并将其作为参数
I'm creating a web app in Python/Flask to display tweets using twitters API using tweepy. I have set up an HTML form, and have got the script that finds the tweets with a certain input, currently, this is hard coded. I want the users to input a hashtag or similar, and then on submit, the script to run, with that as its parameter
我已经创建了一个表单,其中包含GET方法,并且操作是要运行脚本.
I've created a form, with method GET and action is to run the script.
<form class="userinput" method="GET" action="Tweepy.py">
<input type="text" placeholder="Search..">
<button type="submit"></button>
</form>
我不知道如何获取用户输入并将其存储在变量中以用于扭曲代码,将不胜感激
I dont know how I would get the users input and store it in a variable to use for the tweepy code, any help would be greatly appreciated
推荐答案
templates/index.html
<form method="POST">
<input name="variable">
<input type="submit">
</form>
app.py
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/')
def my_form():
return render_template('index.html')
@app.route('/', methods=['POST'])
def my_form_post():
variable = request.form['variable']
return variable
这篇关于将来自HTML表单的用户输入作为Python脚本的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!