无法将JavaScript文件加载到Django App中

时间:2015-08-13 11:43:48

标签: python django

我有一个使用JavaScript文件+ CSS的Django应用。

虽然css文件完全加载,但Java脚本的全部却没有。

CSS文件位置:

 C:\Users\user\Desktop\Django project\general\adray\static\adray\assets

JavaScript文件位置(是的,我检查过,java脚本文件确实存在):

 C:\Users\user\Desktop\Django project\general\adray\static\adray\js

这是base.html加载内容:

{% load staticfiles %}

<link rel="stylesheet" type="text/css" href="{% static 'adray/assets/custom.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'adray/assets/bootstrap.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'adray/assets/font-awesome.css' %}" />

  <script type="text/javascript" src=" {% static 'static/adray/js/jquery-1.10.2.js' %}"></script>
 <script type="text/javascript" src=" {% static 'adray/js/bootstrap.min.js' %}"></script>
  <script type="text/javascript" src=" {% static 'adray/morris/raphael-2.1.0.min.js' %}"></script>
   <script type="text/javascript" src=" {% static 'adray/js/custom.js' %}"></script>
   <script type="text/javascript" src=" {% static 'adray/js/jquery.metisMenu.js' %}"></script>



<!DOCTYPE html> ......

服务器日志(所有JavaScript Get请求返回404):

    C:\Users\user\Desktop\Django project\general>python manage.py r
unserver
Performing system checks...

System check identified no issues (0 silenced).
August 13, 2015 - 14:55:26
Django version 1.8.2, using settings 'general.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[13/Aug/2015 15:04:26]"GET / HTTP/1.1" 200 10215
[13/Aug/2015 15:04:26]"GET /%7B%20%%20static%20'adray/js/bootstrap.min.js'%20%%2
0%7D HTTP/1.1" 404 2706
[13/Aug/2015 15:04:26]"GET /%7B%20%%20static%20'adray/js/jquery-1.10.2.js'%20%%2
0%7D HTTP/1.1" 404 2706
[13/Aug/2015 15:04:26]"GET /static/adray/assets/custom.css HTTP/1.1" 304 0
[13/Aug/2015 15:04:26]"GET /%7B%20%%20static%20'adray/morris/raphael-2.1.0.min.j
s'%20%%20%7D HTTP/1.1" 404 2730
[13/Aug/2015 15:04:26]"GET /static/adray/assets/bootstrap.css HTTP/1.1" 304 0
[13/Aug/2015 15:04:26]"GET /static/adray/assets/font-awesome.css HTTP/1.1" 304 0

[13/Aug/2015 15:04:26]"GET /%7B%20%%20static%20'adray/js/custom.js'%20%%20%7D HT
TP/1.1" 404 2685

Settings.py:

import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))



SECRET_KEY = ***********************


DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'adray',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'general.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'general.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

TEMPLATE_DIRS = (
    'C:/Users/user/Desktop/Django project/general/adray/Templates',
)
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/



STATIC_URL = '/static/'

STATIC_ROOT = "C:/Users/user/Desktop/Django project/general/adray/static"

FIXTURE_DIRS = (
   'C:/Users/user/Desktop/Django project/general/path/to/adray/fixtures/',
)

我也跑了:

C:\Users\user\Desktop\Django project\general>python manage.py collectstatic

关于最新情况的任何想法? 提前谢谢!

1 个答案:

答案 0 :(得分:3)

您的模板标签无法正确识别,因为它们包含空格:

{ % static 'adray/js/jquery.metisMenu.js' % }

应该是:

{% static 'adray/js/jquery.metisMenu.js' %}