赞
踩
一、flask-login 用户管理
Login-Manager 实例创建好之后,并在工厂函数__init__.pfy文件中初始化:login_manager.init__app(app).
而Login-Manager.user_loader()装饰器放在了models.py数据库模型中,导致出现
login_manager.init_app(app)
from app.models import User from app.models import load_user @login_manager.user_loader def load_user(user_id): return User.query.get(int(user_id))
@main.route('/') def index(): return render_template('index.html',title='欢迎您')实在main蓝图下的,所以修改注册顺序后解决问题:
from app.auth import auth as auth_blueprint from app.main import main as main_blueprint app.register_blueprint(main_blueprint) app.register_blueprint(auth_blueprint,url_prefix='/auth')
- E:\Python代码\myproject\hometown>venv\Scripts\activate
- (venv) E:\Python代码\myproject\hometown>python manage.py shell
- C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\flask_sqlalchemy\__init__.py:839: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
- 'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
- C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\IPython\core\interactiveshell.py:714: UserWarning: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
- warn("Attempting to work in a virtualenv. If you encounter problems, please "
-
- In [1]: from app.model import User
-
- In [2]: from app import db
-
- In [3]: db.create_all()
-
- In [4]:
渲染出数据库中的图片,错误代码如下:
<div class="container"> {% for pic in pic_items %} <img src="{{ url_for('static',filename='{{ pic.pic_url }}') }}"/> {% endfor %} </div>所以我在view视图文件中,将所有的图片url链接先存放到列表中,再通过列表把图片url返回网页模板,最后渲染出来
@admin.route('/info_manage') def info_manage(): #将数据库中的图片信息按照id字段进行排序并全部查询,返回一个列表 piclist = Pics.query.order_by(Pics.id) src=[] #将查询到的所有图片链接保存到一个列表中 for p in piclist: src.append(url_for('static',filename=p.pic_url)) # 数据分页显示 page_index = request.args.get('page', 1, int) # 定义数据页,包括数据页和每一页的数据有几行 pagination = piclist.paginate(page_index, per_page=10, error_out=False) pics_items = pagination.items return render_template('admin/info_manage.html', pics_items=pics_items, pagination=pagination,src=src)网页模板中,这个时候的pic就是图片的真实url链接了
<div class="container"> {% for pic in pic_items %} <img src="{{ pic }}" style="width: 30px; height: 30px;"/> {% endfor %} </div>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。