- 安裝coreapi(pip install coreapi)
- 在urls添加(分發(fā)接口)
from rest_framework.documentation import include_docs_urls
urlpatterns = [
...
url('docs/', include_docs_urls(title='接口文檔'))
]
- 在settings.py添加一下內(nèi)容(獲取app為rest_framework的數(shù)據(jù)和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"),
]
這三個(gè)參數(shù)作用:
STATIC_URL:django利用STATIC_URL來讓瀏覽器可以直接訪問靜態(tài)文件逗柴;
STATIC_ROOT :運(yùn)行python manage.py collectstatic 蛹头,django默認(rèn)會(huì)去查看定義在STATICFILES_DIRS里的目錄, 以及在INSTALLED_APPS里定義了的app的static目錄;
STATICFILES_DIRS:告訴django,首先到STATICFILES_DIRS里面尋找靜態(tài)文件渣蜗,其次再到各個(gè)app的static文件夾里面找屠尊;
所以建議都配置static
需要現(xiàn)在項(xiàng)目里創(chuàng)建文件夾叫staic
-
運(yùn)行python manage.py collectstatic
-
直接訪問url:端口/static/rest_framework/css/bootstrap-theme.min.css
urls繼續(xù)添加
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