求助大佬帮忙看看问题出现在哪里,目前我在login页面登录后反馈给Login Success,但是base.html却没有处理current_user.is_authenticated条件下的相关内容
以下是我的相关源码(可能,不太确定是不是这些相关,球球大佬捏 )
# __init__.py
import os
import sys
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
app = Flask(__name__)
app.config['SECRET_KEY'] = 'yzmyyds'
prefix = 'sqlite:///' if sys.platform.startswith("win") else 'sqlite:////'
app.config["SQLALCHEMY_DATABASE_URI"] = prefix + os.path.join(os.path.dirname(app.root_path), "data.db")
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False # 关闭对模型修改的监控
db = SQLAlchemy(app)
login_manager = LoginManager(app)
@login_manager.user_loader
def load_user(user_id):
from watchlist.models import User
user = User.query.get(int(user_id))
return dict(user=user)
login_manager.login_view = 'login'
@app.context_processor
def inject_user():
from watchlist.models import User
user = User.query.first()
return dict(user=user)
from watchlist import views, errors, commands
# views.py
from watchlist import app, db
from watchlist.models import User, Movie
from flask import url_for, render_template, redirect, flash, request
from flask_login import login_user, login_required, logout_user, current_user
from markupsafe import escape
@app.context_processor
def inject_user():
from watchlist.models import User
user = User.query.first()
return dict(user=user)
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
title = request.form.get("title")
year = request.form.get("year")
if not title or not year or len(year) > 4 or len(title) > 60:
flash("Invalid Input.")
return redirect(url_for("index"))
movie = Movie(title=title, year=year)
db.session.add(movie)
db.session.commit()
flash("Item created.")
return redirect(url_for("index"))
movies = Movie.query.all()
return render_template("index.html", movies=movies)
@app.route("/login", methods=["GET", "POST"])
def login():
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
if not username or not password:
flash("Invalid Input")
return redirect(url_for("login"))
user = User.query.first()
if username == username and user.validate_password(password):
login_user(user)
flash("Login Success")
return redirect(url_for("index"))
flash("Invalid username or password")
return redirect(url_for("login"))
return render_template("login.html")
@app.route("/logout")
@login_required
def logout():
logout_user()
flash("Goodbye")
return redirect(url_for("index"))
@app.route("/settings", methods=["GET", "POST"])
@login_required
def settings():
if request.method == "POST":
name = request.form["name"]
if not name or len(name) > 20:
flash("Invalid Input")
return redirect(url_for("settings"))
current_user.name = name
db.session.commit()
flash("Setting Updated")
return redirect(url_for("index"))
return render_template("settings.html")
@app.route("/user/<name>")
def user_page(name):
return f"Wlcome to {escape(name)}"
@app.route("/test")
def test_url_for():
print(url_for("hello_world"))
print(url_for("user_page", name="yzm"))
print(url_for("user_page", name="Alice"))
print(url_for("test_url_for"))
print(url_for("test_url_for", num=2))
print(url_for("static", filename="favicon.png"))
return "Test Page"
@app.route("/movie/edit/<int:movie_id>", methods=["GET", "POST"])
@login_required
def edit(movie_id):
movie = Movie.query.get_or_404(movie_id)
if request.method == "POST":
title = request.form["title"]
year = request.form["year"]
if not title or not year or len(year) != 4 or len(title) > 60:
flash("Invalid Input.")
return redirect(url_for("edit", movie_id=movie_id))
movie.title = title
movie.year = year
db.session.commit() # 更新
flash("Item Updated.")
return redirect(url_for("index"))
return render_template("edit.html", movie=movie)
@app.route("/movie/delete/<int:movie_id>", methods=["POST"]) # 限定只接受 POST 请求
@login_required
def delete(movie_id):
movie = Movie.query.get_or_404(movie_id)
db.session.delete(movie)
db.session.commit()
flash("Item Deleted.")
return redirect(url_for("index"))
对于html文件,代码都是原来的内容,封装成包后也没有更改,困扰好久了,求助