三小时学会flask web后端框架——登录注册页面设计,换元

1.搭建flask项目

from flask import Flask

app=Flask(__name__)

@app.route('/')
def index():

    return 'hello word'
if __name__ == '__main__':
    app.run()

2.搭建一个简单项目

1.在.py完成以下,导入render_remplate
from flask import Flask,render_template

app=Flask(__name__)

@app.route('/')
def index():

    return render_template('index.html')
if __name__ == '__main__':
    app.run()
2.在写index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>欢迎关注大白菜程序猿</h1>
    <h3>qq:1917687390</h3>
    <h5>创作不易请小可爱点个赞内容持续更新中</h5>
</body>
</html>

3.完成一个简单的登录注册页面

1 完成.py文件
from flask import Flask,render_template,request,redirect,session,url_for
app = Flask(__name__)
app.debug = True
app.secret_key = 'fdsafdsafasfsafsaf'

USERS = {
    1:{'name':'张三','age':18,'gender':'男','text':"道路千万条"},
    2:{'name':'李四','age':28,'gender':'男','text':"安全第一条"},
    3:{'name':'王五','age':18,'gender':'女','text':"行车不规范"},
}

@app.route('/detail/<int:nid>',methods=['GET'])
def detail(nid):
    user = session.get('user_info')
    if not user:
        return redirect('/login')

    info = USERS.get(nid)
    return render_template('detail.html',info=info)


@app.route('/index',methods=['GET'])
def index():
    user = session.get('user_info')
    if not user:
        # return redirect('/login')
        url = url_for('l1')
        return redirect(url)
    return render_template('index.html',user_dict=USERS)


@app.route('/login',methods=['GET','POST'],endpoint='l1')
def login():
    if request.method == "GET":
        return render_template('login.html')
    else:
        # request.query_string
        user = request.form.get('user')
        pwd = request.form.get('pwd')
        if user == 'athy' and pwd == '123':
            session['user_info'] = user
            #return  redirect('/index')
            return redirect('http://www.baidu.com')
        return render_template('login.html',error='用户名或密码错误')

if __name__ == '__main__':
    app.run()
2.前端页面
  • login.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>用户登录</h1>
    <form method="post">
        <input type="text" name="user">
        <input type="text" name="pwd">
        <input type="submit" value="登录">{{error}}
    </form>
</body>
</html>
  • index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>用户列表</h1>
    <table>
        {% for k,v in user_dict.items() %}
        <tr>
            <td>{{k}}</td>
            <td>{{v.name}}</td>
            <td>{{v['name']}}</td>
            <td>{{v.get('name')}}</td>
            <td><a href="/detail/{{k}}">查看详细</a></td>
        </tr>
        {% endfor %}
    </table>
</body>
</html>
  • 3detial.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>详细信息 {{info.name}}</h1>
    <div>
        {{info.text}}
    </div>
</body>
</html>

4.换源步骤

  • 阿里源
#阿里云
http://mirrors.aliyun.com/pypi/simple/ 
  • 其他源
#中国科技大学 
https://pypi.mirrors.ustc.edu.cn/simple/

#豆瓣(douban) 一般都换这个源
http://pypi.douban.com/simple/

#Python官方 
https://pypi.python.org/simple/

#v2ex 
http://pypi.v2ex.com/simple/

#中国科学院 
http://pypi.mirrors.opencas.cn/simple/

#清华大学 
https://pypi.tuna.tsinghua.edu.cn/simple/
  • 一次使用换源办法
#可以在使用pip的时候加参数-i 
https://pypi.tuna.tsinghua.edu.cn/simple
#例如:
pip install SomePackage -i https://pypi.tuna.tsinghua.edu.cn/simple
  • 永久换源
#Linux下,修改 ~/.pip/pip.conf (没有就创建一个), 修改 index-url至tuna,内容如下:
 [global]
 index-url = https://pypi.tuna.tsinghua.edu.cn/simple
___________________________________________________________
#windows下,直接在user目录中创建一个pip目录,如:C:\Users\xx\pip,新建文件pip.ini,内容如下:
 [global]
 index-url = https://pypi.tuna.tsinghua.edu.cn/simple

温馨提示:创作不易希望各位刷到的伙伴点赞关注以下

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大白菜程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值