2017-04-04 60 views
0

我已經使用apidoc library爲我的Django應用程序創建了文檔。 它使用角度創建文檔。應用程序正在Heroku上運行。將文檔添加到django應用程序 - 與角度衝突

當我打開index.html文件時,文檔正常工作,但無法通過http://localhost:5000/docs打開文檔。

首先,我得到這個錯誤:

「變量和屬性可能不是下劃線開頭:‘__’」 ,這我能夠把{%逐字%}和{%endverbatim%}爲繞過index.html文件。 (我首先不太滿意,並希望以其他方式做)。

然後在頁面停滯在載入畫面上,但是當我在Chrome中打開它,我有以下錯誤:

"Uncaught SyntaxError: Unexpected token <" in polyfill.js:1 and require.min.js:1

而且還3警告:

"Resource interpreted as Stylesheet but transferred with MIME type text/html"

在供應商

/引導.min.cs,vendor/prettify.css和css/style.css

我們在其他項目中使用apidocs,並且它完美的工作,所以我認爲這是Django的一個問題。由於文檔是自動生成的,我寧願將更改引入到應用程序中,而不是文檔。 我在Chrome和Safari上試過了。

我的問題 1.我能做些什麼才能使它工作? 2.如何在不將{%verbatim%}標籤放入index.html的情況下使Django與Angular兼容?

這裏是我的控制器:

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>

編輯: 感謝answer from hubert,我能夠找到問題的根源。 事實證明,Django對於API文檔使用的RequireJS並不適用。

我必須在生成的代碼中添加以下更改才能使其正常工作: 第1-4點是index.html,第5,6點是main。JS:

  1. 添加上述標籤此行:

{% load 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>

  • 在添加{%逐字%}在開始和{%endverbatim%}結束時,但在使用require.min.js之前!

  • apiProject: './api_project.js', apiData: './api_data.js',

  • 更改行::
  • 在main.js加在文件的開頭以下行路徑

    './api_project.js', './api_data.js',

    'api_project', 'api_data',

  • 回答

    1

    從這兩個錯誤:

    "Uncaught SyntaxError: Unexpected token <" in polyfill.js:1 and require.min.js:1 
    "Resource interpreted as Stylesheet but transferred with MIME type text/html" 
    

    我會認爲有什麼不對您加載靜態文件。可能你有404或500,django加載默認路由。 檢查您是否擁有靜態文件的正確路由。

    +0

    我有同樣的問題,但無法解決。 –

    相關問題