- 安装coreapi(pip install coreapi)
- 在urls添加(分发接口)
from rest_framework.documentation import include_docs_urls
urlpatterns = [
...
url('docs/', include_docs_urls(title='接口文档'))
]
- 在settings.py添加一下内容(获取app为rest_framework的数据和DEFAULT_SCHEMA_CLASS将用于 schema 生成的视图检查类。)
···
INSTALLED_APPS = [
.....
'rest_framework',
]
.....
REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.AutoSchema',
}
··· - 另外还需要配置setting.py 配置
STATIC_URL = '/static/'
STATIC_ROOT = 'static'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
这三个参数作用:
STATIC_URL:django利用STATIC_URL来让浏览器可以直接访问静态文件;
STATIC_ROOT :运行python manage.py collectstatic ,django默认会去查看定义在STATICFILES_DIRS里的目录, 以及在INSTALLED_APPS里定义了的app的static目录;
STATICFILES_DIRS:告诉django,首先到STATICFILES_DIRS里面寻找静态文件,其次再到各个app的static文件夹里面找;
所以建议都配置static
需要现在项目里创建文件夹叫staic
-
运行python manage.py collectstatic
-
直接访问url:端口/static/rest_framework/css/bootstrap-theme.min.css
urls继续添加
from django.conf.urls import url
from django.views import static
from django.conf import settings
url(r'^static/(?P<path>.*)$', static.serve, {'document_root': settings.STATIC_ROOT}, name='static')
-
再次访问url:端口/static/rest_framework/css/bootstrap-theme.min.css 就可以了
10.访问接口文档url