Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.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 将request.form值传递给url_以用于_Python_Html_Forms_Flask - Fatal编程技术网

Python 将request.form值传递给url_以用于

Python 将request.form值传递给url_以用于,python,html,forms,flask,Python,Html,Forms,Flask,我有一个烧瓶模板,显示一个页面,其中包含所有者的下拉列表,表格,以及所有者的输赢记录,还有一个电台,可以在常规赛季记录和季后赛记录之间切换 所需的工作流程是: 如果通过导航栏导航到该页面,则默认为/matchup history/regular。(本工程) 否则,只要切换收音机,它就会相应地进行布线。(这不起作用) 配对历史记录.html {%- extends "base.html" -%} {% block nav_matchups %}active{% endblock %} {%- bl

我有一个烧瓶模板,显示一个页面,其中包含
所有者的
下拉列表
表格
,以及所有者的输赢记录,还有一个
电台
,可以在
常规
赛季记录和
季后赛
记录之间切换

所需的工作流程是:

  • 如果通过导航栏导航到该页面,则默认为
    /matchup history/regular
    。(本工程)
  • 否则,只要切换
    收音机
    ,它就会相应地进行布线。(这不起作用)
  • 配对历史记录.html

    {%- extends "base.html" -%}
    {% block nav_matchups %}active{% endblock %}
    {%- block content -%}
      <form action="{{ url_for('show_matchup_history', matchup_type=request.form['matchup_type']) }}" method="post">
        <label>
          <select name="owner_id" onchange="this.form.submit()">
          {%- for o in owners %}
            {%- if request.form['owner_id'] == o['owner_id']|string() %}
            <option value="{{ o['owner_id'] }}" selected>{{o['first_name'] + " " + o['last_name'] }}</option>
            {%- else %}
            <option value="{{ o['owner_id'] }}">{{o['first_name'] + " " + o['last_name'] }}</option>
            {%- endif %}
          {%- endfor %}
          </select>
        </label>
        {% block matchup_type_radio %}{% endblock %}
      </form>
      {%- if records|length > 0 %}
      <div class="stats-table">
        <table>
          <tr>
            {%- for th in table_headers %}
            <th>{{ th }}</th>
            {%- endfor %}
          </tr>
          {%- for r in records %}
          <tr>
            {%- for cn in column_names %}
            <td>{{ r[cn] }}</td>
            {%- endfor %}
          </tr>
          {%- endfor %}
        </table>
      </div>
      {%- endif %}
    {% endblock -%}
    
    {%- extends "matchup-history.html" -%}
    {% block matchup_type_radio %}
    <label><input type="radio" name="matchup_type" value="regular" onclick="this.form.submit()" checked>Regular Season</label>
    <label><input type="radio" name="matchup_type" value="playoffs" onclick="this.form.submit()">Playoffs</label>
    {% endblock %}
    
    {%- extends "matchup-history.html" -%}
    {% block matchup_type_radio %}
    <label><input type="radio" name="matchup_type" value="regular" onclick="this.form.submit()">Regular Season</label>
    <label><input type="radio" name="matchup_type" value="playoffs" onclick="this.form.submit()" checked>Playoffs</label>
    {% endblock %}
    
    {%- extends "base.html" -%}
    {% block nav_matchups %}active{% endblock %}
    {%- block content -%}
      <form action="{{ url_for('show_matchup_history') }}" method="get">
        <label>
          <select name="owner_id" onchange="this.form.submit()">
          {%- for o in owners %}
            <option value="{{ o['owner_id'] }}" {%- if o['owner_id'] == selected_owner %} selected {% endif %}>{{o['first_name'] + " " + o['last_name'] }}</option>
          {%- endfor %}
          </select>
        </label>
        <label><input type="radio" name="matchup_type" value="regular" onclick="this.form.submit()" {%- if matchup_type == "regular" %} checked {% endif %}>Regular Season</label>
        <label><input type="radio" name="matchup_type" value="playoffs" onclick="this.form.submit()"{%- if matchup_type == "playoffs" %} checked {% endif %}>Playoffs</label>
      </form>
      {%- if records|length > 0 %}
      <div class="stats-table">
        <table>
          <tr>
            {%- for th in table_headers %}
            <th>{{ th }}</th>
            {%- endfor %}
          </tr>
          {%- for r in records %}
          <tr>
            {%- for cn in column_names %}
            <td>{{ r[cn] }}</td>
            {%- endfor %}
          </tr>
          {%- endfor %}
        </table>
      </div>
      {%- endif %}
    {% endblock -%}
    
    ...
    <a href="{{ url_for('show_matchup_history') }}" class="{% block nav_matchups %}{% endblock %}">Matchups</a>
    ...
    
    看起来像是
    request.form['matchup\u type']
    在呈现
    matchup history.html
    时是空的,因此提交表单将不会产生预期效果。如何重构以将
    url\u路由到不同的
    匹配类型

    编辑:根据佩尔的建议,我重新思考了设计

    配对历史记录.html

    {%- extends "base.html" -%}
    {% block nav_matchups %}active{% endblock %}
    {%- block content -%}
      <form action="{{ url_for('show_matchup_history', matchup_type=request.form['matchup_type']) }}" method="post">
        <label>
          <select name="owner_id" onchange="this.form.submit()">
          {%- for o in owners %}
            {%- if request.form['owner_id'] == o['owner_id']|string() %}
            <option value="{{ o['owner_id'] }}" selected>{{o['first_name'] + " " + o['last_name'] }}</option>
            {%- else %}
            <option value="{{ o['owner_id'] }}">{{o['first_name'] + " " + o['last_name'] }}</option>
            {%- endif %}
          {%- endfor %}
          </select>
        </label>
        {% block matchup_type_radio %}{% endblock %}
      </form>
      {%- if records|length > 0 %}
      <div class="stats-table">
        <table>
          <tr>
            {%- for th in table_headers %}
            <th>{{ th }}</th>
            {%- endfor %}
          </tr>
          {%- for r in records %}
          <tr>
            {%- for cn in column_names %}
            <td>{{ r[cn] }}</td>
            {%- endfor %}
          </tr>
          {%- endfor %}
        </table>
      </div>
      {%- endif %}
    {% endblock -%}
    
    {%- extends "matchup-history.html" -%}
    {% block matchup_type_radio %}
    <label><input type="radio" name="matchup_type" value="regular" onclick="this.form.submit()" checked>Regular Season</label>
    <label><input type="radio" name="matchup_type" value="playoffs" onclick="this.form.submit()">Playoffs</label>
    {% endblock %}
    
    {%- extends "matchup-history.html" -%}
    {% block matchup_type_radio %}
    <label><input type="radio" name="matchup_type" value="regular" onclick="this.form.submit()">Regular Season</label>
    <label><input type="radio" name="matchup_type" value="playoffs" onclick="this.form.submit()" checked>Playoffs</label>
    {% endblock %}
    
    {%- extends "base.html" -%}
    {% block nav_matchups %}active{% endblock %}
    {%- block content -%}
      <form action="{{ url_for('show_matchup_history') }}" method="get">
        <label>
          <select name="owner_id" onchange="this.form.submit()">
          {%- for o in owners %}
            <option value="{{ o['owner_id'] }}" {%- if o['owner_id'] == selected_owner %} selected {% endif %}>{{o['first_name'] + " " + o['last_name'] }}</option>
          {%- endfor %}
          </select>
        </label>
        <label><input type="radio" name="matchup_type" value="regular" onclick="this.form.submit()" {%- if matchup_type == "regular" %} checked {% endif %}>Regular Season</label>
        <label><input type="radio" name="matchup_type" value="playoffs" onclick="this.form.submit()"{%- if matchup_type == "playoffs" %} checked {% endif %}>Playoffs</label>
      </form>
      {%- if records|length > 0 %}
      <div class="stats-table">
        <table>
          <tr>
            {%- for th in table_headers %}
            <th>{{ th }}</th>
            {%- endfor %}
          </tr>
          {%- for r in records %}
          <tr>
            {%- for cn in column_names %}
            <td>{{ r[cn] }}</td>
            {%- endfor %}
          </tr>
          {%- endfor %}
        </table>
      </div>
      {%- endif %}
    {% endblock -%}
    
    ...
    <a href="{{ url_for('show_matchup_history') }}" class="{% block nav_matchups %}{% endblock %}">Matchups</a>
    ...
    
    现在的流量是:

  • 点击导航栏中的
    比赛
    ,将转到
    /matchup history
    ,默认显示常规赛比赛
  • 点击
    季后赛
    收音机将转到
    /matchup history?matchup\u type=季后赛&所有者\u id=12345
  • 单击
    Regular
    收音机将转到
    /matchup history?matchup\u type=Regular&owner\u id=12345
  • 单击
    下拉列表中的其他所有者
    将转到
    /matchup history?matchup\u type=regular&owner\u id=98765

  • 因此,现在您正试图在get请求中访问
    请求.form
    。但是,get请求中的
    表单
    将始终为空,因为这是get请求的性质。因此,只有通过post请求访问路由
    @app.route('/matchup history/'
    ),它才能以正确的方式重定向

    这个正在工作的小应用程序可以很好地显示它:

    from flask import Flask, render_template_string, request
    app = Flask(__name__)
    
    TEMPLATE_STRING = """
        <form action="{{ url_for('index') }}" method="post">
        {{request.form['matchup_type']}}<br><br>
        <label><input type="radio" name="matchup_type" value="regular" onclick="this.form.submit()" checked>Regular Season</label>
        <label><input type="radio" name="matchup_type" value="playoffs" onclick="this.form.submit()">Playoffs</label>
        </form>
    """
    
    
    @app.route('/', methods=['GET', 'POST'])
    def index():
        if request.method == 'GET':
            return render_template_string(TEMPLATE_STRING)
        else:
            return render_template_string(TEMPLATE_STRING)
    
    从烧瓶导入烧瓶,呈现模板字符串,请求
    app=烧瓶(名称)
    模板字符串=“”“
    {{request.form['matchip_type']}

    常规赛 季后赛 """ @app.route('/',方法=['GET','POST']) def index(): 如果request.method==“GET”: 返回渲染模板字符串(模板字符串) 其他: 返回渲染模板字符串(模板字符串)
    第一次打开页面时,您只能看到单选按钮。但只要单击单选按钮,它就会发布表单,因此您现在会在页面顶部看到所选值。如果再次单击,则会再次发布表单,以此类推

    那么你应该如何解决呢?我认为没有必要用这个表单进行POST请求,因为你没有更新任何数据,你只是在查询

        from flask import Flask, render_template_string, request
    app = Flask(__name__)
    
    TEMPLATE_STRING = """
        <form action="{{ url_for('history') }}" method="get">
        <select name="owner_id">
        {% for owner in owners %}
          <option {% if owner['id'] == selected_owner_id %} selected {% endif %}value="{{owner['id']}}">{{owner['name']}}</option>
         {% endfor %}
        </select>
        <label><input type="radio" name="matchup_type" value="regular" {%if selected_matchup_type == 'regular'%}checked{%endif%} onclick="this.form.submit()">Regular Season</label>
        <label><input type="radio" name="matchup_type" value="playoffs" {%if selected_matchup_type == 'playoffs'%}checked{%endif%} onclick="this.form.submit()"  >Playoffs</label>
        <br>Queried data goes here
        </form>
    """
    owners = [{'id': 1, 'name': 'bob'}, {'id': 2, 'name': 'gary'}, {'id': 3, 'name': 'tom'}]
    matchup_types = 'regular', 'playoffs'
    
    
    @app.route('/history', methods=['GET'])
    def history():
        owner_id = request.args.get('owner_id', None, type=int)
        if owner_id not in [owner['id'] for owner in owners]:
            owner_id = owners[0]['id']
        matchup_type = request.args.get('matchup_type', None)
        if matchup_type not in matchup_types:
            matchup_type = matchup_types[0]
        # now you know the owner_id and the matchup type, and know that both are valid, do some query to get table data
        return render_template_string(TEMPLATE_STRING, owners=owners,
                                      selected_owner_id=owner_id,
                                      selected_matchup_type=matchup_type,
                                      matchup_types=matchup_types)
    
    从烧瓶导入烧瓶,呈现模板字符串,请求
    app=烧瓶(名称)
    模板字符串=“”“
    {owners%%中所有者的百分比}
    {{owner['name']}
    {%endfor%}
    常规赛
    季后赛
    
    查询的数据在这里 """ 所有者=[{'id':1,'name':'bob'},{'id':2,'name':'gary'},{'id':3,'name':'tom'}] 比赛类型=‘常规赛’、‘季后赛’ @app.route('/history',methods=['GET']) def history(): owner\u id=request.args.get('owner\u id',None,type=int) 如果所有者id不在[所有者中的所有者的所有者['id']中]: 所有者\u id=所有者[0]['id'] matchup\u type=request.args.get('matchup\u type',None) 如果匹配类型不在匹配类型中: 匹配类型=匹配类型[0] #现在您知道了所有者id和匹配类型,并且知道它们都是有效的,请执行一些查询以获取表数据 返回渲染模板字符串(模板字符串,所有者=所有者, 选定的所有者id=所有者id, 所选匹配类型=匹配类型, 匹配类型=匹配类型)
    我想这正是您需要的。表单永远不会发布,而是始终作为get请求(
    )放置。如果值丢失或无效,我们将默认返回某个所有者/匹配类型。选中的值将被记住,并用于呈现模板

    这会将所有逻辑放在
    @app.route
    中,将所有jinja逻辑放在模板中

    一些一般性意见:

    我认为在jinja中访问
    请求
    并不可取,因为jinja处理错误/缺失值的方式不同,如果它们是与您的请求相关的逻辑结果,那么就很难猜测发生了什么。因此,在python端处理传入的请求

    不要根据选择的值包装两个无线模块,只需使用一个模块并在选项中检查是否与您需要的模块一致。
    blabla

    进行更多的输入验证!在第一个示例中,模板名称由某个用户输入的值(匹配类型)决定。但是如果用户发布了一个不存在的值怎么办?会出现错误


    如果两个模板之间的唯一区别是选择了哪个单选按钮,则您不需要两个模板。请参阅更新版本如何在一个模板中处理它。

    谢谢您的解释。现在,如果从导航栏单击,url将显示
    /matchup history?matchup\u type=regular
    。当单击单选按钮,url将变为
    /matchup history
    。如何扩展它,使url在切换时变为
    /matchup history/regular
    /matchup history/季后赛
    ?请参见编辑。我自己可能不会这样做,但它也可以工作。这仍然不会产生预期的效果。url即使选择了季后赛,仍然保持
    /matchup history/regular
    。再次编辑后,它现在可以工作了。但是,你不能再在其中传递变量了。老实说,整个设置都有很大的缺陷。或者使用我的第一个解决方案,它可以作为expec