【问题标题】:Flask loop through POST Requests with input data烧瓶循环通过带有输入数据的 POST 请求
【发布时间】:2019-01-30 17:34:07
【问题描述】:

所以我创建了从计划中获取 json 数据并在烧瓶模板上使用 for 循环打印键值对的代码。对中的值位于文本字段中,您应该能够在提交时更新这些值。问题是我不知道如何循环完成日程表中的每一堂课,我只是设法完成了第一堂课。

这是我的表格

<form method="POST">
    <body>
    <h1>Uppdatera lektion</h1>
        {%for dict_item in update%}
        <br>
            {%for key, value in dict_item.items()%}
                <br>
                    <b>{{key}}</b>
                    <input type="text" name="values" value="{{value}}"/>
                </br>
            {%endfor%}
        </br>
        {%endfor%}
    <input type=submit value=Registrera>
    </body>
</form>

这是我的发帖方法

if request.method == "POST":
    input_values = request.form.getlist("values")

    url = "https://ltu.instructure.com/api/v1/calendar_events.json"

    payload = {
        'calendar_event[context_code]': "MY_USER",
        'calendar_event[title]': input_values[0],
        'calendar_event[start_at]': input_values[1]+"T"+input_values[2]+"Z",
        'calendar_event[end_at]': input_values[3]+"T"+input_values[4]+"Z",
        'calendar_event[description]': input_values[5]
        }

    headers = {
        'Authorization': "MY_TOKEN",
        'cache-control': "no-cache",
        'Postman-Token': "e0bc1a1c-5baf-47f8-a2ce-62d476040e73"
        }

    r = requests.post(url, data=payload, headers=headers)

    print(r.text)
return "Uppdateringen lyckades!"

这是带有键和值的讲座格式在浏览器上的样子

https://imgur.com/YDIMslw

所以,是的,我需要一些关于如何继续并能够更新所有讲座的建议,而不仅仅是第一个。提前致谢!

【问题讨论】:

  • 你应该看看基本的 HTML 格式。 form 中的 body 标签不好。检查:stackoverflow.com/questions/30976969/… 另外,对多个输入字段使用相同的名称看起来也不是很好,因为每个实现都以不同的方式解析它。
  • @spaniard 好的,谢谢,但是你对我的问题有什么建议吗?
  • 我在@user10712526 上发布了一些建议的答案,希望对您有所帮助。

标签: python html json post flask


【解决方案1】:

我认为您应该将[]添加到您的表单中的值,如下所示:

<input type="text" name="values[]" value="{{value}}"/>

【讨论】:

    【解决方案2】:

    正如我在评论中告诉您的,您不应该对多个 input 字段使用相同的属性 name。这使一切变得复杂,首先因为每个实现都以不同的方式解析它(没有标准),其次更重要的是,它使您的工作变得非常困难(我会说不可能,但可能会有一些我想念的东西)。

    你有什么区别input_value属于title和哪个description?我在您的代码中看到您使用排序,但我不确定订单是否保留。另外,你知道`dict_items的键

    我建议你使用这样的东西:

    {% for index, dict_item in enumerate(update) %}
    <br>
        <input type="hidden" name="item_count" value="{{ len(update) }}"/>
        {% for key, value in dict_item.items() %}
            <br>
                <b>{{key}}</b>
                <input type="text" name="{{key + index}}" value="{{value}}"/>
            </br>
        {% endfor %}
    </br>
    {% endfor %}
    

    然后将它们作为:

    item_count = request.form['item_count']
    
    for i in range(item_count):
    
        # key_0, key_1, key_2, etc... should be replace for the corresponding keys of dict_item 
        # that you use for build your form.
        # Dict are unordered, I used the numbers to reflect the previous order you were using.
        payload = {
            'calendar_event[context_code]': "MY_USER",
            'calendar_event[title]': input_values[key_0 + i],
            'calendar_event[start_at]': input_values[key_1 + i]+"T"+input_values[key_2 + i]+"Z",
            'calendar_event[end_at]': input_values[key_3 + i]+"T"+input_values[key_4 + i]+"Z",
            'calendar_event[description]': input_values[key_5 + i]
        }
    
        # Do with payload whatever you want to do.
    

    【讨论】:

      【解决方案3】:

      @spaniard 不幸的是,我无法在我的烧瓶模板中使用 python 方法,例如 enumerate 和 len。我搞砸了一点,想出了这个:

      {%for dict_item in update%}
          <br>
              {%for key, value in dict_item.items()%}
                  <br>
                      <b>{{key}}</b><input type="hidden" name="keys" value="{{key}}" />
                      <input type="text" name="values" value="{{value}}"/>
                  </br>
              {%endfor%}
          </br>
      {%endfor%}
      

      我在我的 python 代码中做了什么,用新的键值获取了两个列表,并用它们创建了一个新列表。

      input_values = request.form.getlist("values") item_count = request.form.getlist("keys") new_list = list(zip(item_count, input_values))

      不确定如何继续前进,有什么建议吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-25
        • 1970-01-01
        • 2018-06-15
        • 2018-03-06
        • 1970-01-01
        • 2014-07-23
        • 1970-01-01
        • 2014-05-21
        相关资源
        最近更新 更多