2012-05-30 29 views
16

Tôi mới đến heroku và tôi đã thử một ứng dụng django đơn giản mà không cần css.Django heroku static dir

Nhưng tôi chỉ cần thêm một file css trong ứng dụng của tôi và khi tôi làm điều này:

git push heroku master 

Bộ sưu tập tập tin tĩnh thất bại:

[...] 
-----> Collecting static files 
Traceback (most recent call last): 
    File "manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line 
    utility.execute() 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv 
    self.execute(*args, **options.__dict__) 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute 
    output = self.handle(*args, **options) 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle 
    return self.handle_noargs(**options) 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 163, in handle_noargs 
    collected = self.collect() 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 104, in collect 
    for path, storage in finder.list(self.ignore_patterns): 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/contrib/staticfiles/finders.py", line 105, in list 
    for path in utils.get_files(storage, ignore_patterns): 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/contrib/staticfiles/utils.py", line 25, in get_files 
    directories, files = storage.listdir(location) 
    File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/files/storage.py", line 235, in listdir 
    for entry in os.listdir(path): 
OSError: [Errno 2] No such file or directory: '/home/kevin/web/django/appheroku/blogapp/static' 
!  Heroku push rejected, failed to compile Python/django app 

To [email protected]:[...] 
! [remote rejected] master -> master (pre-receive hook declined) 
error: failed to push some refs to '[email protected]:[...]' 

Đây là settings.py của tôi:

[...] 
STATIC_ROOT = '/home/kevin/web/django/appheroku/staticfiles' 

STATIC_URL = '/static/' 

STATICFILES_DIRS = (
    '/home/kevin/web/django/appheroku/blogapp/static', 
) 
[...] 

The urls.py:

[...] 
if settings.DEBUG: 
    urlpatterns += staticfiles_urlpatterns() 

Procfile:

web: python appheroku/manage.py collectstatic --noinput; bin/gunicorn_django --workers=4 --bind=0.0.0.0:$PORT appheroku/monblog/settings.py 

Có vẻ như ngay cả những '/ home/kevin/web/django/appheroku/blogapp/tĩnh' thư mục tồn tại, nó không được phát hiện và tôi không thể tìm ra lý do tại sao. :(

Xin hãy giúp tôi ^^

EDIT:

Bây giờ settings.py của tôi trông như thế này:

import os 
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) 
PROJECT_DIR = os.path.join(PROJECT_ROOT,'../blogapp') 
[...] 
STATIC_ROOT = os.path.join(PROJECT_ROOT,'staticfiles/') 
STATIC_URL = '/static/' 
STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR,'static/'), 
) 
[...] 

Và bây giờ bước, Gõ từ vào tĩnh dường như làm việc tốt:

-----> Collecting static files 
     74 static files copied. 

Đã xảy ra sự cố khác: trong tệp procfile của tôi, 'appherok Không tìm thấy u/manage.py ',' bin/gunicorn_django 'và' appheroku/monblog/settings.py '. Tôi đã sửa lỗi này và giờ đây hình ảnh của tôi trông giống như sau:

web: python manage.py collectstatic --noinput; gunicorn_django --workers=4 --bind=0.0.0.0:$PORT monblog.settings 

Ứng dụng này không bị lỗi nữa nhưng vẫn không có css. Đây là nhật ký:

'2012-05-31T17:35:16+00:00 heroku[router]: GET xxxx-xxxx-number.herokuapp.com/ dyno=web.1 queue=0 wait=0ms service=85ms status=200 bytes=1054 
2012-05-31T17:35:17+00:00 heroku[router]: GET xxxx-xxxx-number.herokuapp.com/static/css/style.css dyno=web.1 queue=0 wait=0ms service=5ms status=404 bytes=2088 
2012-05-31T17:35:17+00:00 heroku[router]: GET xxxx-xxxx-number.herokuapp.com/static/js/jquery-1.7.2.min.js dyno=web.1 queue=0 wait=0ms service=5ms status=404 bytes=2115' 

EDIT3:

Có! nó hoạt động ^^ Vấn đề nằm trong urls.py của tôi. Tôi đã viết:

[...] 
if not settings.DEBUG: 
    urlpatterns += staticfiles_urlpatterns() 

thay vì

[...] 
if settings.DEBUG: 
    urlpatterns += staticfiles_urlpatterns() 

Các lỗi được không trong thông điệp của tôi .. Vâng, nó đã thực sự khó khăn để giúp tôi. (Xin lỗi) Tôi cảm thấy thật ngu ngốc ^^ '

Trả lời

12

Sự cố là đường dẫn tuyệt đối mà bạn đang sử dụng cho STATIC_ROOT không được tìm thấy trong máy chủ Heroku.

Để giải quyết, hãy xem xét cách tiếp cận sau.

Trong settings.py của bạn:

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) 
STATIC_ROOT= os.path.join(PROJECT_DIR,'staticfiles/') 
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT,'static/'), 
) 
+0

Bây giờ tôi sử dụng phương pháp này với một số sửa đổi như được giải thích trong bản chỉnh sửa của tôi. (vì thư mục tĩnh của tôi ('tĩnh /') nằm bên trong thư mục ứng dụng của tôi ('blogapp /')). Nó giải quyết vấn đề của tôi (ngay cả bây giờ tôi đang gặp một số khác -_-) – heathen90

+0

Có phải 'blogapp /' trong thư mục mẹ của tệp cài đặt này không? Có vẻ như nhiều khả năng 'blogapp,' nằm trong cùng thư mục với tệp cài đặt nghĩa là bạn chỉ nên tham gia 'blogapp /' thay vì '../ blogapp'. –

+0

appheroku/chứa monblog/directory, thư mục blogapp/và thư mục staticfiles. Thư mục blogapp/chứa tệp models.py, tệp views.py, admin.py, .. (v.v) và tĩnh/dir. Monblog/thư mục chứa settings.py, urls.py, v.v. – heathen90

3

Bạn đang trên đường đến. Điều gì đang xảy ra là Heroku đang cố gắng sử dụng thiết lập STATICFILES_DIRS của bạn (/ home/kevin/web/django/appheroku/blogapp/static) tồn tại cục bộ trên máy của bạn nhưng sẽ không tồn tại trên máy chủ Heroku.

Một giải pháp đơn giản là để loại bỏ dòng đó từ biến staticfiles_dir vì vậy bạn phải:

STATICFILES_DIRS =() 

Sau đó STATICFILES_FINDERS mặc định của bạn sẽ kick vào và bạn sẽ được tốt để đi để chạy ứng dụng của bạn cả trong nước cũng như được triển khai trên Heroku.

+0

Cảm ơn, tôi hiểu ngay bây giờ. Giải pháp của bạn biến mất lỗi "Không có tệp hoặc dir", nhưng hiện tại có lỗi như sau: "Traceback (cuộc gọi gần đây nhất): OSError: [Errno 30] Hệ thống tệp chỉ đọc: '/ home/kevin' – heathen90

+0

Vì vậy, lỗi mới nên được cố định với khuyến nghị của Kay Zhu.Không có CSS ​​được hiển thị, các lỗi trong nhật ký heroku là gì khi bạn cố truy cập trực tiếp vào trang css? Bạn có thể đăng nhập bằng "heroku logs --tail "trên dòng lệnh để xử lý tốt hơn những gì treo lên là – eliotk

+0

GET xxx-xxx-number.herokuapp.com/static/css/style.css dyno = web.1 queue = 0 wait = 0ms service = 6ms status = 404 bytes = 2088 – heathen90

1

Đối với những ai sắp tới trong như tôi:

Giống như @eliotk ngụ ý,

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) 
STATIC_ROOT= os.path.join(PROJECT_DIR,'staticfiles/') 
STATICFILES_DIRS =() 

Đây là cấu hình bạn muốn tránh FileNotFoundError: [Errno 2] No such file or directory: lỗi hoặc OSError: [Errno 30] Read-only file system: lỗi.