高级应用
高级应用
一、Admin后台管理
1、概述
什么是admin?
admin是后台可视化界面管理系统 方便模型的操作与维护
2、配置
-
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
]
汉化
LANGUAGE_CODE = 'zh-Hans'TIME_ZONE = 'Asia/Shanghai'
-
创建用户进行访问
python manage.py createsuperuser
依次输入用户名 邮箱 密码 -
路由访问
127.0.0.1:8000/admin/
3、配置后台
班级与学生模型类
-
models.py
from django.db import models# 创建班级模型 class Grade(models.Model): gname = models.CharField(max_length=10, default='python36') gnum = models.SmallIntegerField(default=30) gboynum = models.SmallIntegerField(default=15) ggirlnum = models.SmallIntegerField(default=15) def __str__(self): return self.gname class Meta: db_table = 'grade' # 创建学生模型 class Student(models.Model): sname = models.CharField(max_length=10, default='张三') sage = models.SmallIntegerField(default=18) ssex = models.BooleanField(default=True) # 一对多外键关系 从表随着主表而删除 # sgrade = models.ForeignKey(Grade, on_delete=models.CASCADE) # sgrade = models.ForeignKey(Grade, on_delete=models.SET_NULL, null=True) # sgrade = models.ForeignKey(Grade, on_delete=models.PROTECT) sgrade = models.ForeignKey(Grade, on_delete=models.SET_DEFAULT, default=1) def __str__(self): return self.sname class Meta: db_table = 'student'
-
设置为中文显示(字段在后台进行中文显示)
class Grade(models.Model):
...
ggirlnum = models.SmallIntegerField(default=15, verbose_name='女生人数')
class Meta:
db_table = 'grade'
verbose_name = '班级'
class Student(models.Model):
... -
admin.py
from django.contrib import admin
from App.models import Grade, Student# class StudentInfo(admin.StackedInline): # 竖着 class StudentInfo(admin.TabularInline): # 横着 model = Student extra = 2 @admin.register(Grade) class GradeAdmin(admin.ModelAdmin): inlines = [StudentInfo] # 字段显示 list_display = ['pk', 'gname', 'gnum', 'gboynum', 'ggirlnum'] # 过滤字段 list_filter = ['gname'] # 搜索字段 search_fields = ['gname'] # 分页 list_per_page = 5 # 每页显示五条数据 # 更改添加修改样式 # fields = ['gname', 'gnum', 'gboynum', 'ggirlnum'] # 添加修改分组(fieldsets和fields不能同时存在) fieldsets = [ ('组一', {'fields':['gboynum', 'ggirlnum']}), ('组二', {'fields':['gname', 'gnum']}) ] @admin.register(Student) class StudentAdmin(admin.ModelAdmin): def show_sex(self): if self.ssex: return '男' else: return '女' show_sex.short_description = '性别' list_display = ['pk', 'sname', 'sage', show_sex, 'sgrade'] # Register your models here. # admin.site.register(Grade, GradeAdmin) # admin.site.register(Student, StudentAdmin)
二、邮件发送
https://docs.djangoproject.com/zh-hans/3.2/topics/email/#django.core.mail.send_mass_mail
1、配置
基础阶段里面的邮箱配置
2、发送邮件
def test_send_mail(req):
from django.core.mail import send_mail
send_mail(
'主体',
'邮件内容',
settings.EMAIL_HOST_USER,
['793390457@qq.com'],
fail_silently=False,
)
return HttpResponse('发送邮件')
3、群发
from django.core.mail import send_mass_mail
message1 = ('Subject here', 'Here is the message', 'from@example.com', ['first@example.com', 'other@example.com'])
message2 = ('Another Subject', 'Here is another message', 'from@example.com', ['second@test.com'])
send_mass_mail((message1, message2), fail_silently=False)
send_mass_mail()和send_mail()之间的主要区别在于,send_mail()每次执行时都会打开到邮件服务器的连接,而send_mass_mail()对其所有邮件使用单个连接。这使得send_mass_mail()的效率略微提高。
4、使用文本和html发送
from django.core.mail import EmailMultiAlternatives
subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
5、使用html发送
from django.core.mail import send_mass_mail, EmailMessage
msg = EmailMessage('主体', '<b>邮件内容</b>', settings.EMAIL_HOST_USER, ['793390457@qq.com','948807313@qq.com'])
msg.content_subtype = "html" # Main content is now text/html
msg.send()
三、分页
1、Paginator对象
- 创建对象
Paginator() - 属性
count 对象总数
num_pages 页面总数
page_range 页码列表 - 方法
page(num)
2、page对象
通过Paginator的方法page 得到page对象
- 属性
object_list 当前页上所有数据
number 当前页的页码值
paginator 获取paginator 对象 - 方法
has_next 是否有下一页
has_previous 是否有上一页
next_page_num 返回下一页页码
previous_page_num 返回上一页页码
3、实例
-
views.py
def index(req):
nowPage = req.GET.get('page',1)
stu = Student.objects.all()
p = Paginator(stu, 2)
# print(p)# 通过页码获取当前页的对象 try: # 判断页码是否合法 if int(nowPage) >= int(p.num_pages): nowPage = p.num_pages page = p.page(nowPage) except: page = p.page(p.num_pages) return render(req, 'index.html', {'page': page})
-
index.html
Title
首页<ol> {% for s in page.object_list %} <li>{{ s.sname }}</li> {% endfor %} </ol> <nav aria-label="Page navigation"> <ul class="pagination pagination-lg"> <li> <a {% if page.has_previous %}href="{% url 'App:index' %}?page={{ page.previous_page_number }}"{% endif %} aria-label="Previous"> <span aria-hidden="true">«</span> </a> </li> {% for index in page.paginator.page_range %} <li {% if index == page.number %}class="active"{% endif %}><a href="{% url 'App:index' %}?page={{ index }}">{{ index }}</a></li> {% endfor %} {# <li><a href="#">2</a></li>#} <li> <a {% if page.has_next %}href="{% url 'App:index' %}?page={{ page.next_page_number }}"{% endif %} aria-label="Next"> <span aria-hidden="true">»</span> </a> </li> </ul> </nav> </body> </html>
四、中间件
1、说明
中间件就是在请求与响应过程中 进行安全验证的功能 每个中间件组件都会负责做一些特定的功能
2、本质
python类
3、使用场景
- 白名单、黑名单
- url访问过滤
- 缓存
浏览器->中间件->url路由地址分发->中间件->视图函数->中间件->响应->
4、方法
- process_request(self, req)
在执行视图之前被调用(分配url匹配视图) 每个请求都会被调用 返回None或者response对象 - process_view(self, req, view_func, view_args, view_kwargs)
调用视图之前进行调用 每个表请求都会调用 返回None或者response对象 - process_response(self, req, response)
所有响应在返回浏览器之前都会被调用 返回response - process_exception(self, req, exception)
当视图抛出异常时进行调用
5、自定义中间件
-
App同级创建middleware文件夹
App/
middleware/
myMiddly.py -
myMiddly.py中创建MyMiddle类
from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse, redirectclass MyMiddle(MiddlewareMixin): def process_request(self, req): print(req.path) print(req.method) print(req.META['REMOTE_ADDR']) # if req.META['REMOTE_ADDR'] == '127.0.0.1': # return HttpResponse('您被拦截了') def process_exception(self, req, exception): print(exception) return redirect('/a/')
-
settings.py
middleware中添加
'middleware.myMiddle.MyMiddle',
本文来自博客园,作者:寻月隐君,转载请注明原文链接:https://www.cnblogs.com/QiaoPengjun/articles/15948858.html