当前位置:   article > 正文

Flask项目实战优化之【活动网站开发】_flask项目优化

flask项目优化

一,前言

之前的文章提到过现在需求。

1、活动需要手动定为历史活动,不要按照年份自动变为历史活动
2、同时多个活动开展上传

对于需求一,经过这几天的测试与改进已经是非常稳定了哈。
对于需求二,也已经基本解决了。下面来记录一下我遇到的疑难杂症以及解决良方:

二,问题一的良方

推进项目的过程中,我发现了应该严重的bug,就是评委比如说在文化比赛和计算机比赛的俩个比赛中。评委到评委入口的后台评选作品板块,的话默认是最新的一个比赛的信息。比如说先创建的文化比赛,再创建计算机比赛,这样的话评委到后台评选板块就会发现:只能评选计算机比赛的作品。
这是一个严重的bug。非常的困难,幸亏昨天2:00临睡觉想出了一种解决方案,不能说是好的解决方法,但是基本实现了我们的需求。

2.1,解决方法

我的解决方案是:
新建一个dbm数据库,名为:临时比赛标识,临时比赛名称。

我们访问的时候,用户获取比赛标识。存入dbm数据库,之后点击访问网站,传入目标参数。

db_config["lingshi_match"]=match
db_config["lingshi_match_name"]=match_name1
  • 1
  • 2

如上代码,我们实现了一种临时的效果。match和match_name1分别是比赛的标识和比赛的名称。

2.2,/admin思路设计

后端设计:

match=db_config["lingshi_match"].decode()
return render_template("admin/dashboard.html",match=match)
  • 1
  • 2

这部分简单哈,拿出来然后传入前端,都是套路。

前端设计:

<a href="/?match={{match}}">
 返回首页
  </a>
  • 1
  • 2
  • 3

前端写一个超链接,传入match临时存储的id值。

这部分实现了一个操作:
如果在在文化比赛和计算机比赛的俩个比赛中,某评委是从文化比赛进入后台的,依然可以点击首页返回文化比赛,而不是计算机比赛。

在这里插入图片描述

2.3,pingxuan_fenlei思路设计

这部分也是哈,为了解决这个困难,我写了如下代码(原理相同)。

 match_name=db_config["lingshi_match_name"].decode(), match=db_config["lingshi_match"].decode()
  • 1

这是传入的值,直接写入目标值。
在这里插入图片描述
完美的分开了。

前端相同操作套路。

{{leixing.val}}{{Post.query.filter_by(leixing=leixing.key,time=time,match=match).count()}}
  • 1

非常的巧妙。

2.X,前方高能;被大佬打开新思路

就在我写博客的时候,我向我的开发队友——郭大佬,提出了一个问题:

就是有一个需求:a用户访问网站得到值A,b用户访问网站得到值B,a进入一个模块传入参数A,b进入这个模块传入参数B,互不影响,保持一段时间即可

在这里插入图片描述

经过一系列与大佬的对话,打开了我优化的思路。我完全可以有sqlite来写呀,比dbm方便和强大多了。【等我有时间再优化吧,哈哈】。
大佬的CSDN博客链接——世界尽头与你
非常强的全能型选手哈!

三,问题二【\x00\x00\x00的大型bug解决】

一个非常神奇的现象哈,我在处理dbm的数据清洗的时候根本,没有这个bug。
在这里插入图片描述
如上代码块为清洗代码块。

with dbm.open('config', 'r') as db:
    print('keys():', db.keys())

    for k in db.keys():
        # for i in k :
        #     print(i)
        print(k, str(db[k],'utf-8'))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

我们遍历输出一下,发现还是非常正常的哈。
但是如果放到项目里就不行了,爆出一大堆的\x00\x00\x00,非常的难受哈,我明明已经转化格式了,但是还是如此。【特别说明,不是合适的问题,已经转过了,而且排查过好几次】
比如说

2021|包头市第二界文化旅游创意作品大赛|3\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
  • 1

这样的,我开始追寻他的原因,但是还是没有发现什么问题,但是,我有冒出了应该想法,既然不知道底层的bug原因,那么我可以在用户执行一个固定的操作,把x00过滤掉不就好了吗。

于是:

 db_config["match_url"] = db_config["match_url"].decode().strip(b'\x00'.decode())
  • 1

我悄悄的在def config_set():的开头加上这行代码。

这样我们发现,如果在部署初期,进入网站,封面会莫名其妙的消失,但是如何我们进入后台之后,他就会把X00搞掉,重新赋值。这样就永久的搞定了这个bug。

四,效果展示:

包头市文创比赛开发功能介绍

这次搞了一个视频上来哈。

五,重要源码展示:

config_set(): 部分如下:

def config_set():
    db_config["match_url"] = db_config["match_url"].decode().strip(b'\x00'.decode())
.......
  • 1
  • 2
  • 3

index(): 部分如下:

def index():
    post_count = Post.query.count()
    user_count = User.query.count()
    page = request.args.get('page', 1, type=int)
    match = request.args.get('match', db_config["match"].decode())
    is_search = False
    search_text = ""
    posts = Post.query
    if request.method == "POST":
        is_search = True
        query = request.form["query"]
        search_text = query
        search = "%{}%".format(query)
        posts = posts.filter(
            or_(
                Post.title.like(search),
                Post.text.like(search)
            ))

    posts = posts\
        .filter(Post.enabled == True).filter_by(match=match)\
        .order_by(Post.created.desc())\
        .paginate(page, app.config['POSTS_PER_PAGE'], False)
    paginates = posts.has_next or posts.has_prev
    next_url = url_for(
        'index', page=posts.next_num) if posts.has_next else None
    prev_url = url_for(
        'index', page=posts.prev_num) if posts.has_prev else None

    posts = False if posts.total == 0 else posts.items


    btn_vars = {}
    for key in db_config.keys():
        if key.decode().startswith('btn'):
            num = key.decode().split('_')[1]
            btn_vars[f"btn{num}_name"], btn_vars[f"btn{num}_url"] = db_config[key.decode(
            )].decode().split("|")
    try:
        user_scid = [score.post_id for score in current_user.scores]
    except:
        user_scid = 0

    past_url = db_config["match_url"].decode().split('|')
    #print(past_url[0])
    past_matchs = db_config["past_matchs"].decode().split(',')
    flag=1
    for match_tuple in past_matchs:
        if not match_tuple:
            continue
        match_mark = match_tuple.split("|")[0]
        match_name1=match_tuple.split("|")[1]
        if(match==match_mark):
            break
        flag=flag+1
    match_url=past_url[flag]
    data_cout= Post.query.filter_by(match=match).count()
    db_config["lingshi_match"]=match

    db_config["lingshi_match_name"]=match_name1
    return render_template(
        "index.html",
        sort='recent',
        user_count=user_count,
        posts=posts,
        paginates=paginates,
        next_url=next_url,
        prev_url=prev_url, match=match, is_search=is_search, search_text=search_text, **btn_vars,
        post_count=post_count,
        score_post_ids=user_scid,
        match_url=match_url,
        data_cout=data_cout,
        match_name1=match_name1
    ),match
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74

def admin(): 部分如下:

def admin():
    match=db_config["lingshi_match"].decode()
    return render_template("admin/dashboard.html",match=match)
  • 1
  • 2
  • 3

六,最后

传统Flask镇文!
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/51644
推荐阅读
相关标签
  

闽ICP备14008679号