Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Django:返回包含脚本的HTML文件_Javascript_Html_Django - Fatal编程技术网

Javascript Django:返回包含脚本的HTML文件

Javascript Django:返回包含脚本的HTML文件,javascript,html,django,Javascript,Html,Django,我正在开发一个名为“书签”的单页应用程序,使用HTML+CSS+jQuery作为前端,Django作为后端。我创建了一个Django项目,并将我的app.html+app.js+app.css+jquery.js放入/static 我的问题是:我希望我的视图在不使用Django模板的情况下按原样返回app.html。我试过这个: #app/views.py from django.shortcuts import render_to_response, HttpResponse import u

我正在开发一个名为“书签”的单页应用程序,使用HTML+CSS+jQuery作为前端,Django作为后端。我创建了一个Django项目,并将我的app.html+app.js+app.css+jquery.js放入/static

我的问题是:我希望我的视图在不使用Django模板的情况下按原样返回app.html。我试过这个:

#app/views.py
from django.shortcuts import render_to_response, HttpResponse
import urllib
def index(request):
    index = urllib.urlopen('static/app.html').read()
    return HttpResponse(index)

#static/app.html
<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'>
    <title>My Bookmarks</title>
    <link type="text/css" rel="stylesheet" href="app.css">
    <script src="jquery.js"></script>
    <script type='text/javascript' src="app.js">
    jQuery.noConflict();
    </script>
</head>
<body>
    <div id="title">
        Easy-Save Bookmarks
    </div>
    <div class="left">
        <h3 class="tag">News</h3>
            <div>
                <p> A book mark here!</p>
            </div>
        <h3 class="tag">Reading</h3>
            <div>
                <p> A book mark here!</p>
            </div>
            <div id="newTag"></div>
            <div>
                <form name="addTagForm">
                    <input type="text" name="newTag">
                    <input id="addTag" type="button" value="Add a new tag">
                </form>
            </div>
    </div>
    <div class="right">
    </div> 
</body>
</html>
#app/views.py
从django.shortcuts导入render_to_响应,HttpResponse
导入URL库
def索引(请求):
index=urllib.urlopen('static/app.html').read()
返回HttpResponse(索引)
#static/app.html
我的书签
jQuery.noConflict();
轻松保存书签
新闻
这里有个书号

阅读 这里有个书号

但似乎所有的JS和CSS效果都被忽略了


有人能帮我解决这个问题吗?

最后我发现这是因为我没有绝对地包含我的js+css文件

现在,当我将HTML+JS+CSS文件放入我的_app/static/,并包含绝对值
视图将正确返回我的html。

为什么这样做?正确渲染模板有什么问题?是的,这是我渲染模板的第二个选择,但我始终想知道是否有一些简单的方法可以在没有模板的情况下实现这一点。所以我不会浪费我编写的html文件。有什么比在Django中呈现模板更简单的呢?现有的html文件是如何被浪费的?您只需使用app.html作为模板,而无需更改任何内容-这会将您的views.py缩短为一个在线版本。但是,我怀疑是否会找到js和css:路径必须设置为src='/static/apps.js'