当我尝试做这样的if语句时:
if request.method == "POST":
我收到此错误:
AttributeError: 'module' object has no attribute 'method'
完整追溯:
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\flask\app.py", line 2000, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python34\lib\site-packages\flask\app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python34\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "C:\Python34\lib\site-packages\flask\app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python34\lib\site-packages\flask\app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python34\lib\site-packages\flask\app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python34\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "C:\Python34\lib\site-packages\flask\app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Python34\lib\site-packages\flask\app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python34\lib\site-packages\flask\app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "K:\HinaCh4n-PenSuite\main.py", line 75, in login_page
raise e
File "K:\HinaCh4n-PenSuite\main.py", line 55, in login_page
if request.method == "POST":
AttributeError: 'module' object has no attribute 'method'
以下是我的导入:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from flask import Flask, request, render_template, flash, url_for, redirect, session
from flask_cors import CORS, cross_origin
from dbconnect import connection
from pymysql import escape_string as thwart
from passlib.hash import sha256_crypt
from functools import wraps
import random
from base64 import b64encode
from wtforms import Form, BooleanField, TextField, PasswordField, validators
import gc
from urllib import request
我的功能:
@app.route('/login', methods=['POST', 'GET'])
def login_page():
error = " "
try:
c, conn = connection()
if request.method == "POST":
data = c.execute("SELECT * FROM users WHERE username = (%s)", (thwart(request.form['username'])))
print(data)
if data < 1 :
return "0"
else:
data = c.fetchone()[2]
if sha256_crypt.verify(request.form['password'], data):
session['logged_in'] = True
session['username'] = request.form['username']
return "1"
else:
return "0"
gc.collect()
return render_template("login.html")
except Exception as e:
raise e
error = "Invalid credentials"
return "error"
答案 0 :(得分:2)
因为您要导入2个单独的request
s(来自flask和urllib),所以它无法正常工作。将urllib请求的导入更改为此,并将urllib请求的所有引用更新为request
至req
from urllib import request as req