gpt4 book ai didi

python - 从另一个文件导入方法的问题

转载 作者:可可西里 更新时间:2023-11-01 16:36:25 25 4
gpt4 key购买 nike

我正在尝试使用模型文件中的装饰器 requires_auth(f) 。 Docker-compose 日志说明如下:

   __import__(module)
wsgi_1 | File "/deploy/project/__init__.py", line 11, in <module>
wsgi_1 | @requires_auth
wsgi_1 | NameError: name 'requires_auth' is not defined

我似乎无法弄清楚为什么这不会构建。据我所知,我没有做任何会弄乱它的循环导入,因为我的其余代码构建良好。这是我的 init.py

from flask import Flask, Response

app = Flask(__name__)

from flask import Flask
from flask import render_template
from flask import request, Response
import models

@app.route('/secret-page', methods=['GET'])
@requires_auth
def secret_page():
users = models.retrieveUsers()
return render_template('secret.html' , users=users, current=current)

@app.route('/', methods=['POST', 'GET'])
def home():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
phone = request.form['phone']
models.insertUser(username, password, phone)
return render_template('index.html')
else:
return render_template('index.html')

这是我的 models.py

import sqlite3 as sql
from functools import wraps

q = """
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
password TEXT NOT NULL,
phone TEXT NOT NULL
);
"""

con = sql.connect("database.db")
cur = con.cursor()
cur.execute(q)

# original code from https://gist.github.com/PolBaladas/07bfcdefb5c1c57cdeb5


def insertUser(username, password, phone):
con = sql.connect("database.db")
cur = con.cursor()
cur.execute("INSERT INTO users (username,password,phone) VALUES (?,?,?)", (username,password,phone))
con.commit()
con.close()

def retrieveUsers():
con = sql.connect("database.db")
cur = con.cursor()
cur.execute("SELECT username, password, phone FROM users")
users = cur.fetchall()
con.close()
return users

def retrieveUser(username):
con = sql.connect("database.db")
cur = con.cursor()
cur.execute("SELECT username, password FROM users WHERE username = (?)", [username])
user = cur.fetchone()
con.close()
return user

def check_auth(username, password):
"""This function is called to check if a username /
password combination is valid.
"""
return username == 'admin' and password == 'password'

def authenticate():
"""Sends a 401 response that enables basic auth"""
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})

def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated

最佳答案

一个更 Flasky 的方法是使用 before_request装饰器。在你的情况下:

from models import requires_auth

@app.before_request
def before():
if request.path == url_for('secret-page'):
requires_auth()

编辑:我忘记使用 url_for。我应该know better .

关于python - 从另一个文件导入方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42775914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com