我使用apidoc library为我的 Django 应用程序创建了文档。它使用 Angular 创建文档。应用程序正在 Heroku 上运行。
当我打开文件时,文档运行良好index.html
,但我无法通过http://localhost:5000/docs打开它们。
首先我得到了这个错误:
“变量和属性可能不以下划线开头:'__'”,我可以通过将 {% verbatim %} 和 {% endverbatim %} 放入 index.html 文件来绕过它。(我一开始不太满意,想以其他方式做到这一点)。
然后页面卡在加载屏幕上,但是当我在 Chrome 中打开它时,出现以下错误:
polyfill.js:1 和 require.min.js:1 中的“Uncaught SyntaxError: Unexpected token <”
还有3个警告:
“资源解释为样式表,但使用 MIME 类型 text/html 传输”
在 vendor/bootstrap.min.cs、vendor/prettify.css 和 css/style.css
我们也在其他带有 Node 的项目中使用 apidocs,它运行良好,所以我认为这是 Django 的问题。由于文档是自动生成的,我更愿意将更改引入应用程序,而不是文档。我在 Chrome 和 Safari 上试过。
我的问题 1. 我该怎么做才能让它发挥作用?2. 如何在不添加 {%verbatim%} 标签的情况下使 Django 与 Angular 兼容index.html
?
这是我的控制器:
from django.shortcuts import render
def show_docs(request):
return render(request, 'index.html')
和 url_pattern:
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
import my_app.controller
from django.views.decorators.csrf import csrf_exempt
urlpatterns = [
url(r'^docs/', my_app.controller.show_docs),
]
index.html
头:
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Loading...</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="vendor/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="vendor/prettify.css" rel="stylesheet" media="screen">
<link href="css/style.css" rel="stylesheet" media="screen, print">
<link href="img/favicon.ico" rel="icon" type="image/x-icon">
<link href="css/apidoccustom.css" rel="stylesheet" media="screen, print">
<script src="vendor/polyfill.js"></script>
</head>
编辑:感谢休伯特的回答,我能够找到问题的根源。事实证明,Django 不能很好地与 api 文档使用的 RequireJS 一起工作。
我必须在生成的代码中添加以下更改才能使其工作:第 1-4 点用于 index.html,第 5、6 点用于 main.js:
- 在标签上方添加这一行:
{% load static %}
- 将“{% static”+“%}”标签添加到所有标签,如下所示:
<link href="{% static "vendor/bootstrap.min.css" %}" rel="stylesheet" media="screen">
- 使用 polyfill.js 和 require.min.js 将相同的静态标签添加到标签中:
<script src="{% static "vendor/polyfill.js" %}"></script>
<script data-main="{% static "main.js" %}" src="{% static "vendor/require.min.js" %}"></script>
在开头添加 {% verbatim %} 并在末尾添加 {% endverbatim %} ,但在 require.min.js 之前!
在 main.js 中,将以下行添加到文件开头的路径中:
apiProject: './api_project.js',
apiData: './api_data.js',
- 换行:
'./api_project.js',
'./api_data.js',
至:
'api_project',
'api_data',