简体   繁体   English

Django-使用Apache / Webfaction在生产中提供Media / Admin文件

[英]Django - Serving Media/Admin files in production with Apache/Webfaction

As it stands my STATIC files are served without issue using the configuration below. 就目前而言,使用以下配置可以毫无问题地提供我的STATIC文件。

In my settings.py: 在我的settings.py中:

MEDIA_ROOT = '/home/chronic88/webapps/media_media/'
MEDIA_URL = '/media/'

STATIC_ROOT = '/home/chronic88/webapps/static_media/'
STATIC_URL = '/static/'

static_media and media_media are both applications served by apache. static_mediamedia_media都是apache服务的应用程序。

I can upload files via the admin and they show up inside the folder media_media , but they won't display on their pages. 我可以通过管理员上传文件,它们会显示在media_media文件夹中,但不会显示在其页面上。 When I check the file paths in the page source they seem correct mydomain.com/media/image.png but they simply won't display. 当我检查页面源文件中的文件路径时,它们似乎是正确的mydomain.com/media/image.png但它们根本不会显示。 So, it seems the link is there but there is some problem communicating between apache and django that I can't put my finger on. 因此,似乎链接存在,但是在apache和django之间通信时出现了一些我无法动弹的问题。

And my main urls.py: 而我的主要urls.py:

admin.autodiscover()

urlpatterns = patterns('',
    url(r'^', include('polls.urls', namespace="polls")),
    url(r'^admin/', include(admin.site.urls)),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

This is the urls.py I'm using in prodution that works. 这是我在生产中使用的有效的urls.py。 I tried it without the last line in production but it gives the same result (files are uploaded but not displayable). 我在生产中没有最后一行的情况下尝试了它,但结果却相同(文件已上传但无法显示)。

What am I missing? 我想念什么?

If you configured your Apache to serve the static files from the respective root folders, you also have to run manage.py collectstatic (see docs ). 如果您将Apache配置为从相应的根文件夹提供静态文件,则还必须运行manage.py collectstatic (请参阅docs )。

To test static and media files I usually have this in my urls.py: 为了测试静态和媒体文件,我通常在urls.py中包含以下内容:

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

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

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