【发布时间】: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