簡體   English   中英

為什么新創建的項目不顯示內置API Django文檔?

[英]Why newly created project doesn't display built-in API Django documentation?

我在這篇文章中描述了一個問題,我不知道問題的原因是什么,所以我決定像沒有它一樣在Docker中創建一個新項目。 在這兩種情況下,我都會遇到相同的問題。 為了新創建的項目,我只添加到urls.py

from rest_framework.documentation import include_docs_urls

API_TITLE = 'API title'
API_DESCRIPTION = '...'

urlpatterns = [
    url(r'^docs/', include_docs_urls(title=API_TITLE, description=API_DESCRIPTION))
]

在Docker requirements.txt添加coreapi 在沒有Docker的項目中,我通過virtualenv pip3 install coreapi 我認為這就是所有工作,以便從文檔中獲得本網站頂部的視圖,顯然是在http://localhost:8000/docs/但是在兩種情況下,我都可以

位於/ docs /的TemplateDoesNotExist

rest_framework /文檔/ index.html中

Django嘗試按以下順序加載這些模板:

使用引擎django:

django.template.loaders.app_directories.Loader:/usr/local/lib/python3.6/site-packages/django/contrib/admin/templates/rest_framework/docs/index.html(源不存在)

django.template.loaders.app_directories.Loader:/usr/local/lib/python3.6/site-packages/django/contrib/auth/templates/rest_framework/docs/index.html(源不存在)

(在virtualenv中只有不同的路徑)

有人知道什么地方可能出問題嗎? 如果需要更多詳細信息,請立即將其添加到這篇文章中。

更新當我將rest_framowork添加到INSTALLED_APPS我得到:

AttributeError at /docs/
'NoneType' object has no attribute 'items'
Request Method: GET
Request URL:    http://localhost:8001/docs/
Django Version: 1.11
Exception Type: AttributeError
Exception Value:    
'NoneType' object has no attribute 'items'
Exception Location: /usr/local/lib/python3.6/site-packages/rest_framework/templatetags/rest_framework.py in items, line 244
Python Executable:  /usr/local/bin/python3
Python Version: 3.6.1
Python Path:    
['/code',
 '/usr/local/lib/python36.zip',
 '/usr/local/lib/python3.6',
 '/usr/local/lib/python3.6/lib-dynload',
 '/usr/local/lib/python3.6/site-packages']

在模板/Users/myUser/Projects/DjangoTest/lib/python3.6/site-packages/rest_framework/templates/rest_framework/docs/sidebar.html中,第8行錯誤

UPDATE2我的應用程序位於INSTALLED_APPS頂部的heroku rest_framowork上,但是http://myapp.herokuapp.com/docs/返回:

AttributeError at /docs/
'NoneType' object has no attribute 'items'
Request Method: GET
Request URL:    http://myapp.herokuapp.com/docs/
Django Version: 1.11
Exception Type: AttributeError
Exception Value:    
'NoneType' object has no attribute 'items'
Exception Location: /app/.heroku/python/lib/python3.6/site-packages/rest_framework/templatetags/rest_framework.py in items, line 244
Python Executable:  /app/.heroku/python/bin/python
Python Version: 3.6.1
Python Path:    
['/app/Project/backend/project',
 '/app/.heroku/python/bin',
 '/app',
 '/app/.heroku/python/lib/python36.zip',
 '/app/.heroku/python/lib/python3.6',
 '/app/.heroku/python/lib/python3.6/lib-dynload',
 '/app/.heroku/python/lib/python3.6/site-packages']
Server time:    Mon, 17 Jul 2017 12:49:49 +0000

解決方案我在requirements.txt中使用djangorestframework==3.6.2代替djangorestframework==3.6.3 我不知道為什么3.6.3版本不起作用。

這可能意味着您沒有在settings.py文件中將rest_framework添加到INSTALLED_APPS中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM