django - 无法从apache中看到上传的媒体文件

时间:2013-09-05 14:18:13

标签: django apache django-settings django-media

我有两个不同的媒体和静态目录。当我通过本地服务器(localhost:8000)上传文件时,我可以看到本地服务器和apache服务器上传的图像。但是,当我用apache(localhost)上传它时,我看不到它们。我只能看到静电。但是当我重新加载本地服务器提供的页面(而不是再次运行服务器)时,我可以通过本地服务器和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服务器(localhost)上传时无法看到: enter image description here

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

1 个答案:

答案 0 :(得分:1)

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

这是html文件的片段:

{% 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>

希望这对某人有帮助!