我现在已经使用Django一段时间了,但我很擅长将网站投入生产。不幸的是,我使用了非Django友好的托管服务(HostGator)并使用FastCGI和教程here设置了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 %}
答案 0 :(得分:1)
staticfiles_urlpatterns()
不适用于制作,仅适用于DEBUG = True
。您需要单独提供静态文件。我从来没有在fcgi上使用django,但是我假设你可以相对容易地解决它。
正在yourdomain.com/static
查找文件,因为这是您为STATIC_URL
首先,将您的STATIC_ROOT
更改为公共目录。您应该在/home2/mlinsenb/public_html
放置index.fcgi
文件。在此处创建static
目录并更改您的STATIC_ROOT = "/home2/mlinsenb/public_html/static"
您可能希望对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提供文件。
答案 1 :(得分:0)
Heroku,其中包括recommend让Django使用WSGI插件提供静态文件。他们过去常常推荐dj-static
,现在他们推荐whitenoise。