django - 看不到从 apache 上传的媒体文件

Posted

技术标签:

【中文标题】django - 看不到从 apache 上传的媒体文件【英文标题】:django - can't see the uploaded media files from apache 【发布时间】:2013-09-09 10:26:04 【问题描述】:

我有两个不同的媒体目录和静态目录。当我通过本地服务器(localhost:8000)上传文件时,我可以看到本地服务器和 apache 服务器上传的图像。但是,当我使用 apache(localhost) 上传它时,我看不到它们。我只能看到静态。但是当我重新加载由本地服务器提供的页面时(不再是 runserver),我可以同时看到本地服务器和 apache 提供的这些图片。我究竟做错了什么?任何帮助将不胜感激。谢谢。

models.py 用于上传文件:

def get_upload_file_name(instance, filename):
    return "uploaded_files/%s_%s" %(str(time()).replace('.','_'), filename)

class Status(models.Model):
    status = models.TextField()
    image = models.ImageField(upload_to=get_upload_file_name, blank=True)
    pub_date = models.DateTimeField(default=datetime.now)
    creator = models.ForeignKey(User, related_name="creator_set")
    likes = models.ManyToManyField(User, through="Like")

    class Meta:
        ordering = ['-pub_date']
        verbose_name_plural = ('Status')

    def __unicode__(self):
        return self.status

settings.py:

MEDIA_ROOT = 'C:/Users/Robin/media'

MEDIA_URL = '/media/'

STATIC_ROOT = 'C:/Users/Robin/static/'

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    "C:/Users/Robin/web/leo/static",
)

http.conf:

WSGIScriptAlias / C:/Users/Robin/web/leo/leo/wsgi.py
WSGIPythonPath C:/Users/Robin/web/leo

<Directory C:/Users/Robin/web/leo>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

#Alias /robots.txt /path/to/mysite.com/static/robots.txt
#Alias /favicon.ico /path/to/mysite.com/static/favicon.ico

AliasMatch ^/([^/]*\.css) C:/Users/Robin/static/styles/$1

Alias /media/ C:/Users/Robin/media/
Alias /static/ C:/Users/Robin/static/

<Directory C:/Users/Robin/static>
Order deny,allow
Allow from all
</Directory>

<Directory C:/Users/Robin/media>
Order deny,allow
Allow from all
</Directory>

WSGIScriptAlias / C:/Users/Robin/web/leo/leo/wsgi.py

<Directory C:/Users/Robin/web/leo/leo>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>

urls.py:

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', 'leo.views.home', name='home'),
    url(r'^home/', include('status.urls')),
    url(r'^gallery/$', 'leo.views.gallery'),
    url(r'^mypage/$', 'leo.views.mypage'),

    # Accounts
    url(r'^please_login/$', 'leo.views.please_login'),
    url (r'^accounts/auth_view/$', 'leo.views.auth_view'),
    url (r'accounts/register/$', 'leo.views.register_user'),
    url (r'register_success/$', 'leo.views.register_success'),
    url (r'^accounts/loggedin/$', 'leo.views.loggedin'),
    url (r'^accounts/invalid/$', 'leo.views.invalid_login'),
    url (r'^accounts/logout/$', 'leo.views.logout'),

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

.html:

            % block content %
                <div class="status">
                    <div class="inner">
                        % for Status in status %
                            <p class="user"> Status.creator.get_full_name </p>
                            % if Status.image %
                                <div class="image_image">
                                    <center>
                                    <img src="Status.image |thumbnail_url:'status'"/>
                                    </center>
                                </div>
                                <p class="status_image">Status</p>
                                <span class="clear"></span>
                                <hr>
                            % else %
                                <p class="status_status">Status</p>
                                <span class="clear_right"></span>
                                <hr>
                            % endif %
                        % endfor %
                    </div>
                </div>
            % endblock %

使用 apache 服务器(本地主机)上传时看不到:

只有当我重新加载 django 的服务器(localhost:8000)提供的页面时,我才能在 apache 提供的页面上看到它。

【问题讨论】:

您的配置看起来不错,不知道哪里出了问题。那么runserver 通过上传的媒体正确地为站点提供服务,而 Apache 没有?不要同时在本地运行它们,这可能有助于您调试。 问题是easy_thumbnails!!!!!!我已经详细回答过了。不过谢谢! 【参考方案1】:

由于 index.html 文件中的 % load_thumbnai %,直接从 apache 服务器上传图像时未加载图像。

这是html文件的sn-p:

% load static from staticfiles %
% load thumbnail %

<!DOCTYPE html>

<html>
    <head>
        <title>leo</title>
        <link rel="stylesheet" type="text/css" href="% static "css/style.css" %">
    </head>
....
....

这是图片的另一部分:

                        % for Status in status %
                            <p class="user"> Status.creator.get_full_name </p>
                            % if Status.image %
                                <div class="image_image">
                                    <center>
                                    <img src="Status.image |thumbnail_url:'status'"/>
                                    </center>
                                </div>
                                <p class="status_image">Status</p>
                                <span class="clear"></span>
                                <hr>
                            % else %
                                <p class="status_status">Status</p>
                                <span class="clear_right"></span>
                                <hr>
                            % endif %
                        % endfor %

在我进行这些更改后,对我来说没问题。

% load static from staticfiles %

<!DOCTYPE html>

<html>
....
....

还有这个:

                            <div class="image_image">
                                <center>
                                <img src=" MEDIA_URL Status.image" width=300px />
                                </center>
                            </div>

希望这对某人有所帮助!

【讨论】:

以上是关于django - 看不到从 apache 上传的媒体文件的主要内容,如果未能解决你的问题,请参考以下文章

django+nginx+uwsgi,文件浏览器不上传

错误:在 apache 上运行 django 时找不到目标 WSGI 脚本或无法统计

安全的 django 文件权限

CentOS 7下使用Apache2部署Django项目,解决文件名中含有中文报错的问题

docker容器中的django + nginx:无法上传任何带有表单的文件

Django-CKedtior图片找不到的问题