#### 实现步骤
1. 项目根目录下创建python package
2. 自定义一个python文件
```
#coding=utf-8
from django.utils.deprecation import MiddlewareMixin
class Row1(MiddlewareMixin):
def process_request(self, request):
print("中间件1")
def process_response(self, request, response):
print("中间件1返回")
return response
# 参数里的 response :就是views里面返回的值,所以要继续返回一下,否则客户端收不到数据
class Row2(MiddlewareMixin):
def process_request(self, request):
print("中间件2")
def process_response(self, request, response):
print("中间件2返回")
return response
class Row3(MiddlewareMixin):
def process_request(self, request):
print("中间件3")
def process_response(self, request, response):
print("中间件3返回")
return response
```
3. settings文件中配置自定义中间件
```
MIDDLEWARE = [
‘django.middleware.security.SecurityMiddleware‘,
‘django.contrib.sessions.middleware.SessionMiddleware‘,
‘django.middleware.common.CommonMiddleware‘,
‘django.middleware.csrf.CsrfViewMiddleware‘,
‘django.contrib.auth.middleware.AuthenticationMiddleware‘,
‘django.contrib.messages.middleware.MessageMiddleware‘,
‘django.middleware.clickjacking.XFrameOptionsMiddleware‘,
‘middle.my.Row1‘,
‘middle.my.Row2‘,
‘middle.my.Row3‘,
]
```
4. 配置URL
```
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r‘^admin/‘, admin.site.urls),
url(r‘^student/‘, include(‘student.urls‘)),
]
#coding=utf-8
from django.conf.urls import url
import views
urlpatterns=[
url(r‘^$‘,views.IndexView.as_view()),
]
```
5.创建视图
```
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.views import View
# Create your views here.
class IndexView(View):
def get(self,request,*args,**kwargs):
print u‘最终返回值‘
return HttpResponse(‘OK‘)
```
6. 浏览器访问:http://127.0.0.1:8000/student/
7. 运行结果:
```
中间件1
中间件2
中间件3
最终返回值
中间件3返回
中间件2返回
中间件1返回
```
四、admin后台类
#### 配置模型类
```
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
# Create your models here.
from django.contrib.auth.models import User
from django.utils import timezone
class BlogArticles(models.Model):
title = models.CharField(max_length=300)
author = models.ForeignKey(User,related_name=‘blog_posts‘)
body = models.TextField()
publish = models.DateTimeField(default=timezone.now)
class Meta:
ordering = (‘-publish‘,)
def __unicode__(self):
return self.title
```
#### 配置admin后台类
```
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
# Register your models here.
from .models import BlogArticles
class BlogArticlesAdmin(admin.ModelAdmin):
#显示表格列表字段
list_display = (‘title‘,‘author‘,‘publish‘,)
#条件查询字段
list_filter = (‘publish‘,‘author‘,)
#搜索框中根据某些字段进行查询
search_fields = (‘title‘,‘body‘)
# 在admin后台类中加入raw_id_fields(只适用于外键)后,会显示外键的详细信息
raw_id_fields = ("author",)
#以某个日期字段分层次查询
date_hierarchy = ‘publish‘
#排序字段
ordering = [‘publish‘,‘author‘]
admin.site.register(BlogArticles,BlogArticlesAdmin)
```
hierarchy:层级
#### 终端创建超级用户
```
python manage.py createsuperuser
```
#### 浏览器访问
```
http://127.0.0.1:8000/admin
```