簡體   English   中英

js文件沒有加載到django模板中

[英]js files are not loading in django template

我的 django 項目有點問題。 我的靜態文件正在加載(img 和 css),而 js 文件沒有。 我在我的應用程序中創建了一個靜態文件夾,其結構如下static/js/my_app/ ,其中包含我的 js 文件。

這是我使用 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>

這是我的 settings.py 文件中處理靜態文件的部分:

STATIC_URL = '/static/'

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

謝謝

通常,標准建議是使用app_name / static / app_name / js / file.js

然后,在模板中,您將只使用:{%static'app_name / js / file_name.js'%},前提是正確設置了static的其他所有內容。

在您的配置中,您完成了static / js / app_name而不是static / app_name / js。

如果遵循此步驟,則可以在模板中直接調用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()

解決方案不合邏輯,但如果你這樣做,它可能是正確的在同一個js目錄中,創建另一個名為js的目錄並將你的文件傳輸到它現在運行你的站點,也許它已經改變在新目錄中創建另一個文件名為js 並將文件傳輸給它再次保存並運行它意味着,現在你的文件應該在 js/js/js/file.js 現在把文件放回主目錄並運行代碼,也許它會被修復我說的沒有意義,但是我做到了,而且成功了另外,必須在html文件中正確鏈接你移動的文件(每次更改js文件的路徑后)。 我從 js 文件計算,但你應該從包含你的 JavaScript 代碼的文件計算

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM