我創建了一個使用資料庫進行簡單登錄的網站。我想要這樣,當您訪問主頁時,它會提示您注冊/登錄;并且當您登錄時,它會重定向到同一個主頁,但主頁不得顯示提示(提示您已登錄)并顯示僅對登錄用戶可用的內容。所以它的路由必須仍然是“/”。我試過這個:
<meta http-equiv="refresh" content="0; url={{ url_for('index', authenticated=1) }}">
在我的 Python 檔案中:
build = Flask(__name__)
@build.route('/')
def index(authenticated=0):
if authenticated == 0:
return render_template("index.html")
elif authenticated == 1:
return render_template("main.html")
我為什么要這樣?嗯.. 我不希望新用戶訪問該專屬鏈接。因此,除非有一種解決方法可以讓獨占鏈接檢測到新用戶是否未登錄,否則我不知道任何其他方法可以完成此操作
uj5u.com熱心網友回復:
您也可以通過創建兩個不同的路線來做到這一點。
舉個例子,這里的admin路由是針對登錄用戶的,home是針對普通用戶的。如果用戶通過身份驗證,它會重定向到adminfunc。
@app.route("/login", methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('admin'))
else:
return redirect(url_for('home'))
@app.route("/admin")
def admin():
return render_template('admin_home.html')
@app.route("/home")
def home():
return render_template('home.html')
uj5u.com熱心網友回復:
您必須像這樣使用重定向:
from flask import Flask, redirect, render_template
app = Flask(__name__)
@app.route("/")
def index():
if authenticated == 0:
return redirect('/login')
return render_template("index.html")
@app.route("/login")
def login():
if authenticated == 1:
return redirect('/')
return render_template("index.html")
uj5u.com熱心網友回復:
我解決了。對于正在閱讀本文的其他人,這是我的解決方案:首先,您需要匯入flask_login并安裝它,您可以通過pip install flask_login. 然后,在您的 python 檔案中,您需要匯入這些:
from flask_login import UserMixin, login_user, LoginManager, login_required, logout_user, current_user
之后,創建您的app并配置您的flask_login:
app= Flask(__name__)
db = SQLAlchemy(app) # this is my database file, if you've already created it than you can leave it out
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login' # REPLACE login WITH YOUR LOGIN ROUTE, BUT DO NOT INCLUDE A SLASH (/) AND MUST BE IN QUOTES
login_manager.login_message = "You must be logged-in in order to view this page." # OPTIONAL: A CUSTOM FLASH MESSAGE THAT WILL BE SHOWN WHEN A SIGNED-OUT USER TRIES TO ACCESS PAGES THAT REQUIRE A LOGIN. YOU CAN DELETE IT IF YOU WANT THE DEFAULT MESSAGE
@login_manager.user_loader
def user(id):
return Clients.query.get(int(id)) # REPLACE Clients WITH YOUR USER CLASS THAT IS USED TO AUTHENTICATE (NORMALLY IT INHERITS A DATABASE MODEL AND USERMIXIN)
最后,滾動到要限制的函式并應用@login_required裝飾器(從flask_login 匯入),它必須在@app.route()
@app.route('/exclusive_page_for_logged_in_users')
@login_required
def exclusive_page():
return render_template('exclusive_page.html')
并回答我原來的問題:
@build.route('/')
def index():
if current_user.is_authenticated:
return render_template("index.html")
else:
return render_template("authenticate.html")
你可以看到條件current_user.is_authenticated。不要更換任何東西,因為它本來就是這樣的。該條件檢查用戶是否已登錄,如果您未登錄,則加載authenticate.html而不是index.html保持相同的路線;因此你看不到@login_required裝飾器。有什么區別?好吧,當您使用裝飾器時,您不必傳遞任何引數。例如,我想將 a 傳遞FlaskForm給一個函式并讓它使用它。如果使用條件current_user.is_authenticated,則必須回傳類似 return render_template("authenticate.html", flaskform=FlaskForm, id=id, etc...)
現在,您如何登錄?您可以使用login_user(YOUR_USER_INFO_FROM_DB)
它非常簡單,但是您需要一個資料庫來執行此操作。你可以在你的資料庫中搜索資訊來驗證它們,我不會在這里包括。
pw_check_validation = Clients.query.filter_by(auth_name=auth_name).first()
login_user(pw_check_validation)
flash(f"Login successful.")
return redirect(url_for("index"))
希望這可以幫助任何有同樣問題的人!
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/388399.html
