• 大小: 71KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-28
  • 语言: Python
  • 标签:

资源简介

利用flask搭建的一个简单的个人博客网站

资源截图

代码片段和文件信息

#!/usr/bin/env python
#-*-coding:utf-8-*-

import sys
reload(sys)
sys.setdefaultencoding(‘utf-8‘) #解决在linux下编码错误的问题

from flask import Flaskrender_templateurl_forsessionredirectrequestflashabortMarkup
from werkzeug.security import check_password_hash
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
from flask_admin import Admin
from flask_admin.contrib import sqla
from flask_wtf import Form
import os
import hashlib
import functools
from datetime import datetime
‘‘‘导入支持markdown文本内容的相关库‘‘‘
from markdown import markdown
from markdown.extensions.codehilite import CodeHiliteExtension
from markdown.extensions.extra import ExtraExtension
from micawber import bootstrap_basic parse_html
from micawber.cache import Cache as OembedCache


‘‘‘配置内容‘‘‘
basedir=os.path.abspath(os.path.dirname(__file__))
#此处设置密码的hash值用于登陆,此处默认密码是‘123‘
#from werkzeug.security import generate_password_hash
#generate_password_hash(‘你的密码‘)
#将生成的值替换PASSWORD_HASH
PASSWORD_HASH = ‘pbkdf2:sha1:1000$80Oc5MyH$74a5c46815e27f6282b744c6590b012cf9f23b56‘
DEBUG=True
SECRET_KEY=“(\x8c\x9c\x1e\xe7y\x05\x98E4\x92\x12‘d\xd2\xc4\xcd\x8e3@\xd5\xc15“
SQLALCHEMY_DATAbase_URI=‘sqlite:///‘+os.path.join(basedir‘data.sqlite‘)
SQLALCHEMY_TRACK_MODIFICATIONS = True
# SQLALCHEMY_COMMIT_ON_TEARDOWN=True
SQLALCHEMY_ECHO=True
SITE_WIDTH = 800

‘‘‘配置注册‘‘‘
app=Flask(__name__)
app.config.from_object(__name__)
moment=Moment(app)
db=SQLAlchemy(app)
oembed_providers = bootstrap_basic(OembedCache())
admin=Admin(app name=‘博客后台管理‘)

‘‘‘错误页面配置‘‘‘
@app.errorhandler(404)
def page_not_found(e):
    return render_template(‘404.html‘)404

@app.errorhandler(500)
def internal_server_error(e):
    return render_template(‘500.html‘)500


‘‘‘登陆验证‘‘‘
def login_required(fn):
    @functools.wraps(fn)
    def inner(*args**kwargs):
        if session.get(‘logged_in‘):
            return fn(*args**kwargs)
        return redirect(url_for(‘login‘next=request.path))
    return inner


‘‘‘登陆视图函数‘‘‘
@app.route(‘/login‘methods=[‘POST‘‘GET‘])
def login():
    next_url=request.args.get(‘next‘) or request.form.get(‘next‘)
    if request.method == ‘POST‘ and request.form.get(‘password‘):
        if check_password_hash(app.config[‘PASSWORD_HASH‘]request.form.get(‘password‘)):
            session[‘logged_in‘]=True
            session.permanent = True  # Use cookie to store session.
            flash(‘你已经成功登陆.‘‘success‘)
            print(request.path)
            return redirect(next_url or url_for(‘index‘))
        flash(‘密码错误,请重新输入.‘‘danger‘)
    return render_template(‘login.html‘next=next_url)

‘‘‘登出视图函数‘‘‘
@app.route(‘/logout‘methods=[‘GET‘‘POST‘])
@login_required
def logout():
    if request.method == ‘POST‘:
        session.clear()
        flash(‘你已经成功登出本站.‘‘success‘)
        print(request.path)
        return redirect(url_for(‘index‘))
    return render_template(‘logout.html‘)

‘‘‘博文数据库模型设计‘‘‘
class Post(db.Model):
    __tablename__=‘posts‘
    id=db.Column(db.Integerprimary_key=True)
    tit

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-10-15 09:19  A-simple-Blog-master\
     文件         734  2016-10-15 09:19  A-simple-Blog-master\README.md
     目录           0  2016-10-15 09:19  A-simple-Blog-master\app\
     文件        9125  2016-10-15 09:19  A-simple-Blog-master\app\app.py
     文件         421  2016-10-15 09:19  A-simple-Blog-master\app\manage.py
     文件         104  2016-10-15 09:19  A-simple-Blog-master\app\requirement.txt
     目录           0  2016-10-15 09:19  A-simple-Blog-master\app\static\
     目录           0  2016-10-15 09:19  A-simple-Blog-master\app\static\css\
     文件      114011  2016-10-15 09:19  A-simple-Blog-master\app\static\css\bootstrap.min.css
     目录           0  2016-10-15 09:19  A-simple-Blog-master\app\static\js\
     文件       29110  2016-10-15 09:19  A-simple-Blog-master\app\static\js\bootstrap.min.js
     文件       96381  2016-10-15 09:19  A-simple-Blog-master\app\static\js\jquery-1.11.0.min.js
     目录           0  2016-10-15 09:19  A-simple-Blog-master\app\templates\
     文件         181  2016-10-15 09:19  A-simple-Blog-master\app\templates\404.html
     文件         176  2016-10-15 09:19  A-simple-Blog-master\app\templates\500.html
     文件        1162  2016-10-15 09:19  A-simple-Blog-master\app\templates\_macros.html
     文件        2687  2016-10-15 09:19  A-simple-Blog-master\app\templates\base.html
     文件        1725  2016-10-15 09:19  A-simple-Blog-master\app\templates\create.html
     文件         524  2016-10-15 09:19  A-simple-Blog-master\app\templates\detail.html
     文件        1880  2016-10-15 09:19  A-simple-Blog-master\app\templates\edit.html
     文件          94  2016-10-15 09:19  A-simple-Blog-master\app\templates\home.html
     文件         870  2016-10-15 09:19  A-simple-Blog-master\app\templates\index.html
     文件         686  2016-10-15 09:19  A-simple-Blog-master\app\templates\login.html
     文件         374  2016-10-15 09:19  A-simple-Blog-master\app\templates\logout.html
     文件         572  2016-10-15 09:19  A-simple-Blog-master\app\templates\posts.html

评论

共有 条评论