2013-09-05 47 views
2

我有兩個媒體和靜態不同的目錄。當我通過本地服務器(localhost:8000)上傳文件時,我可以通過本地服務器和apache服務器看到上傳的圖像。但是,當我用apache(localhost)上傳它時,我看不到它們。我只能看到靜態。但是當我重新加載由本地服務器(不再是runserver)服務的頁面時,我可以通過本地服務器和apache查看這些圖片。我究竟做錯了什麼?任何幫助將不勝感激。謝謝。django - 無法看到從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服務器(本地主機)上傳: enter image description here

只有當我重裝(本地主機:8000)由Django的服務器提供服務的頁面,我可以在由Apache服務的頁面上看到它。 enter image description here

+0

你的配置似乎沒問題,不知道有什麼問題。所以'runserver'正確地爲上傳的媒體提供站點,Apache不?不要同時在本地運行它們,這可能有助於您進行調試。 – HankMoody

+0

問題是easy_thumbnails !!!!!!我已經詳細回答了。雖然謝謝! – Robin

回答

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

後,我做了這些改變,對我來說是OK。

{% load static from staticfiles %} 

<!DOCTYPE html> 

<html> 
.... 
.... 

這:

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

希望這進來幫助的人!

相關問題