You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flask项目报错:AttributeError——‘Request’对象无‘methods’属性求助

解决Flask中AttributeError: 'Request' object has no attribute 'methods'的问题

嘿,我一眼就揪出问题啦!这个报错完全是个小拼写失误——你在代码里写了request.methods,但Flask的请求对象里对应的属性是单数的request.method!

错误原因解释

methods是你在路由装饰器@app.route('/add/', methods=['POST', 'GET'])里用来定义允许的HTTP请求方法的参数,而当前请求实际使用的方法是存在request.method(单数)这个属性里的,写成复数的methods自然会触发“属性不存在”的错误。

修正后的代码片段

@app.route('/add/', methods=['POST', 'GET'])
def add():
    if not session.get('logged_in'):
        return render_template('login.html')
    else:
        # 关键修正:把methods改成method
        if request.method == 'POST':
            nom = str(request.form.get('nom'))  # 假设你原本要获取表单字段
            # 后续的数据库交互逻辑...
        # GET请求时渲染表单页面的逻辑
        return render_template('add_form.html')

额外小检查

顺便确认下你已经正确导入了Flask的request对象:

from flask import Flask, request, session, render_template

内容的提问来源于stack exchange,提问作者Newbiedev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:59:16