簡體   English   中英

在Heroku上使用gunicorn服務器的Django項目不提供靜態文件

[英]Django project with gunicorn server on Heroku does not serve static files

我的Django 1.3項目服務於開發服務器上的靜態文件,但沒有提供gunicorn服務器靜態文件。 我按照Heroku指南的步驟進行操作。

當我使用我的proc文件的內容時,如指南( web: gunicorn myproject_django.wsgi -b 0.0.0.0:$PORT ),Heroku無法識別我的項目。

然后我將Procfile更改為:

web: python myproject_django/manage.py run_gunicorn -b 0.0.0.0:$PORT -w 3

現在我的應用運行除了靜態文件(css不活動也沒有圖像)。

我的項目樹:

.
├── Procfile
├── myproject_django
│   ├── core
│   │   ├── admin.py
│   │   ├── __init__.py
│   │   ├── models.py
│   │   ├── static
│   │   │   ├── css
│   │   │   │   ├── base.css
│   │   │   │   ├── layout.css
│   │   │   │   
│   │   │   └── media
│   │   │       ├── pek.ico
│   │   │       ├── pek.png
│   │   │       ├── pek_symbol.png
│   │   ├── tests.py
│   │   └── views.py
│   ├── __init__.py
│   ├── manage.py
│   ├── settings.py
│   ├── templates
│   │   └── core
│   │       ├── home.html
│   │       └── install.html
│   └── urls.py
└── requirements.txt

settings.py的潛在相關部分

MEDIA_ROOT = ''

MEDIA_URL = '/static/media'

STATIC_ROOT = ''

STATIC_URL = '/static/'

ADMIN_MEDIA_PREFIX = '/static/admin/'

STATICFILES_DIRS = (
    os.path.abspath(__file__)+'/..'+'/myproject_django/core/static', 
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)


INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
    'gunicorn',
    'django.contrib.admin',
)

編輯

在Francis Yaconiello的參賽作品后,我調整了以下內容:

在settings.py中

STATIC_ROOT = os.path.join(os.getcwd(),'core/static')
STATICFILES_DIRS = ( os.path.abspath(__file__)+'/..'+'/core/static', )

在gitignore:

staticfiles/*

然后承諾。 最后運行heroku run python myproject_django/manage.py collectstatic

但是當我檢查網頁時,仍然沒有提供靜態文件。 鑒於我的目錄樹,為什么這些更改不起作用?

EDIT2

我仍然沒有看到我的靜態文件。 當我在DEBUG=True時單擊圖像時,我得到了這個:

Request URL: http://myproject.herokuapp.com/static/media/pek.png

(請注意, staticfiles目錄為空)

.
├── Procfile
├── myproject_django
│   ├── admin
│   ├── core
│   │   ├── admin.py
│   │   ├── __init__.py
│   │   ├── models.py
│   │   ├── static
│   │   │   ├── css
│   │   │   │   ├── base.css
│   │   │   │   ├── layout.css
│   │   │   └── media
|   |   |       ├── pek.ico
|   │   │       ├── pek.png
|   │   │       ├── pek_symbol.png
│   │   ├── tests.py
│   │   ├── views.py
│   ├── __init__.py
│   ├── manage.py
│   ├── settings.py
│   ├── staticfiles
│   ├── templates
│   │   └── core
│   │       ├── 404.html
│   │       ├── 500.html
│   │       ├── home.html
│   │       └── install.html
│   ├── urls.py
└── requirements.txt

在settings.py中

PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'static/media')
STATIC_ROOT = os.path.join(PROJECT_PATH,'staticfiles')
STATICFILES_DIRS = (
    os.path.join(PROJECT_PATH, 'core/static'),
)

指定您的STATIC_ROOT

我通常把它設置為:

import os
STATIC_ROOT = os.path.join(os.getcwd(), "staticfiles")

在項目中創建該目錄

mkdir staticfiles

確保靜態文件的內容不是git跟蹤的

nano .gitignore

staticfiles/*

然后提交它並推送到heroku。

最后,

heroku run python manage.py collectstatic

編輯

STATIC_ROOT = os.path.join(os.getcwd(),'core/static')
STATICFILES_DIRS = ( os.path.abspath(__file__)+'/..'+'/core/static', )

這兩個設置不可能是一回事。

STATICFILES_DIR是好的,如果多余,你的STATICFILES_FINDERS已告訴它在每個應用程序的static目錄中找到static文件: 'django.contrib.staticfiles.finders.AppDirectoriesFinder',

STATIC_ROOT是提供一個與您的項目完全分開的新目錄,您可以使用第三方服務器(如nginx)。 這就是您創建該目錄staticfiles

STATIC_ROOT = os.path.join(os.getcwd(),'staticfiles')

另一個編輯

臨時文件系統的解決方法

每個dyno都有自己的短暫文件系統,並帶有最近部署的代碼的全新副本。 在dyno的生命周期中,其運行進程可以將文件系統用作臨時暫存器,但是任何其他dyno中的進程都不會看到所寫的文件,並且在dyno停止或重新啟動時,所寫的任何文件都將被丟棄。

這意味着只要你在網絡啟動期間在每個dyno實例上收集靜態,你就不會有任何問題。

web: python myproject_django/manage.py collectstatic --noinput; python myproject_django/manage.py run_gunicorn -b 0.0.0.0:$PORT -w 3

也就是說,我目前使用S3和django存儲http://django-storages.readthedocs.org/en/latest/index.html 這很容易(也很便宜)。

有一個解決方案。

在urls.py中添加:

from django.conf import settings

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

ps對於STATIC_URL = '/static/'

使用run_collectstatic其運行的一部分腳本post_compile所提供的Heroku,Django的食譜 它使用了Heroku python buildpack提供的post_compile鈎子

我遇到了同樣的問題; 看來你需要特別需要這三個設置:

  1. 必須在settings.py定義STATIC_ROOT ,例如:

     STATIC_ROOT = os.path.join(PROJECT_PATH, 'served/static/') 

    PROJECT_PATH是您的項目根目錄(在您的情況下,是myproject_django目錄的絕對路徑)。

  2. 類似地,還必須設置STATIC_URL (如果不這樣,Django將拋出一個ImproperlyConfigured錯誤)。 你當前的'/static/'設置沒問題。

  3. 在你的root urls.py ,配置靜態URL:

     from django.conf import settings from django.conf.urls.static import static ... urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 

最后,運行python manage.py collectstatic ; 這會將所有文件從/path/to/site-packages/django/contrib/admin/static/admin/復制/path/to/site-packages/django/contrib/admin/static/admin//served/static/ (您可以在此處閱讀有關Django如何提供靜態文件的更多信息)。

現在當你運行foreman start ,你應該看到帶有樣式的管理員。

不要忘記將.gitignore served/static/添加到.gitignore

我在這里找到解決我在heroku上使用Django的所有問題的好方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM