【问题标题】:(Python) Flask - why is request.args.get returning none type [duplicate](Python)Flask - 为什么request.args.get返回无类型[重复]
【发布时间】:2021-09-24 23:01:21
【问题描述】:

我无法弄清楚为什么“request.args.get”没有返回任何内容。我从表单中获取输入,当表单刷新时,我希望我的输入在那里,但它会返回“none”。

这两行代码是

student = request.args.get('name')
 grade = request.args.get('grade')

main.py:

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

@app.route('/info', method=['POST'])
def info():
    student = request.args.get('name')
    grade = request.args.get('grade')
    return render_template('results.html', studentid=studentid, student=student,grade=grade)

home.html

                <label for="name">Name:</label>
                <input type="text" class="form-control" id="name" name="student" placeholder="John Smith" style="width: 500px;" required>
            </div>
            <div class="form-group">
                <label for="fgrade">Final Grade:</label>
                <input type="text" class="form-control" id="fgrade" name="grade" placeholder="90" style="width: 500px;" required>
            </div>
            <button type="submit" class="btn btn-dark" value="Submit Form">Submit</button>
        </form>
        <br>
        

results.html

<body>
    
                <td>{{studentid}}</td>
                <td>{{student}}</td>
                <td>{{grade}}</td>
            

非常感谢任何人的意见或帮助。

【问题讨论】:

  • 请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange 网络上发帖,您已在 CC BY-SA 4.0 license 下授予 Stack Exchange 分发该内容的不可撤销的权利(即无论您未来的选择如何)。根据 Stack Exchange 政策,帖子的非破坏版本是分发的版本。因此,任何破坏行为都将被撤销。如果您想了解更多关于删除帖子的信息,请参阅:How does deleting work?
  • 你一再企图破坏你的帖子,我一再的逆转,系统会注意到,版主会收到警报。请 - 克制!
  • 我是一名大学学生,我正试图删除我自己的问题,因为我不想让其他人复制,这是一个错误,我想删除它,这样我的代码就不会被抄袭。
  • 我留下了足够的代码让任何人看问题并理解它,请就这样离开它。
  • 让你的问题保持原样,很好很清楚。您不能删除,因为只有一个答案,一票赞成,而且您不能破坏您的帖子。谢谢。

标签: python html forms flask request


【解决方案1】:

因为您正在从您的 html 页面 (method="POST") 发送 HTTP POST 请求,所以参数被插入到请求的正文中。要检索您需要使用的参数request.form.get("param_name")

【讨论】:

  • 感谢您的帮助,此建议和我的一个参数不正确。使用“request.form.get”并仔细检查我的参数是否正确解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-10
  • 2019-03-30
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多