ImportError: cannot import name 'url_decode' from 'werkzeug.urls'
回答 3
浏览 2555
2023-10-02
我正在使用 Flask 构建一个网络应用程序。我导入了flask-login
库来处理用户登录。但它显示导入错误。
下面是我的文件夹结构:
>flask_blog1
>flaskblog
>static
>templates
>__init__.py
>forms.py
>models.py
>routes.py
>instance
>site.db
>venv
>requirements.txt
>run.py
我的run.py
:
from flaskblog import app
if __name__ == "__main__":
app.run(debug=True)
我的__init__.py
:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
app = Flask(__name__)
app.config["SECRET_KEY"] = "5791628bb0b13ce0c676dfde280ba245"
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///site.db"
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager = LoginManager(app)
from flaskblog import routes
我的models.py
:
from datetime import datetime
# from .extensions import db
from flaskblog import db, login_manager
from flask_login import UserMixin
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
image_file = db.Column(db.String(20), nullable=False, default="default.jpg")
password = db.Column(db.String(60), nullable=False)
posts = db.relationship("Post", backref="author", lazy=True)
def __repr__(self):
return f"User('{self.username}', '{self.email}', '{self.image_file}')"
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
content = db.Column(db.Text, nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
def __repr__(self):
return f"Post('{self.title}', '{self.date_posted}')"
我的routes.py
:
from flask import render_template, flash, redirect, url_for
from flaskblog import app, db, bcrypt
from flaskblog.forms import RegistrationForm, LoginForm
from flaskblog.models import User, Post
from flask_login import login_user
posts = [
{
"author": "Ashutosh Chapagain",
"title": "Blog Post 1",
"content": "First Post Content",
"date_posted": "October 1, 2023",
},
{
"author": "Ash Dhakal",
"title": "Blog Post 2",
"content": "Second Post Content",
"date_posted": "October 2, 2023",
},
]
@app.route("/")
@app.route("/home")
def home():
return render_template("home.html", posts=posts)
@app.route("/about")
def about():
return render_template("about.html", title="About")
@app.route("/register", methods=["GET", "POST"])
def register():
form = RegistrationForm()
if form.validate_on_submit():
hashed_password = bcrypt.generate_password_hash(form.password.data).decode(
"utf-8"
)
user = User(
username=form.username.data, email=form.email.data, password=hashed_password
)
db.session.add(user)
db.session.commit()
flash(f"Your account has been created! You are now able to log in!", "success")
return redirect(url_for("login"))
return render_template("register.html", title="Register", form=form)
@app.route("/login", methods=["GET", "POST"])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user and bcrypt.check_password_hash(user.password, form.password.data):
login_user(user, remember=form.remember.data)
return redirect(url_for("home"))
else:
flash("Login Unsuccessful. Please check email and password", "danger")
return render_template("login.html", title="Login", form=form)
我的forms.py
:
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField, BooleanField
from wtforms.validators import DataRequired, Length, Email, EqualTo, ValidationError
from flaskblog.models import User
class RegistrationForm(FlaskForm):
username = StringField(
"Username", validators=[DataRequired(), Length(min=2, max=20)]
)
email = StringField("Email", validators=[DataRequired(), Email()])
password = PasswordField("Password", validators=[DataRequired()])
confirm_password = PasswordField(
"Confirm Password", validators=[DataRequired(), EqualTo("password")]
)
submit = SubmitField("Sign Up")
def validate_username(self, username):
user = User.query.filter_by(username=username.data).first()
if user:
raise ValidationError(
"That username is taken. Please choose a different one."
)
def validate_email(self, email):
user = User.query.filter_by(email=email.data).first()
if user:
raise ValidationError("That email is taken. Please choose a different one.")
class LoginForm(FlaskForm):
email = StringField("Email", validators=[DataRequired(), Email()])
password = PasswordField("Password", validators=[DataRequired()])
remember = BooleanField("Remember Me")
submit = SubmitField("Login")
确切的错误是:
(venv) asu@asu-Lenovo-Legion-5-15ARH05:/media/asu/Data/Projects/flask_blog1$ python3 run.py
Traceback (most recent call last):
File "/media/asu/Data/Projects/flask_blog1/run.py", line 1, in <module>
from flaskblog import app
File "/media/asu/Data/Projects/flask_blog1/flaskblog/__init__.py", line 4, in <module>
from flask_login import LoginManager
File "/media/asu/Data/Projects/flask_blog1/venv/lib/python3.10/site-packages/flask_login/__init__.py", line 12, in <module>
from .login_manager import LoginManager
File "/media/asu/Data/Projects/flask_blog1/venv/lib/python3.10/site-packages/flask_login/login_manager.py", line 33, in <module>
from .utils import _create_identifier
File "/media/asu/Data/Projects/flask_blog1/venv/lib/python3.10/site-packages/flask_login/utils.py", line 14, in <module>
from werkzeug.urls import url_decode
ImportError: cannot import name 'url_decode' from 'werkzeug.urls' (/media/asu/Data/Projects/flask_blog1/venv/lib/python3.10/site-packages/werkzeug/urls.py)
3 个回答
#1楼
已采纳
得票数 11
我只能假设您获得了 Werkzeug 3.0 更新(因为 Flask-login 没有上绑定其 werkzeug 依赖项)。
在不断删除 werkzeug 的所有非核心公共 API 的过程中,开发人员在Werkzeug 2.3(2023 年 4 月 25 日发布)中弃用了大多数werkzeug.urls
,并在 Werkzeug 3.0(2023 年 9 月 30 日发布)中删除了它。
您的选择是:
- 强制werkzeug为 3.0 之前的版本
- 等待 Flask-login 发布与 werkzeug 3 兼容的版本,修复了该问题以及一堆其他内容几分钟前合并
我强迫 werkzeug 到 2.3 版本,它就成功了。
- Culpepper 2023-10-02
您可以尝试直接使用开发存储库中的版本:
pip install git+https://github.com/maxcountryman/flask-login.git
- 最后提交消息是:“flask 3.0 compatibility”。
- Manuel 2023-10-08
git+https://github.com/maxcountryman/flask-login.git@7d98a49bc38d0849367b348bfe37a2b689323419
- Manuel 2023-10-08
#2楼
得票数 4
该问题如@Masklinn 所描述。我通过更改requirements.py
中的werkzeug和flask的版本并重新安装来解决了这个问题。
我原来的requirements.txt
:
bcrypt==4.0.1
blinker==1.6.2
click==8.1.7
dnspython==2.4.2
email-validator==2.0.0.post2
Flask==3.0.0
Flask-Bcrypt==1.0.1
Flask-Login==0.6.2
Flask-SQLAlchemy==3.1.1
Flask-WTF==1.2.0
greenlet==2.0.2
idna==3.4
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
SQLAlchemy==2.0.21
typing_extensions==4.8.0
Werkzeug==3.0.0
WTForms==3.0.1
我修改后的requirements.txt
:
bcrypt==4.0.1
blinker==1.6.2
click==8.1.7
dnspython==2.4.2
email-validator==2.0.0.post2
Flask==2.3.0
Flask-Bcrypt==1.0.1
Flask-Login==0.6.2
Flask-SQLAlchemy==3.1.1
Flask-WTF==1.2.1
greenlet==2.0.2
idna==3.4
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.3
SQLAlchemy==2.0.21
typing_extensions==4.8.0
Werkzeug==2.3.0
WTForms==3.0.1
#3楼
得票数 -2
我今天也遇到同样的错误,好像这些功能不再存在了
这篇文章属于评论部分。
- Karl-Marx 2023-10-04