Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何根据用户的输入使用带有if函数的flask重定向?_Python_Html_Flask_Redirect_Python Requests - Fatal编程技术网

Python 如何根据用户的输入使用带有if函数的flask重定向?

Python 如何根据用户的输入使用带有if函数的flask重定向?,python,html,flask,redirect,python-requests,Python,Html,Flask,Redirect,Python Requests,我对使用flask创建网站还不熟悉,所以我有点困惑。 基本上,在第一个HTML(website3.HTML)中,我打开了一个页面,用户可以选择选项1和选项2。 在html中,我使用了这部分代码: <form action="/redirect"> <label for="option">Choose an option:</label> <select id="options"

我对使用flask创建网站还不熟悉,所以我有点困惑。 基本上,在第一个HTML(website3.HTML)中,我打开了一个页面,用户可以选择选项1和选项2。 在html中,我使用了这部分代码:

  <form action="/redirect">
    <label for="option">Choose an option:</label>
    <select id="options" name="options">
      <option value="option1">Option 1</option>
      <option value="option2">Option 2</option>
    </select>
    <input type="submit">
  </form>

请求
不存在,我相信您的意思是
请求
,因此请确保导入它

from flask import Flask, render_template, url_for, request

@app.route('/redirect')
def options():
  option = request.args.get("options")
  if option == "option1": 
    return "<script>window.location.replace('/option1')</script>", 200
  elif option == "option2":
    return "<script>window.location.replace('/option2')</script>", 200
从烧瓶导入烧瓶,呈现模板,url,请求
@app.route(“/redirect”)
定义选项():
option=request.args.get(“选项”)
如果选项==“选项1”:
返回“window.location.replace('/option1')”,200
elif选项==“选项2”:
返回“window.location.replace('/option2')”,200
另一个问题是您的
选项
ifs之间有一个空格。所以我已经纠正了这一点。这应该行得通


不过这是一个客户端重定向
window.location.replace
用于在浏览浏览器历史记录或按浏览器上的“后退”按钮时隐藏/重定向,如果愿意,您可以使用
window.location=“

我实际上通过修复一些问题使其工作,下面是更新的代码:

from flask import Flask, render_template, url_for, redirect
from flask_ngrok import run_with_ngrok
from sqlalchemy import create_engine
from flask import request
import requests


app = Flask(__name__)
run_with_ngrok(app)

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


@app.route('/redirect')
def options():
  option = request.args.get("options")
  if option == "option1": 
    return redirect("/option1")
  elif option == "option2":
    return redirect("/option2")
   


@app.route('/option1')
def option1():
  return render_template('option1.html')

@app.route('/option2')
def option2():
  return render_template('option2.html')
app.run()

感谢您的帮助

非常感谢您的帮助,这很有效,我被告知有一种使用重定向的方法,您知道我怎么做吗?更改
return“window.location.replace('/option1')”,200
返回重定向(url\u for('option1'))
并对选项2执行相同的操作,替换url\u for()
from flask import Flask, render_template, url_for, redirect
from flask_ngrok import run_with_ngrok
from sqlalchemy import create_engine
from flask import request
import requests


app = Flask(__name__)
run_with_ngrok(app)

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


@app.route('/redirect')
def options():
  option = request.args.get("options")
  if option == "option1": 
    return redirect("/option1")
  elif option == "option2":
    return redirect("/option2")
   


@app.route('/option1')
def option1():
  return render_template('option1.html')

@app.route('/option2')
def option2():
  return render_template('option2.html')
app.run()