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

时间:2021-01-15 19:21:41

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.

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

models.py for uploading file:

模型。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:

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:

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

Can't see when uploaded with apache server(localhost): django—无法看到从apache上传的媒体文件

无法查看何时与apache服务器(localhost)上传:

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. django—无法看到从apache上传的媒体文件

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

1 个解决方案

#1


1  

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

由于索引中的{% load_thumbnai %},当从apache服务器直接上传图像时,图像没有被加载。html文件。

Here's the snippet of html file:

这是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>
....
....

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

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

在我做了这些改变之后,我觉得还可以。

{% load static from staticfiles %}

<!DOCTYPE html>

<html>
....
....

And this:

这:

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

Hope this comes in help for someone!

希望这对某人有帮助!

#1


1  

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

由于索引中的{% load_thumbnai %},当从apache服务器直接上传图像时,图像没有被加载。html文件。

Here's the snippet of html file:

这是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>
....
....

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

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

在我做了这些改变之后,我觉得还可以。

{% load static from staticfiles %}

<!DOCTYPE html>

<html>
....
....

And this:

这:

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

Hope this comes in help for someone!

希望这对某人有帮助!