簡體   English   中英

Django靜態文件未在生產中加載

[英]Django staticfiles aren't loading in production

我已經使用Django一段時間了,但是對於將網站投入生產來說,我是一個新手。 不幸的是,我使用的是非Django友好的托管服務(HostGator),並使用FastCGI和此處的教程設置了Django。 Django版本是1.3 ... :(

該站點已啟動,但是由於某些原因,靜態文件無法加載。 調試輸出顯示正在my_domain / static /查找文件,我不確定這是正確的。

我無權編輯任何服務器配置或任何內容,因此我只能通過Django代碼解決此問題。 感謝您的幫助。 (代碼將跟隨)

settings.py

MEDIA_ROOT = "/home2/mlinsenb/django/projects/website/media"

MEDIA_URL = '/media/'

STATIC_ROOT = "/home2/mlinsenb/django/projects/website/static"

STATIC_URL = "/static/"

STATICFILES_DIRS = ()

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

TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
#'django.template.loaders.eggs.Loader',
)

TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
)

urls.py

from django.conf.urls.defaults import patterns, include, url
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('homepage.views',
url(r'^$', 'home'),
url(r'^blog/$', 'blog'),
url(r'^programming/$', 'programming'),
url(r'^music/$', 'music'),
url(r'^about/$', 'about'),
url(r'^contact/$', 'contact'),

# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
)

urlpatterns += staticfiles_urlpatterns()

home.html的

{% extends "homepage/base.html" %}
{% block content %}
<h1>Hi, I'm Max.</h1>
<img src="{{ STATIC_URL }}img/me.jpg" alt="Serious business" height="300px" width="300px">
{% endblock %}

staticfiles_urlpatterns()不是為生產設計的,僅在DEBUG = True 您需要分別提供靜態文件。 我從未將django與fcgi一起使用,但我假設您可以相對輕松地實現這一目標。

yourdomain.com/static中查找文件,因為這是您為STATIC_URL設置的

首先,將您的STATIC_ROOT更改為公共目錄。 您應該將/home2/mlinsenb/public_html內容放置在index.fcgi文件中。 在此處創建static目錄,然后更改您的STATIC_ROOT = "/home2/mlinsenb/public_html/static"

您也可能希望對MEDIA_ROOT進行相同的操作, MEDIA_ROOT將來使用。

如果您遵循發布的指南,則您的.htaccess應如下所示:

AddHandler fcgid-script .fcgi
Options +FollowSymLinks
RewriteEngine On

RewriteRule (media/.*)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(admin/.*)$ index.fcgi/$1 [L]

再說一次,我不是mod_rewrite的專家,但是看起來他們用它來處理媒體文件,所以我會嘗試為您的靜態文件添加一行:

AddHandler fcgid-script .fcgi
Options +FollowSymLinks
RewriteEngine On

RewriteRule (media/.*)$ - [L]
RewriteRule (static/.*)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(admin/.*)$ index.fcgi/$1 [L]

然后運行python manage.py collectstatic和django應該將所有靜態文件放在/home2/mlinsenb/public_html/static

現在hostgator應該從該目錄提供服務到yourdomain.com/static。

Heroku等建議僅讓Django使用WSGI插件來提供靜態文件。 他們曾經推薦dj-static ,現在推薦whitenoise

暫無
暫無

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

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