无法加载Django中的静态文件

2024-05-29 02:35:08 发布

您现在位置:Python中文网/ 问答频道 /正文

这里的相关配置设置.py公司名称:

import os

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

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

STATIC_URL = '/static/'

#STATICFILES_DIR = (
#    os.path.join(BASE_DIR, 'static'),
#)

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

在项目中网址.py

^{pr2}$

在marcador应用程序中网址.py

urlpatterns = [
    url(r'^user/(?P<username>[-\w]+)/$', 'marcador.views.bookmark_user',
        name='marcador_bookmark_user'),
    url(r'^$', 'marcador.views.bookmark_list', name='marcador_bookmark_list'),
]

在模板中/基本.html(摘录)

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Marcador - {% block title %}{% endblock %}</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="apple-touch-icon" href="{% static 'apple-touch-icon.png' %}">

    <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
    <style>
        body {
            padding-top: 50px;
            padding-bottom: 20px;
        }
    </style>
    <link rel="stylesheet" href="{% static 'css/bootstrap-theme.min.css' %}">
    <link rel="stylesheet" href="{% static 'css/main.css' %}">

    <script src="{% static 'js/vendor/modernizr-2.8.3-respond-1.4.2.min.js' %}"></script>
</head>

这里是我的目录结构(项目名为marca,应用程序名为marcador)

+-- marca
+-- marcador
|   +-- migrations
|   +-- templates
|       +-- marcador
+-- static
|   +-- admin
|   |   +-- css
|   |   +-- img
|   |   |   +-- gis
|   |   +-- js
|   |       +-- admin
|   +-- css
|   +-- img
|   +-- js
|       +-- vendor
+-- templates

这里是变量的打印输出

Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import settings
>>> print settings.BASE_DIR
/home/fabrice/Documents/Programing/django/marca
>>> print settings.PROJ_DIR
/home/fabrice/Documents/Programing/django/marca/marca
>>> print settings.STATIC_URL
/static/
>>> print settings.STATICFILES_DIR
('/home/fabrice/Documents/Programing/django/marca/marca/static',)
>>> print settings.STATIC_ROOT
/home/fabrice/Documents/Programing/django/marca/static
>>>  

内容页面显示正确,但是由于服务器返回404错误,bootstap没有加载

[20/Sep/2015 16:09:53] "GET / HTTP/1.1" 200 4822
[20/Sep/2015 16:09:54] "GET /static/css/bootstrap.min.css HTTP/1.1" 404 1676
[20/Sep/2015 16:09:54] "GET /static/css/bootstrap-theme.min.css HTTP/1.1" 404 1694
[20/Sep/2015 16:09:54] "GET /static/js/vendor/modernizr-2.8.3-respond-1.4.2.min.js HTTP/1.1" 404 1751
[20/Sep/2015 16:09:54] "GET /static/css/main.css HTTP/1.1" 404 1649
[20/Sep/2015 16:09:54] "GET /static/js/vendor/bootstrap.min.js HTTP/1.1" 404 1691
[20/Sep/2015 16:09:54] "GET /static/js/main.js HTTP/1.1" 404 1643
[20/Sep/2015 16:09:54] "GET /static/js/vendor/bootstrap.min.js HTTP/1.1" 404 1691
[20/Sep/2015 16:09:54] "GET /static/js/main.js HTTP/1.1" 404 1643

我读过一些关于同一问题的文章,但是当我打印BASE_DIR变量时,仍然无法使其工作,我可以看到路径是正确的。在

唯一能让它正常工作的方法是当我将引导静态文件复制到

/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/css/

我只是不明白,已经花了不少时间了。在

有什么建议吗?在

关于信息,我正在做这个教程http://django-marcador.keimlink.de/en/


Tags: djangohttpgetsettingsosdirjsstatic
3条回答

您的STATICFILES_DIR包含错误的值。您使用os.path.join(BASE_DIR, '/static')。在

引用documentation for join

If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.

请看下面的代码示例:

>>> os.path.join('/a', 'b', '/c', 'd')
'/c/d'

解决方案:删除'/static'中的正斜杠:os.path.join(BASE_DIR, '/static')

我知道回答这个问题已经太晚了,但是我发现如果我使用下面的STATICFILES_DIRS并注释STATIC_ROOT,它就可以正常工作了

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

您应该看看Django文档。有一个关于管理静态文件的特定页面:

https://docs.djangoproject.com/en/1.8/howto/static-files/

项目开发期间网址.py应该是:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('marcador.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

但在生产中,一个常见的做法是使用http服务器来处理静态文件。生产配置的答案是广泛的,因为它将取决于您的堆栈。在

相关问题 更多 >

    热门问题