【问题标题】:AttributeError: 'function' object has no attribute 'method' for if request.method == "POST":AttributeError: 'function' 对象没有属性'method' for if request.method == "POST":
【发布时间】:2018-03-13 04:49:41
【问题描述】:

Running Flask 它给了我一条错误消息指向:

if request.method == "POST":

这是我的错误:

AttributeError: 'function' object has no attribute 'method'

我很困惑,因为我在另一个应用程序中使用了相同的代码并且它可以工作。我是一个完全的新手。

这是 app.py:

from __future__ import print_function
from flask import Flask, render_template, flash, request, url_for, redirect, session, g
import controllers
import config
from functools import wraps
import argparse
import json
import pprint
import requests
import sys
import urllib

# this client code can run on Python 2.x or 3.x.  Your imports can be
# simpler if you only need one of those.
try:
    # For Python 3.0 and later
    from urllib.error import HTTPError
    from urllib.parse import quote
    from urllib.parse import urlencode
except ImportError:
    # Fall back to Python 2's urllib2 and urllib
    from urllib2 import HTTPError
    from urllib import quote
    from urllib import urlencode

@app.route('/',  methods=["GET","POST"])
def main_route():
    if request.method == "POST":
        input_place = request.form['location']
        input_type = request.form['type']
        return render_template('index.html')
    return render_template('index.html')

这是我的 index.html:

<form class="form-inline" method="post">
  <input type="text" class="form-control" placeholder="Zip Code or City" name="location_zip" value="{{request.form.location}}">
  <input type="text" class="form-control" placeholder="Type of Establishment" name="type_place" value="{{request.form.type}}">
  <input class="btn btn-link" type="submit" value="Submit">
</form>

【问题讨论】:

  • 您是否在此文件旁边的另一个项目或模块中命名了函数request
  • 不——除了这个,还有大麦
  • 将您的代码更改为使用import flask,然后使用flask.whatever 以减少出错的可能性
  • 和/或将print(request.__module__) 作为main_route 的第一行。看看它是否输出除了 'werkzeug.local' 以外的任何东西。
  • 确定你没有做from requests import request

标签: python python-3.x methods flask


【解决方案1】:

flask.request 是未绑定的 LocalProxy 但 requests.request 是一个函数

【讨论】:

    猜你喜欢
    • 2016-03-10
    • 2022-01-26
    • 2019-05-17
    • 2017-07-04
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多