Javascript与Django? [英] Javascript with Django?

查看:92
本文介绍了Javascript与Django?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这之前已经被问过,但是我很难在我的Django网络应用程序上设置JS,即使我正在阅读文档。



我正在运行Django dev服务器。我的文件结构如下:

  mysite / 
__init__.py
MySiteDB
manage .py
settings.py
urls.py
myapp /
__init__.py
admin.py
models.py
test.py
views.py
templates /
index.html

我想把Javascript和CSS吗?我在很多地方尝试过,包括 myapp / templates /



index.html

$ b $

 < head> 
< title>学位计划< / title>
< script type =text / javascriptsrc =/ scripts / JQuery.js>< / script>
< script type =text / javascriptsrc =/ media / scripts / sprintf.js>< / script>
< script type =text / javascriptsrc =/ media / scripts / clientside.js>< / script>
< / head>

urls.py

 (r'^ admin /',include(admin.site.urls)),
(r'^ media /(?P< path& *)$','django.views.static.serve',{'document_root':'media'})
(r'^。*','mysite.myapp.views.index'),

我怀疑 serve()错误原因:

  / admin / auth / $ b下的TypeError $ b'tuple'对象不可调用

为了避免猖獗,我在 settings.py 中更改了这些设置:

  MEDIA_ROOT ='/ media /'
MEDIA_URL ='http://127.0.0.1:8000/media'

UPDATE :我进行了一些更改,但仍然无法正常工作: / p>

settings.py

  ROOT_PATH = os.path.normpath(os.path.dirname(__ file__))

py

 (r'^ media /(?P< path>。*)$','django .views.static.serve',{'document_root':os.path.join(settings.ROOT_PATH,'site_media')}),

index.html

 < script type = text / javascriptsrc =/ media / JQuery.js>< / script> 
< script type =text / javascriptsrc =/ media / sprintf.js>< / script>
< script type =text / javascriptsrc =/ media / clientside.js>< / script>

文件系统

  mysite / 
site_media /
JQuery.js
sprintf.js
clientside.js
__init__.py
settings.py
manage.py
- etc
myapp /
- 应用程式档案等

当我访问 http://127.0.0.1:8000/media/sprintf.js 我得到:

 未找到页面:/media/sprintf.js 
pre>

解决方案

但是 / media / 全局目录是否存在?你有放在那里的脚本子目录与你想要从那里服务的脚本?对于要提供 JQuery.js / scripts /... url, t似乎在您的 urls.py 的任何地方提供。如果您(无论出于何种原因)想要从几个不同的网址路径提供脚本(或任何其他静态提供的文件),则所有这些网址路径都需要在 urls.py 与静态服务 - 否则,做正常的事情,并从 / media /...根URL服务它们,并将该媒体根映射到dir



Django的文档关于静态服务(仅用于开发),因为它被记录为


使用此方法无效
不安全,请勿在
生产设置中使用此方法。


所以要小心! - )对我来说很清楚。


I know this has been asked before, but I'm having a hard time setting up JS on my Django web app, even though I'm reading the documentation.

I'm running the Django dev server. My file structure looks like this:

mysite/
      __init__.py
      MySiteDB
      manage.py
      settings.py
      urls.py
      myapp/
           __init__.py
           admin.py
           models.py
           test.py
           views.py
           templates/
                index.html

Where do I want to put the Javascript and CSS? I've tried it in a bunch of places, including myapp/, templates/ and mysite/, but none seem to work.

From index.html:

<head>
    <title>Degree Planner</title>
    <script type="text/javascript" src="/scripts/JQuery.js"></script>
    <script type="text/javascript" src="/media/scripts/sprintf.js"></script>
    <script type="text/javascript" src="/media/scripts/clientside.js"></script>
</head>

From urls.py:

(r'^admin/', include(admin.site.urls)),
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': 'media'})
(r'^.*', 'mysite.myapp.views.index'),

I suspect that the serve() line is the cause of errors like:

TypeError at /admin/auth/
'tuple' object is not callable

Just to round off the rampant flailing, I changed these settings in settings.py:

MEDIA_ROOT = '/media/'
MEDIA_URL = 'http://127.0.0.1:8000/media'

UPDATE: I made some changes, but it's still not working:

settings.py:

ROOT_PATH = os.path.normpath(os.path.dirname(__file__))

urls.py:

(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': os.path.join(settings.ROOT_PATH, 'site_media')}),

index.html:

<script type="text/javascript" src="/media/JQuery.js"></script>
<script type="text/javascript" src="/media/sprintf.js"></script>
<script type="text/javascript" src="/media/clientside.js"></script>

Filesystem:

mysite/
      site_media/
                JQuery.js
                sprintf.js
                clientside.js
      __init__.py
      settings.py
      manage.py
      -- etc
      myapp/
           -- app files, etc

When I go to a url like http://127.0.0.1:8000/media/sprintf.js, I get:

Page not found: /media/sprintf.js

解决方案

But does that /media/ global directory exist? And have you placed in there a scripts subdirectories with the scripts you want to serve from there? What about the /scripts/... url from which you want to serve JQuery.js -- that doesn't seem to be served anywhere from your urls.py. If you (for whatever reason) want to serve scripts (or any other statically served file) from several different URL paths, all of those URL paths need to be matched in urls.py with the static-serving -- or else, do the normal things and serve them all from the /media/... root URL, and map that media root to the dir where you actually keep these files (in their respective subdirs, typically).

Django's docs about static serving (for development only, since it's documented as

Using this method is inefficient and insecure. Do not use this in a production setting. Use this only for development.

so beware!-) seems pretty clear to me.

这篇关于Javascript与Django?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆