2017-04-05 104 views
0

我的静态文件不会加载,无论什么和我已经通过stackoverflow查看和阅读文档五个小时,所以我需要帮助。静态文件没有注册标签和静态文件没有加载

这里是我的urls.py

urlpatterns = [ 
url(r'^admin/', admin.site.urls), 
url(r'', include('notebook.urls')) 
] 
from django.contrib.staticfiles.urls import staticfiles_urlpatterns 

urlpatterns += staticfiles_urlpatterns() 

同时这是我为我的项目设置,我异乎寻常一直试图找出为什么我的staticfiles没有装入URL。我没有在virtualenv中运行这个,所以这些相关的问题已经解决了我已经查看了Django文档,试图找到我的设置中静态文件未加载的原因,并且在几小时之后,答案已经有效了。

这是我的设置文件的项目

""" 
Django settings for onesky project. 

Generated by 'django-admin startproject' using Django 1.10.5. 

For more information on this file, see 
https://docs.djangoproject.com/en/1.10/topics/settings/ 

For the full list of settings and their values, see 
https://docs.djangoproject.com/en/1.10/ref/settings/ 
""" 

import os 

# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 


# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/ 

# Static file directory 
TEMPLATE_DIR = os.path.join(BASE_DIR, "templates") 
# Static files for multiple directories 
STATICFILES_DIR = os.path.join(BASE_DIR, "static") 
# Media file directory 
MEDIA_DIR = os.path.join(BASE_DIR, "media") 

# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = 'q*6q$)mqhr**pwaj4m!8km&2i7s2ewu+lg%7p$*pm6+8!l8cwf' 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 

ALLOWED_HOSTS = [] 


# Application definition 

INSTALLED_APPS = [ 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.staticfiles', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'rest_framework', 
    'notebook', 

] 

MIDDLEWARE = [ 
    'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
] 

ROOT_URLCONF = 'onesky.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [TEMPLATE_DIR], 
     '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', 
       'django.template.context_processors.media', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'onesky.wsgi.application' 


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

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


# Password validation 
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators 

AUTH_PASSWORD_VALIDATORS = [ 
    { 
     'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 
    }, 
] 


# Internationalization 
# https://docs.djangoproject.com/en/1.10/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.10/howto/static-files/ 
STATIC_URL = '/static/' 
STATIC_ROOT = STATICFILES_DIR 

# Media Root 
MEDIA_ROOT = MEDIA_DIR 
MEDIA_URL = '/media/' 

只是为了完整性这里也wgsi.py文件

""" 
WSGI config for onesky project. 

It exposes the WSGI callable as a module-level variable named ``application``. 

For more information on this file, see 
https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/ 
""" 

import os 

from django.core.wsgi import get_wsgi_application 

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "onesky.settings") 

application = get_wsgi_application() 

这里是我的模板代码

<!DOCTYPE html> 
<html lang="en"> 
<head> 
{% load staticfiles %} 

    <meta charset="UTF-8"> 
    <title>OneSky</title> 
    <link rel="stylesheet" href="{% load static "css/normalize.css" %}"> 
    <link rel="stylesheet" href="{% load static'css/home.css' %}"> 
    <link rel="stylesheet" href="{% load static '/css/notebook.css' %}"> 
    <link rel="stylesheet" href="{% load static 'css/skeleton.css' %}"> 
</head> 
<body> 
<div class="row" style="padding-top: 1%;"> 
    <div class="nav"> 
     <div class="three columns"> 
      <div class="logo">OneSky</div> 
     </div> 
     <!-- 
      @TODO:Change to class based styling 
     --> 
     <div class="seven columns" style="height: 100%"> 
      <div class="spacer"></div> 
     </div> 
     <div class="one column"> 
      <img src="{% load static 'img/settings-cog.png' %}" class="icon"> 
     </div> 
     <div class="one column"> 
      <img src="img/account.png" class="icon"> 
     </div> 
    </div> 
</div> 
<div class="data-container"> 
<div class="row"> 

     <div class="three columns" style="overflow: hidden"> 
      <div class="scroll-container"> 
      <div class="note-container"> 
      <div class="row"> 
       <div class="eight columns"> 
        <h2 class="note header"> Notebook </h2> 
       </div> 
       <div class="two columns"> 
        <img src="img/add-button.png" class="icon"> 

       </div> 
       <div class="two columns"> 
        <img src="img/list-button.png" class="icon"> 
       </div> 
      </div> 
       <div id="content" class="hiddenscrollbars"> 
       <hr> 
        {% for post in posts %} 

          <div class="row"> 
        <div class="note-title"> 
         <h4> {{ post.title }}</h4> 
        </div> 
       </div> 
        {% endfor %} 

      </div> 

       </div> 

     </div> 
      </div> 
    </div> 


<div class="eight columns"> 
    <div class="row"> 
     <h3 class="selected-note-title">Select Something</h3> <span><button class="submit" title="submit">Save</button></span> 
    </div> 
    <div class="scrolling-container"> 
     <div class="editor-container"> 
     </div> 

    </div> 
</div> 

</div> 
</div> 








</body> 
<!-- Include the Quill library --> 
<script src="js/quill.js"></script> 
<!-- Initialize Quill editor --> 
<script src="js/notebook.js"></script> 


</html> 

感谢您在提前求助

+0

你是怎样尝试访问它,什么当你尝试访问它时,你会收到错误吗? –

+0

''img''不是已注册的标签库。必须是一个: admin_list admin_modify admin_static admin_urls 缓存 国际化 本地化 日志 rest_framework 静态 staticfiles TZ @almostabeginner –

+0

你能请更新您的问题,并添加相关的模板代码,谢谢。 –

回答

0

您需要使用static才能获取静态文件的url,而不是使用load static,这会让Django误以为您正在加载自定义代码。

,你需要使用load唯一的实例是:

{% load staticfiles %} 

更改这些:

<link rel="stylesheet" href="{% load static "css/normalize.css" %}"> 
<link rel="stylesheet" href="{% load static'css/home.css' %}"> 
<link rel="stylesheet" href="{% load static '/css/notebook.css' %}"> 
<link rel="stylesheet" href="{% load static 'css/skeleton.css' %}"> 
.... 
.... 
<img src="{% load static 'img/settings-cog.png' %}" class="icon"> 

要:

<link rel="stylesheet" href="{% static "css/normalize.css" %}"> 
<link rel="stylesheet" href="{% static'css/home.css' %}"> 
<link rel="stylesheet" href="{% static '/css/notebook.css' %}"> 
<link rel="stylesheet" href="{% static 'css/skeleton.css' %}"> 
.... 
.... 
<img src="{% static 'img/settings-cog.png' %}" class="icon"> 
+0

我感谢你先生......我甚至不知道该说些什么 –

+0

@travissmith很高兴能帮上忙。 –