Exemple #1
0
def user_regist():
    form = RegistForm()
    if form.validate_on_submit():
        # 查看用户名是否已经存在
        user_name = form.user_name.data
        user_x = query_user_by_name(user_name)
        if user_x:
            flash("用户名已经存在!", category='err')
            return render_template('user_regist.html', form=form)
        # 如果用户不存在,执行注册
        user = User()
        user.name = form.user_name.data
        user.pwd = form.user_pwd.data
        user.email = form.data['user_email']
        user.age = form.user_edge.data
        user.birthday = form.data["user_birthday"]
        filestorage = request.files["user_face"]
        user.face = secure_filename_with_uuid(filestorage.filename)
        # 如果用户不存在,执行插入操作
        insert_user_to_db(user)
        # 保存用户头像文件
        user_folder = os.path.join(app.config["UPLOADS_FOLDER"], user.name)
        create_folder(user_folder)
        filestorage.save(os.path.join(user_folder, user.face))
        flash("用户注册成功!", category='ok')
        return redirect(url_for("user_login", username=user.name))
    return render_template('user_regist.html', form=form)
Exemple #2
0
def user_regist():  # 注册
    form = RegistForm()
    if form.validate_on_submit(
    ):  # 检查提交方式是否为post 验证forms.py定义的validators 验证是否通过
        # 检查用户上传的头像文件名是否符合要求
        if not check_files_extension([form.user_face.data.filename],
                                     ALLOWED_IMAGEEXTENSIONS):
            flash("头像文件格式错误!", category="err")
            return render_template("user_regist.html", form=form)
        # 查看用户是否存在
        user_name = form.user_name.data
        user_one = query_user_by_name(user_name)
        if user_one:
            # 返回注册界面,重新注册
            flash("用户名已存在!", category="err"
                  )  # Flashes a message to the next request 闪现一条消息到下一次消息请求

            return render_template("user_regist.html", form=form)

        # print("form", form.user_name.data)
        # print("form", form.data)
        # print("form", form.data["user_name"])
        # print("request.form", request.form)
        user = User()
        # user.name = request.form["user_name"]
        user.name = form.user_name.data
        # user.pwd = request.form["user_pwd"]
        user.pwd = form.user_pwd.data
        # user.age = request.form["user_age"]
        user.age = form.user_age.data
        # user.birthday = request.form["user_birthday"]
        user.birthday = form.user_birthday.data
        # user.email = request.form["user_email"]
        user.email = form.user_email.data
        # user.face = request.form["user_face"]
        # user.face = form.user_face.data
        # filerstorage=form.user_face.data
        filerstorage = request.files["user_face"]  # 获取头像文件
        user.face = secure_filename_with_uuid(
            filerstorage.filename
        )  # secure_filename 文件名安全性检测,如果文件名有特殊字符,会将特殊字符转义,没有就返回原文件名
        # print(user.face)

        # 如果不存在执行插入操作
        # 插入一条数据
        instert_user_to_db(user)
        # 保存用户头像文件
        user_folder = os.path.join(app.config["UPLOADS_FOLDER"], user.name)
        create_folder(user_folder)  # 创建用户文件夹
        filerstorage.save(os.path.join(user_folder, user.face))
        flash("注册成功!", category="ok")
        # username作为查询参数带到url中去
        # 重定向页面 生成url 执行 user_login 函数 跳转到登录界面
        return redirect(url_for("user_login", username=user.name))
    return render_template("user_regist.html", form=form)
Exemple #3
0
def register():
    form = RegisterForm()
    if form.validate_on_submit():

        # 检查用户上传的图像文件是否符合要求
        if not check_files_extension([form.photo.data.filename],
                                     ALLOWED_IMAGE_EXTENSION):
            flash('图片格式不正确!', 'danger')
            return redirect(url_for('auth.register'), form=form)

        user_name_db = User.query.filter(
            User.name == form.user_name.data).first()
        if user_name_db:
            flash('用户名已经存在!', 'danger')
            return render_template('register.html', form=form)
        user_email_db = User.query.filter(
            User.email == form.email.data).first()
        if user_email_db:
            flash('邮箱已经被注册过!', 'danger')
            return render_template('register.html', form=form)
        user_phone_db = User.query.filter(
            User.phone == form.phone.data).first()
        if user_phone_db:
            flash('手机号已经被注册过!', 'danger')
            return render_template('register.html', form=form)

        user = User()
        user.name = form.user_name.data
        user.pwd = generate_password_hash(form.user_pwd.data)
        user.email = form.email.data
        user.phone = form.phone.data
        user.introduce = form.introduce.data
        user.birthday = form.birthday.data
        # 文件上传
        # img_file = request.files['photo']
        img_file = request.files.get('photo')
        user.photo = secure_filename(img_file.filename)

        flash('注册成功', 'success')
        db.session.add(user)
        db.session.commit()
        # file_path = file_bastpath + img_file.filename
        user_folder = os.path.join(app.config['UPLOADED_FOLDER'], user.name)
        create_folder(user_folder)
        img_file.save(os.path.join(user_folder, user.photo))

        return redirect(url_for('auth.login', user_name=user.name))
    # user_name = form.data['user_name']
    return render_template('register.html', form=form)
Exemple #4
0
# app.register_blueprint(main_blueprint,static_folder='static',templates_folder='templates')
APPS_DIR = os.path.dirname(__file__)  # 当前工作空间的地址
STATIC_DIR = os.path.join(APPS_DIR, 'static')  # 静态文件的地址

app.config['UPLOADED_RELATIVE'] = 'uploads'  # 上传文件的目标文件夹
app.config['UPLOADED_FOLDER'] = os.path.join(STATIC_DIR,
                                             app.config['UPLOADED_RELATIVE'])

# 第一步:配置上传文件保存地址
app.config['UPLOADED_PHOTOS_DEST'] = app.config['UPLOADED_FOLDER']
# app.config['UPLOADED_MUSICS_DEST'] = app.config['UPLOADED_FOLDER']
# 创建文件夹
from apps.utils import create_folder

create_folder(app.config['UPLOADED_FOLDER'])

# manager = Manager(app)
# nav.register_element('top', Navbar('flask入门',
#                                    View('主页', 'home'),
#                                    View('关于', 'about'),
#                                    View('服务', 'services'),
#                                    View('项目', 'projects')))

db.init_app(app)
bootstrap.init_app(app)
# nav.init_app(app)
login_manager.init_app(app)
pagedown.init_app(app)

Gravatar(app, size=64)  # 默认图像大小
Exemple #5
0
    "SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:root@localhost/flasker"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True

db = SQLAlchemy(app)

APPS_DIR = os.path.dirname(__file__)
STATIC_DIR = os.path.join(APPS_DIR, 'static')
app.config["UPLOADS_RELATIVE"] = 'uploads'
app.config["UPLOADS_FOLDER"] = os.path.join(STATIC_DIR,
                                            app.config["UPLOADS_RELATIVE"])
# 第一步:配置 上传文件保存地址
app.config['UPLOADED_PHOTOS_DEST'] = app.config["UPLOADS_FOLDER"]
app.config['UPLOADED_IMGS_DEST'] = app.config["UPLOADS_FOLDER"]
app.config['UPLOADED_FILES_DEST'] = app.config["UPLOADS_FOLDER"]
app.config['UPLOADED_MUSICCOVERS_DEST'] = app.config["UPLOADS_FOLDER"]
app.config['UPLOADED_MUSICAUDIOS_DEST'] = app.config["UPLOADS_FOLDER"]
app.config['UPLOADED_MUSICLRCS_DEST'] = app.config["UPLOADS_FOLDER"]

create_folder(app.config["UPLOADS_FOLDER"])

from apps.admin import admin_bp
from apps.home import home_bp

app.register_blueprint(admin_bp, url_prefix='/admin')
app.register_blueprint(home_bp)


@app.errorhandler(404)
def page_not_found(error):
    return render_template('error/page_not_found.html'), 404
Exemple #6
0
app = Flask(__name__)
app.debug = True
app.config["SECRET_KEY"] = "who i am? do you know?"

# ./flask003/apps
APPS_DIR = os.path.dirname(__file__)  # os.path.dirname(__file__)为 当前文件__init__.py文件所在路径
# ./flask003/apps/static
STATIC_DIR = os.path.join(APPS_DIR, "static/")
# 数据库文件路径
app.config["DATABASE"] = os.path.join(APPS_DIR, "database.db")
app.config["UPLOADS_RELATIVE"] = "uploads"
# 上传文件存储路径路径
app.config["UPLOADS_FOLDER"] = os.path.join(STATIC_DIR, app.config["UPLOADS_RELATIVE"])
app.config["MAX_CONTENT_LENGTH"] = 2 * 1024 * 1024  # 限制上传文件大小

create_folder(app.config["UPLOADS_FOLDER"])  # 创建uploads

# app的工作目录
# print("__init__当前目录os:", os.getcwd())
# print("__init__当前目录 __file__:", __file__)
# print("__init__当前目录 os.path.dirname:", os.path.dirname(__file__))

# 防止循环导入报错  app 在导入views之前创建成功(app = Flask(__name__)),
# 才能在views.py模块导入app(from apps import app)时正常导入
import apps.views

# 当前文件作为执行文件(运行)的时候__name__才会等于:__main__
# 当前文件作为包的一个模块导入到文件的时候当前文件的__name__为包名(此为__name__== apps)
# 如:启动runserver.py文件的时候 当前的文件 print( __name__) 输出结果为: apps
# 单独启动__init__.py文件的时候 当前的文件 print( __name__) 输出结果为: __main__
# if __name__ == '__main__':