简体   繁体   English

无法提供静态文件,Django 1.6

[英]Cannot serve static files, Django 1.6

I am unable to get Django 1.6 to serve static files. 我无法使Django 1.6提供静态文件。

In my settings.py I have: 在我的settings.py中,我有:

DEBUG = True

#...

INSTALLED_APPS = (
    #...
    'django.contrib.staticfiles',
    #...
)

STATIC_URL = '/static/'
STATIC_ROOT = '/home/it/static/'
STATIC_DIRS = (
    os.path.join(BASE_DIR, '../static'),
)
STATIC_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

I have also added the following to my urls.py: 我还向我的urls.py中添加了以下内容:

urlpatterns = patterns('',
    #...
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

The HTML file in question has the following: 该HTML文件具有以下内容:

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

The relevant parts of the project tree look like this: 项目树的相关部分如下所示:

/media/extra/django/
├── it
│   ├── it
│   │   ├── __init__.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   ├── wsgi.py
│   ├── jobs
│   │   ├── admin.py
│   │   ├── __init__.py
│   │   ├── models.py
│   │   ├── static
│   │   │   └── jobs
│   │   ├── tests.py
│   │   ├── urls.py
│   │   ├── views.py
│   ├── manage.py
├── static
│   ├── css
│   │   ├── base.css
│   │   └── colours.css
│   ├── images
│   │   └── favicon.ico
│   └── scripts
│       └── base.js
└── templates
    ├── it
    │   ├── base.html
    │   └── index.html
    ├── jobs
    │   └── index.html
    ├── site_base.html

I have tried having the static directory as a sibling of the 'it' directory (and changing the STATIC_DIRS variable). 我尝试过将静态目录作为“ it”目录的同级(并更改STATIC_DIRS变量)。 I have tried using += staticfiles_urlpatterns() on the urlpatterns instead. 我试过在urlpatterns上使用+ = staticfiles_urlpatterns()。 I have tried adding the full path to the STATIC_DIRS. 我尝试将完整路径添加到STATIC_DIRS。

I have also looked at these relevant threads: static files in django 1.6 not found Can't get Django to serve static files Django 1.6.2 will not serve static content 我也看过这些相关的线程: django 1.6中的静态文件未找到 无法让Django提供静态文件 Django 1.6.2将不会提供静态内容

Many thanks 非常感谢

I'm not sure what is wrong with your setup, but this are the settings that work for me: 我不确定您的设置有什么问题,但这是对我有用的设置:

settings.py settings.py

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

I haven't defined STATIC_ROOT or STATIC_FINDERS so it's using the defaults. 我尚未定义STATIC_ROOT或STATIC_FINDERS,因此它使用的是默认设置。 I have nothing special in urls.py for static files, just one line for media: 对于静态文件,我在urls.py中没有什么特别的要求,对于媒体来说只有一行:

urls.py urls.py

urlpatterns = patterns('',
    #...
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

The static tag is working fine from the templates so I believe your line in urls.py is not absolutely needed. 静态标记在模板上工作正常,因此我认为您并不是绝对需要urls.py的行。

You shouldn't be serving static content through a web framework, that's a job for the webserver (apache or nginx for example) 您不应该通过Web框架来提供静态内容,这是Web服务器的工作(例如Apache或Nginx)

If you have STATIC_URL = '/static/' in your settings.py file you can put "{{ STATIC_URL }}css/base.css" in your templates for example, and have the web server serve /static/ statically, bypassing django completely. 例如,如果您settings.py文件中的STATIC_URL = '/static/' ,则可以在模板中放置"{{ STATIC_URL }}css/base.css" ,然后让网络服务器以/static/方式提供/static/服务,从而绕过Django完全。

In your urls.py try to tell django standard views to serve your static files by changing the urlpatterns 在您的urls.py中,尝试通过更改urlpatterns来告诉Django标准视图为您的静态文件提供服务

 urlpatterns = patterns('',
   #...
 ) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

 With the following:

 urlpatterns = patterns('',
   #...
 )
 if settings.DEBUG:
 urlpatterns += patterns('',
   (r'^static/(?P<path>.*)$', 'django.views.static.serve',
   {'document_root':settings.STATIC_ROOT}),)


 In the settings.py add:

 import os.path
 STATIC_ROOT ='only_the_path_to_the_project'
 STATICFILES_DIRS = (
  os.path.join(os.path.dirname(__file__),'staticcc').replace('\\','/'),
  )

In development 开发中
In your app folder create folder name 'static' and save your picture in that folder. 在您的应用文件夹中,创建文件夹名称“ static”,然后将图片保存在该文件夹中。
To use picture use: 要使用图片,请使用:

<html>
    <head>
       {% load staticfiles %} <!-- Prepare django to load static files -->
    </head>
    <body>
        <img src={% static "image.jpg" %}>
    </body>
</html>

In production: 在生产中:
Everything same like in development, just add couple more parameters for Django: 就像开发中一样,只需为Django添加更多参数:
1. add in settings.py 1.添加settings.py
STATIC_ROOT = os.path.join(BASE_DIR, "static/") (this will prepare folder where static files from all apps will be stored) STATIC_ROOT = os.path.join(BASE_DIR, "static/") (这将准备用于存储所有应用程序的静态文件的文件夹)
2. be sure your app is in INSTALLED_APPS = ['myapp',] 2.确保您的应用程序位于INSTALLED_APPS = ['myapp',]
3. in terminall run command python manage.py collectstatic (this will make copy of static files from all apps included in INSTALLED_APPS to global static folder - STATIC_ROOT folder ) 3.在terminall中运行命令python manage.py collectstatic (这会将INSTALLED_APPS中包含的所有应用程序的静态文件复制到全局静态文件夹-STATIC_ROOT文件夹中)

Thats all what Django need, after this you need to make some web server side setup to make premissions for use static folder. 这就是Django需要的全部内容,之后,您需要进行一些Web服务器端设置以允许使用静态文件夹。 Eg in apache2 in configuration file httpd.conf (for windows) or sites-enabled/000-default.conf. 例如,在配置文件httpd.conf (对于Windows)中的apache2或sites-enabled / 000-default.conf中。 (under site virtual host part for linux) add: (在Linux的站点虚拟主机部分下)添加:

 Alias \static "path_to_your_project\static"

<Directory "path_to_your_project\static">
    Require all granted
</Directory>



And that's all 就这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM