简体   繁体   English

js文件没有加载到django模板中

[英]js files are not loading in django template

I have a little issue with my django project.我的 django 项目有点问题。 My static files are loading (img and css) whereas js files are not.我的静态文件正在加载(img 和 css),而 js 文件没有。 I have created a folder static in my app with the following structure static/js/my_app/ with my js files in it.我在我的应用程序中创建了一个静态文件夹,其结构如下static/js/my_app/ ,其中包含我的 js 文件。

Here is the part of my template using js scripts:这是我使用 js 脚本的模板的一部分:

<!-- Scripts -->
<script src="{% static 'js/landing/jquery.min.js' %}"></script>
<script src="{% static 'js/landing/jquery.scrolly.min.js' %}"></script>
<script src="{% static 'js/landing/jquery.dropotron.min.js' %}"></script>
<script src="{% static 'js/landing/jquery.scrollex.min.js' %}"></script>
<script src="{% static 'js/landing/util.js' %}"></script>
<!--[if lte IE 8]><script src="assets/js/ie/respond.min.js"></script><![endif]-->
<script src="{% static 'js/landing/main.js' %}"></script>

And here is the part of my settings.py file dealing with static files:这是我的 settings.py 文件中处理静态文件的部分:

STATIC_URL = '/static/'

STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)

Thanks谢谢

Usually the standard recommendation is to have app_name/static/app_name/js/file.js 通常,标准建议是使用app_name / static / app_name / js / file.js

Then in template you would just use: {% static 'app_name/js/file_name.js' %}, provided everything else for static is set up correctly. 然后,在模板中,您将只使用:{%static'app_name / js / file_name.js'%},前提是正确设置了static的其他所有内容。

In your configuration you have done static/js/app_name instead of static/app_name/js. 在您的配置中,您完成了static / js / app_name而不是static / app_name / js。

if you are follow this steps means you can make direct call call your js or css file /static/css/a.css , in template , make sure the collectstatic method 如果遵循此步骤,则可以在模板中直接调用js或CSS文件/static/css/a.css,请确保collectstatic方法

import os
def root(x):
    return os.path.join(os.path.abspath(os.path.dirname(__file__)), '..',x)
MEDIA_ROOT = root('media')
MEDIA_URL = '/media/'
STATIC_ROOT = root('staticstorage')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    root('static'),
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'dajaxice.finders.DajaxiceFinder',
)
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    'django.template.loaders.eggs.Loader',

)
TEMPLATE_DIRS = (
    root('templates')
)
TEMPLATE_CONTEXT_PROCESSORS = (

    "--------------------------------------"
    'django.core.context_processors.media',
    'django.core.context_processors.static',
)

url.py

from django.conf.urls import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf import settings
from django.conf.urls.static import static
admin.autodiscover()

urlpatterns = patterns('',
)

urlpatterns += patterns('',
    url(r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.STATIC_ROOT, 'show_indexes': False}),
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),

) 

urlpatterns += staticfiles_urlpatterns()

The solution is not logical, but if you do it, it might be right In the same js directory, create another directory called js and transfer your file to it Run your site now, maybe it has changed Create another file in the new directory called js and transfer the file to it Save again and run it That means, now your file should be in js/js/js/file.js Now put the file back in the primary directory and run the code, maybe it will be fixed I said that it doesn't make sense, but I did it and it worked In addition, you must link the file that you moved correctly in the html file (after changing the path of the js file each time).解决方案不合逻辑,但如果你这样做,它可能是正确的在同一个js目录中,创建另一个名为js的目录并将你的文件传输到它现在运行你的站点,也许它已经改变在新目录中创建另一个文件名为js 并将文件传输给它再次保存并运行它意味着,现在你的文件应该在 js/js/js/file.js 现在把文件放回主目录并运行代码,也许它会被修复我说的没有意义,但是我做到了,而且成功了另外,必须在html文件中正确链接你移动的文件(每次更改js文件的路径后)。 I calculated from the js file, but you should calculate from the file that contains your JavaScript code我从 js 文件计算,但你应该从包含你的 JavaScript 代码的文件计算

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM