我是靠谱客的博主 单身小熊猫,最近开发中收集的这篇文章主要介绍Python-Django 项目模块-使用自定义管理应用-登录(十二)Python-django 自定义模块开发前言 一、Django 创建用户应用程序 二、配置应用程序 三、配置路由地址 urls.py四、在对应的 school_web_users 应用中创建urls.py文件五、编写视图类方法 views.py六、对应的登录页面login.html,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
Python-django 自定义模块开发
第四章 Django 自定义模块-使用自定义用户管理模块进行应用管理
前言
本系列文章以一个简单的学校项目来做演示,项目中遇到的问题会一一记录下来,仅供学习参考使用
此处学习版本 python3.8 django 4.0.6 bootstrap3 开发工具 VSCODE
一、Django 创建用户应用程序
系统默认的管理路径为:http://192.168.10.128:8000/admin/ 你会看到自己的登录界面 需要输入账户密码
python .manage.py startapp school_web_users
二、配置应用程序
配置方法和业务模块一样的操作 将 此应用添加到 settings.py文件中
# Application definition
INSTALLED_APPS = [
## 引入自己的程序 放在最前面可以覆盖默认配置
"school_web_grade",
## 用户应用
"school_web_users",
'django.contrib.admin', ## 管理员站点
'django.contrib.auth', ## 认证授权系统
'django.contrib.contenttypes', ## 内容类型框架
'django.contrib.sessions', ## 会话框架
'django.contrib.messages', ## 消息框架
'django.contrib.staticfiles', ## 管理静态文件的框架
]
三、配置路由地址 urls.py
urlpatterns = [
## 用户应用
path('/', include('school_web_users.urls')),
## 默认管理
path('admin/', admin.site.urls),
## 引入子模块的路径
path('school/', include('school_web_grade.urls')),
]
四、在对应的 school_web_users 应用中创建urls.py文件
from django.urls import path
from django.urls import include
from . import views
## 模块名称 url命名空间
app_name = "school_web_users"
urlpatterns = [
## 默认打开浏览器访问地址跳转方法
path(route="", view=views.index),
## 配置默认的身份验证url
path(route="", view=include('django.contrib.auth.urls'),name="login"),
]
五、编写视图类方法 views.py
from django.shortcuts import render
from django.shortcuts import redirect
from django.views import generic
# Create your views here.
def index(reqeust):
# return render(reqeust,"index.html")
## 跳转到登录页
return redirect("school_web_users:login")
六、对应的登录页面login.html
{% load i18n static %}
<!DOCTYPE html>
<html>
<head>
<title>登录</title>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- 静态地址引入方式-->
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="/static/bootstrap-3.4.1/css/bootstrap.min.css" />
<!-- 程序引入方式 -->
<!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
<link rel="stylesheet" href="{% static 'bootstrap-3.4.1/css/bootstrap-theme.min.css' %}" />
<link rel="stylesheet" href="{% static 'laydate-v5.3.1/theme/default/laydate.css' %}" />
<script src="{% static 'js/jquery-3.2.1/jquery.min.js' %}"></script>
<script src="{% static 'js/popper-1.15.0/umd/popper.min.js' %}"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="{% static 'bootstrap-3.4.1/js/bootstrap.min.js' %}"></script>
<script src="{% static 'laydate-v5.3.1/laydate.js' %}"></script>
<!-- HTML5 shim 和 Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
<!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
<!-- HTML5 Shiv 和 Respond.js 用于让 IE8 支持 HTML5元素和媒体查询 -->
<!--[if lt IE 9]>
<script src="/static/js/html5shiv-3.7.0/html5shiv.js"></script>
<script src="/static/js/respond-1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container-fluid">
<!-- Stack the columns on mobile by making one full-width and the other half-width -->
<div class="row">
<!-- 导航栏固定在顶部-->
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container-fluid">
<!-- 左侧 -->
<div class="navbar-header">
<!--图标 -->
<a class="navbar-brand" href="{% url 'school_web_grade:index'%}" style="padding-top:5px;">
<img alt="Brand" src="/static/images/ico.png">
</a>
<a class="navbar-brand" href="{% url 'school_web_grade:index'%}">陕西家里蹲大学师生信息管理系统</a>
</div>
</div>
</nav>
</div>
<!-- Columns start at 50% wide on mobile and bump up to 33.3% wide on desktop -->
<div class="row" style="padding-top: 50px;padding-bottom: 50px;">
<div class="panel panel-info ">
<div class="panel-heading">系统登录</div>
<div class="panel-body ">
<!-- 居中 -->
<form class="form-horizontal " name="loginForm" id="loginForm" method="post" role="form" action="{% url 'school_web_users:login' %}">
{% csrf_token %}
<div class="form-group">
<label for="username" class="col-sm-4 control-label">登录名:</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="username" name="username" placeholder="请输入登录名">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-4 control-label">登录密码:</label>
<div class="col-sm-4">
<input type="password" class="form-control" id="password" name="password" placeholder="请输入密码">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-4 control-label"></label>
<div class="col-sm-4">
<button type="submit" class="btn btn-primary">登 录</button>
<!-- 此行配置 不可少 指定登录成功后跳转的页面 -->
<input type="hidden" name="next" value="{% url 'school_web_grade:index' %}" />
</div>
</div>
</form>
{% if form.errors %}
<div class="alert alert-danger">用户名或密码错误!</div>
{% endif %}
</div>
</div>
</div>
<div class="row navbar navbar-default navbar-fixed-bottom" style="text-align: center; height: 40px;">
<div class="bg-success" style="height: 100% ;padding-top: 15px;">
版权所有@copyRight 2013-2022 口袋里的小龙 开发框架 python3.8 bootstrap-3 Django4
</div>
</div>
</div>
</body>
</html>
注意: 需要注意的是要在页面指定跳转的方法 登录成功
最后
以上就是单身小熊猫为你收集整理的Python-Django 项目模块-使用自定义管理应用-登录(十二)Python-django 自定义模块开发前言 一、Django 创建用户应用程序 二、配置应用程序 三、配置路由地址 urls.py四、在对应的 school_web_users 应用中创建urls.py文件五、编写视图类方法 views.py六、对应的登录页面login.html的全部内容,希望文章能够帮你解决Python-Django 项目模块-使用自定义管理应用-登录(十二)Python-django 自定义模块开发前言 一、Django 创建用户应用程序 二、配置应用程序 三、配置路由地址 urls.py四、在对应的 school_web_users 应用中创建urls.py文件五、编写视图类方法 views.py六、对应的登录页面login.html所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复