django5博客项目实战-用bootstrap5实现登录和注册页面

新建一个认证的app

python manage.py startapp myauth

在templates文件夹中创建login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我的博客</title>
    <link rel="stylesheet" href="{% static 'bootstrap5/bootstrap.min.css' %}">
    <script src="{% static 'bootstrap5/popper.min.js' %}"></script>
    <script src="{% static 'bootstrap5/bootstrap.min.js' %}"></script>
    <link rel="stylesheet" href="{% static 'css/base.css' %}">
</head>
<body>
<header class="p-3 text-bg-light border-bottom mb-3">
    <div class="container">
        <div class="d-flex flex-wrap align-items-center justify-content-center justify-content-lg-start">
            <a href="/" class="d-flex align-items-center mb-2 mb-lg-0 text-white text-decoration-none">
                <img src="{% static 'image/logo.png' %}" alt="" height="40">
            </a>

            <ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0">
                <li><a href="#" class="nav-link px-2 text-secondary">首页</a></li>
                <li><a href="#" class="nav-link px-2 text-secondary">发布博客</a></li>
            </ul>

            <form class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3" role="search">
                <input type="search" class="form-control" placeholder="搜索..." aria-label="Search">
            </form>

            <div class="text-end">
                <button type="button" class="btn btn-outline-primary me-2">登录</button>
                <button type="button" class="btn btn-primary">注册</button>
            </div>
        </div>
    </div>
</header>
<main class="container bg-white p-3 rounded">
    <div class="m-auto" style="max-width: 330px;">
        <h1>登录</h1>
        <form class="mt-3">
            <div class="mb-3">
                <label class="form-label">邮箱</label>
                <input type="email" name="email" placeholder="请输入邮箱" class='form-control'>
            </div>
            <div class="mb-3">
                <label class="form-label">密码</label>
                <input type="password" name="password" placeholder="请输入密码" class='form-control'>
            </div>
            <div class="form-check mb-3">
                <input class="form-check-input" type="checkbox" name='remember' value="1" id="flexCheckDefault">
                <label class="form-check-label" for="flexCheckDefault">
                    记住我
                </label>
            </div>
            <div class='mb-3'>
                <button class='btn btn-primary w-100'>登录</button>
            </div>
        </form>
    </div>
</main>
</body>
</html>

在templates文件夹中创建register.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我的博客</title>
    <link rel="stylesheet" href="{% static 'bootstrap5/bootstrap.min.css' %}">
    <script src="{% static 'bootstrap5/popper.min.js' %}"></script>
    <script src="{% static 'bootstrap5/bootstrap.min.js' %}"></script>
    <link rel="stylesheet" href="{% static 'css/base.css' %}">
</head>
<body>
<header class="p-3 text-bg-light border-bottom mb-3">
    <div class="container">
        <div class="d-flex flex-wrap align-items-center justify-content-center justify-content-lg-start">
            <a href="/" class="d-flex align-items-center mb-2 mb-lg-0 text-white text-decoration-none">
                <img src="{% static 'image/logo.png' %}" alt="" height="40">
            </a>

            <ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-center mb-md-0">
                <li><a href="#" class="nav-link px-2 text-secondary">首页</a></li>
                <li><a href="#" class="nav-link px-2 text-secondary">发布博客</a></li>
            </ul>

            <form class="col-12 col-lg-auto mb-3 mb-lg-0 me-lg-3" role="search">
                <input type="search" class="form-control" placeholder="搜索..." aria-label="Search">
            </form>

            <div class="text-end">
                <button type="button" class="btn btn-outline-primary me-2">登录</button>
                <button type="button" class="btn btn-primary">注册</button>
            </div>
        </div>
    </div>
</header>
<main class="container bg-white p-3 rounded">
    <div class="m-auto" style="max-width: 330px;">
        <h1>请注册</h1>
        <form class="mt-3">
            <div class="mb-3">
                <label class="form-label">用户名</label>
                <input type="text" name="username" placeholder="请输入用户名" class='form-control'>
            </div>
            <div class="mb-3">
                <label class="form-label">邮箱</label>
                <input type="email" name="email" placeholder="请输入邮箱" class='form-control'>
            </div>
            <div class="mb-3">
                <label class="form-label">验证码</label>
                <div class="input-group mb-3">
                    <input type="text" class="form-control" placeholder="验证码" aria-label="Recipient's username"
                           aria-describedby="button-addon2">
                    <button class="btn btn-outline-secondary" type="button">获取验证码</button>
                </div>
            </div>
            <div class="mb-3">
                <label class="form-label">密码</label>
                <input type="password" name="password" placeholder="请输入密码" class='form-control'>
            </div>
            <div class='mb-3'>
                <button class='btn btn-primary w-100'>注册</button>
            </div>
        </form>
    </div>
</main>
</body>
</html>

在myauth views.py里面创建登录和注册的视图函数

from django.shortcuts import render

def login(request):
    return render(request,'login.html')

def register(request):
    return render(request,'register.html')

在myauth里面创建urls.py

from django.urls import path
from . import views

app_name = 'myauth'

urlpatterns = [
    path('login',views.login,name='login'),
    path('register',views.register,name='register'),
]

在主urls.py中导入认证的urls

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('blog.urls')),
    path('auth/',include('myauth.urls'))
]

测试访问

http://127.0.0.1:8000/auth/login

http://127.0.0.1:8000/auth/register

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值