Django静态css文件

时间:2016-04-13 09:05:35

标签: django

  

项目

myblog-
   |
   |-static
      |  
      |-Css
         | 
         |-bootstrap.css
  

** Setting.py **

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


**urls**

from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static
from myblog.views import hello, current_datetime, hours_ahead

urlpatterns = [
    url(r'^admin/', admin.site.urls), 
    url(r'^hello/$',hello),
    url(r'^time/$', current_datetime),
    url(r'^time/plus/(\d{1,2})/$', hours_ahead),
    url(r'^', include('blog.urls')),
    url(r'^blog/',include('blog.urls')),
]  + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

urlpatterns += staticfiles_urlpatterns()
  

模板/ base.html文件

  {% load staticfiles %}
    <link href="{% static 'base/css/bootstrap.css' %}" rel="stylesheet" type="text/css" >

运行manage.py runserver 192.168.8.213:8080之后。它显示页面未找到(404)请求方法:GET请求      网址:http://192.168.8.213:8080/static/base/css/bootstrap.cs enter code here s提出者:blog.views.index

 'base/css/bootstrap.css' could not be found

 You're seeing this error because you have DEBUG = True in your Django
 settings file. Change that to False, and Django will display a
 standard 404 page.`enter code here`

1 个答案:

答案 0 :(得分:3)

  • 当我们处于调试模式时,我们必须将静态文件加载到单个应用程序中。

  • 在项目名称aPP文件夹中,我们将加载静态文件夹

    在setting.py

  • 我们必须定义路径

STATICFILES_DIRS =(     os.path.join(BASE_DIR,&#39; myblog / static&#39;),  )

在生产中

  • 如果我们写&#34; python manage.py colltecstatic&#34; ,它会将静态文件复制到root静态文件夹中。