Django的 - 不能看到来自Apache上传媒体文件 [英] django - can't see the uploaded media files from apache

查看:166
本文介绍了Django的 - 不能看到来自Apache上传媒体文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对媒体和静态两种不同的目录中。正如我在本地服务器上传文件(本地主机:8000),我既可以通过在本地服务器和Apache服务器看到上传的图片。然而,当我把它上传与Apache(本地主机),我看不到他们。我只能看到静态的。但是,当我重​​新加载本地服务器(而不是再次RUNSERVER)担任该页面,然后我就可以通过在本地服务器和Apache看到那些照片都。我究竟做错了什么?任何帮助将AP preciated。谢谢你。

I have two different directory for both media and static. And as I upload the files through the local server (localhost:8000), I can see the uploaded images both by the local server and the apache server. However, when I upload it with apache(localhost), I can't see them. I can only see the static. But when I reload the page served by the local server (not runserver again), I can then see those pictures both by the local server and the apache. What am I doing wrong? Any help will be appreciated. Thank you.

models.py上传文件:

models.py for uploading file:

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:

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:

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:

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服务器(本地主机)上传:

Can't see when uploaded with apache server(localhost):

只有当我重装了Django的服务器提供服务的页面(本地主机:8000),我可以看到它由Apache服务的页面上。

Only when I reload the page served by the django's server(localhost:8000), I can see it on the page served by the apache.

推荐答案

在从因为Apache服务器直接上传的图像不加载 {%load_thumbnai%} 在index.html文件。

The images were not loading when uploaded directly from the apache server because of the {% load_thumbnai %} in the index.html file.

下面的HTML文件的片段:

Here's the snippet of html file:

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

这就是它的形象,另一部分:

And here's the other part of it for image:

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

在我做了这些改变,对我来说是OK。

After I made these changes, it was OK for me.

{% load static from staticfiles %}

<!DOCTYPE html>

<html>
....
....

和这样的:

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

希望这进来帮助的人!

Hope this comes in help for someone!

这篇关于Django的 - 不能看到来自Apache上传媒体文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆