Flask实战2问答平台-父模板抽离(登录注册界面)

我们写一个基模板,然后让其他的继承.
(1)新建基模板base.html
注意5,13,53行的{%%}块

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}{% endblock %}知了课堂</title>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <script src="https://cdn.bootcss.com/jquery/3.2.1/core.js"></script>

    <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="{{url_for('static',filename = 'css/index.css')}}">
    <link rel="stylesheet" href="{{url_for('static',filename = 'css/base.css')}}">
    {% block head %}{% endblock %}
</head>
<body>
    <nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">
        <img class="logo" src="{{url_for('static',filename = 'Logo/cat.png')}}" alt="">
      </a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">首页 <span class="sr-only">(current)</span></a></li>
        <li><a href="#">发布问答</a></li>

      </ul>
      <form class="navbar-form navbar-left">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="请输入关键字">
        </div>
        <button type="submit" class="btn btn-default">搜索</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="{{url_for('login')}}">登录</a></li>
         <li><a href="#">注册</a></li>

      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>
    <div class="main">
        {% block main %}{% endblock %}
    </div>

</body>
</html>

base.html

.logo{
    width: 30px;
    height: 30px;
}
body{
    background-color: #f3f3f3;
}
.main{
    background: #fff;
    width: 730px;
    margin: 0 auto;
    overflow: hidden;
}

(2)此时index.html可更改如下

{% extends 'base.html' %}

{% block title %} 首页 {% endblock %}

{% block main %} 这是首页 {% endblock %}

(3)login.html

{% extends 'base.html' %}

{% block head %}
<link rel="stylesheet" href="{{url_for('static',filename = 'css/login.css')}}">
{% endblock %}

{% block main %} 
    <h3 class="page-title">登录</h3>
    <form action="" method="post">
        <div class="form-container">
            <div class="form-group">
                <label for="exampleInputEmail1">用户名</label>
                <input type="email" class="form-control" id="exampleInputEmail1" placeholder="手机号码">
              </div>
              <div class="form-group">
                <label for="exampleInputPassword1">密码</label>
                <input type="password" class="form-control" id="exampleInputPassword1" placeholder="密码">
              </div>
              <!-- <div class="checkbox">
                <label>
                  <input type="checkbox"> Check me out
                </label>
              </div> -->
              <button type="submit" class="btn btn-primary btn-block">Submit</button>
              </div>
    </form>
{% endblock %}

index.css

.logo{
    width: 30px;
    height: 25px;
}

login.css

.form-container{
    width: 300px;
    /*将div form-container居中*/
    margin: 0 auto;

}
.page-title{
    text-align: center;
}
.main{
    height: 270px;
}

注册界面由登录界面作相应更改即可

{% extends 'base.html' %}

{% block title %}注册
{% endblock %}

{% block head %}
<link rel="stylesheet" href="{{url_for('static',filename = 'css/login-register.css')}}">
{% endblock %}

{% block main %}
<h3 class="page-title">注册</h3>
    <form action="" method="post">
        <div class="form-container">

            <div class="form-group">
                <label for="exampleInputEmail1">手机号码</label>
                <input type="email" class="form-control" name='username' placeholder="用户名">
              </div>

            <div class="form-group">
                <label for="exampleInputEmail1">用户名</label>
                <input type="text" class="form-control" name="telephone" placeholder="手机号码">
              </div>

             <div class="form-group">
                <label for="exampleInputPassword1">密码</label>
                <input type="password" class="form-control" name="password1" placeholder="确认密码">
              </div>
              <div class="form-group">
                <label for="exampleInputPassword1">确认密码</label>
                <input type="password" class="form-control" name="password2" placeholder="确认密码">
              </div>
              <!-- <div class="checkbox">
                <label>
                  <input type="checkbox"> Check me out
                </label>
              </div> -->
              <button type="submit" class="btn btn-primary btn-block">立即注册</button>
              </div>
    </form>
{% endblock %}

到这一步的时候,把login.css整合了一下,统一成了login-register.css
也就是login.html和register.html共用一个css.
login-register.css如下

.form-container{
    width: 300px;
    /*将div form-container居中*/
    margin: 0 auto;

}
.page-title{
    text-align: center;
}
.main{
    height: 400px;
}

不要忘记在主py文件中添加视图函数
最终效果

这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值