【问题标题】:Flask render html using radio buttonFlask 使用单选按钮渲染 html
【发布时间】:2020-12-29 00:49:26
【问题描述】:

我是 flask 的新手,正在尝试做一些小的 poc uisng 单选按钮。请在下面帮助我。 建议我的方法是否错误

  1. 在主页 - 我有 3 个带有值的单选按钮(ENG、MEDICINE、MBA)
  2. 如果 MBA 是单选按钮被选中,我可以呈现 MBA.HTML(下一页),它又具有 3 个单选按钮(人力资源、财务、营销)。
  3. 关于选择 HR - 单选按钮无法呈现 HR html 页面,我应该在该页面中获取用户输入(显示 405 模块未找到)

以下代码有效,它呈现为 MBA html

@app.route('/', methods=['GET', 'POST'])
def home():
    if request.method == 'POST':
        selectedValue = request.form['r1']
        if selectedValue == 'MBA':
            return redirect(url_for('MBA'))
        if selectedValue == 'ENG':
            return redirect(url_for('ENG'))
        if selectedValue == 'MEDICINE':
            return redirect(url_for('MEDICINE'))
    return render_template('index.html')

--MBA 应用路由内部,检查 HR 中是否选中单选按钮,渲染为 HR html

@app.route('/MBA')
def MBA():
    return render_template('MBA_Home.html')
    if request.method == 'POST':
        aws_service = request.form['options']
        if aws_service == 'HR':
            return redirect(url_for('HR'))
home.html

<html>
<body>
<h2>
CHOOSE YOUR COURSE
</h2>
<font size= 4 face="verdana" color=#112244>
<form method = 'POST'>
<input type="radio" name="r1" value="MBA" >MBA<br>
<input type="radio" name="r1" value="ENG" >ENG<br>
<input type="radio" name="r1" value="MEDICINE" >MEDICINE<br>
<input type="submit" name="b1" value="Continue">
</form>
</body>
</html>
MBA.html:
<html>
<body>
<h2>
CHOOSE YOUR STREAM
</h2>
<font size= 4 face="verdana" color=#112244>
<form method = 'POST'>
<input type="radio" name="options" id="HR" value="HR"> HR </input><br>
<input type="radio" name="options" id="FINANCE" value="FINANCE"> FINANCE </input><br>
<input type="radio" name="options" id="MARKETING" value="MARKETING"> MARKETING </input><br>
<input type="submit" name="b1" value="Continue">

【问题讨论】:

  • 您有HR 的路线吗?在您的示例中,您有 MBA 路线,但没有 HR 路线。

标签: html forms flask


【解决方案1】:

首先;您没有为您的 MBA 路线指定方法,就像 home() 函数一样,您需要指定您的方法:

@users.route('/MBA', methods=['GET', 'POST'])
def MBA():

之后,您需要在函数结束时返回您的渲染模板:

def MBA():
    if request.method == 'POST':
        aws_service = request.form['options']
        if aws_service == 'HR':
            return redirect(url_for('HR'))
    return render_template('MBA.html')

最后,我们看不到您的 HR 功能,能否请您也编写该功能以便我可以帮助您?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-06
    • 2017-10-23
    • 2018-12-28
    • 2021-06-12
    相关资源
    最近更新 更多