简体   繁体   English

从 html 中的 wtforms 获取 SelectField 中的数据

[英]Get the data from SelectField from wtforms in html

I would like to get the data from the SelectField in py:我想从 py 中的 SelectField 获取数据:

from budayaKB_model import BudayaItem, BudayaCollection
from flask import Flask, request, render_template, redirect, flash
from wtforms import Form, validators, TextField, SelectField

app = Flask(__name__)
budayaData = BudayaCollection()

class InputForm1(Form):
    Stats = SelectField(choices=[('All', 'All'), ('Tipe Budaya', 'Tipe Budaya'), ('Asal Provinsi Budaya', 'Asal Provinsi Budaya')], validators = [validators.InputRequired()])

@app.route('/statsBudaya', methods=['GET', 'POST'])
def statsBudaya():
    form = InputForm1(request.form)
    result = None
    if request.method == "GET":
        return render_template("statsBudaya.html", template_form=form)
    elif request.method == "POST" and form.validate():
        select = form.Stats.data
        if select == "All":
            result = budayaData.stat()
        elif select == "Tipe Budaya":
            result = budayaData.statByTipe()
        elif select == "Asal Provinsi Budaya":
            result = budayaData.statByProv()
    return render_template("statsBudaya.html", template_form=form, template_result=result, select=select)

I dont understand to create the "if" condition on situations like this.我不明白在这样的情况下创建“如果”条件。

my approach:我的方法:

<html>
<head>
    <title>BudayaKB Lite</title>
</head>

<body>
    {% include 'includes/_navbar.html' %}
    <h3>Kriteria Pencarian Statistik Data Budaya</h3>
    <form method=post action="">
        {{ template_form.Stats }}
        <p> <input type=submit value=Tampilkan> </p>
    </form>
    {% if select == "All": %}
    <p>Budaya KB memiliki total {{ template_result }}</p>
    {% elif select == "Tipe Budaya": %}
    <table>
    {% for key, value in result.iteritems() %}
        <tr>
            <th> {{key}} </th>
            <td> {{value}} </td>
        </tr>
    {% endfor %}
    </table>
    {% elif select == "Asal Provinsi Budaya": %}
    {% for key, value in result.iteritems() %}
        <tr>
            <th> {{key}} </th>
            <td> {{value}} </td>
        </tr>
    {% endfor %}
    {% endif %}

</body>
</html>

I can't understand how to get data that I want from the SelectField and some of my variables are in Indonesian.我不明白如何从 SelectField 获取我想要的数据,我的一些变量是印度尼西亚语。 But I hope you can understand my code and help me out.但我希望你能理解我的代码并帮助我。

Seems to be an error in Jinja if statement.似乎是Jinja if语句中的错误。 Here is your snippet:这是你的片段:

{% if select == "All": %}
<p>Budaya KB memiliki total {{ template_result }}</p>
{% elif select == "Tipe Budaya": %}
...
{% elif select == "Asal Provinsi Budaya": %}

Remove : after statements, so it should be look like: Remove : after 语句,所以它应该是这样的:

{% if select == "All" %}
<p>Budaya KB memiliki total {{ template_result }}</p>
{% elif select == "Tipe Budaya" %}
...
{% elif select == "Asal Provinsi Budaya" %}

About using if statements in Jinja (official documentation): https://jinja.palletsprojects.com/en/2.10.x/templates/#if关于在Jinja使用if语句(官方文档): https : //jinja.palletsprojects.com/en/2.10.x/templates/#if

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

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