虚拟环境依赖
微信截图_20171205110941.png
- 11:10开始第六章
6-1
urls -views-serializers
6-3
微信截图_20171205133727.png
跨域的问题
- 安装
pip install django-cors-headers
- 配置setting的apps
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'DjangoUeditor',
'users.apps.UsersConfig',
'goods.apps.GoodsConfig',
'trade.apps.TradeConfig',
'user_operation.apps.UserOperationConfig',
'crispy_forms',
'django_filters',
'xadmin',
'rest_framework',
'corsheaders',
]
配置 'corsheaders.middleware.CorsMiddleware',这个配置要在前.
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'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',
]
CORS_ORIGIN_ALLOW_ALL = True
7-1 登录
- 设置setting
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
# 'DEFAULT_THROTTLE_CLASSES': (
# 'rest_framework.throttling.AnonRateThrottle',
# 'rest_framework.throttling.UserRateThrottle'
# ),
# 'DEFAULT_THROTTLE_RATES': {
# 'anon': '2/minute',
# 'user': '3/minute'
# }
}
- 添加rest_framework.authtoken到apps,特别注意逗号.
INSTALLED_APPS = [
'django.contrib.admin',
'rest_framework.authtoken',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'DjangoUeditor',
'users.apps.UsersConfig',
'goods.apps.GoodsConfig',
'trade.apps.TradeConfig',
'user_operation.apps.UserOperationConfig',
'crispy_forms',
'django_filters',
'xadmin',
'rest_framework',
'corsheaders',
]
- 需要生成表的,makemigrations,migrate.
- urls添加
url(r'^api-token-auth/', views.obtain_auth_token),
微信截图_20171205200208.png
再用token测试登录.
- setting
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.tokenAuthentication',
),
# 'DEFAULT_THROTTLE_CLASSES': (
# 'rest_framework.throttling.AnonRateThrottle',
# 'rest_framework.throttling.UserRateThrottle'
# ),
# 'DEFAULT_THROTTLE_RATES': {
# 'anon': '2/minute',
# 'user': '3/minute'
# }
}
微信截图_20171205202310.png
微信截图_20171205202340.png
微信截图_20171205204201.png
7-4 Json web token的原理
7-5 操作
- 虚拟环境安装jwt
pip install djangorestframework-jwt
- 设置
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
# 'rest_framework.authentication.TokenAuthentication',
),
}
- Urls配置
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
#drf自带
# url(r'^api-token-auth/', views.obtain_auth_token),
#jwt
url(r'^api-token-auth/', obtain_jwt_token),
成果: 用postman测试登录成功.
微信截图_20171205212821.png