2014-07-24 2 views
0

내 Django 관리자 페이지는 올바른 디렉토리 (.../project/media/article /)에 이미지를 저장하지만, Article.image.url을 사용하여 이미지를 템플릿 URL은 다음 HTTP를 가리키는 : // 로컬 호스트 /article/image.jpg를하지 않고 HTTP보다 : // localhost를 내가 좋은 4 시간 노력 아마 썼다Django ImageField 링크가 잘못된 디렉토리

/media/article/cat.jpg 이 문제를 해결하려면 아무 소용이 없습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 템플릿은 이미지가 있어야하는 기본 '이미지 누락'/ '깨진 링크'아이콘으로 완벽하게 렌더링됩니다. 사전

에서

덕분에이 내 poject URL을 파일입니다

urlpatterns = patterns('', 
url(r'^admin/', include(admin.site.urls)), 
... 
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': MEDIA_ROOT}), 
) 

그리고 여기에 관련 설정 정보입니다 :

STATIC_ROOT = os.path.join(os.path.dirname(os.path.dirname(__file__)),'app','static') 
STATIC_URL = '/app/static/' 
MEDIA_ROOT = os.path.join(os.path.dirname(os.path.dirname(__file__)),'media') 
MEDIA_URL = '/' 

그리고 관련 모델 정보 :

class Article(models.Model): 
    image = models.ImageField(upload_to = 'article/', blank=True) 
    image_caption = models.CharField(max_length=100, blank=True) 

UP DATE는 :

MEDIA_URL = '/' 

에 :

{% if article %} 
      <!-- Large/Medium (big desktops) --> 
      <li class='list-group-item hidden-xs hidden-sm fourColumns'> 
       <div class='panel'> 
        <div class='panel-heading'> 
         {{ article.pub_date }} 
        </div> 
        <div class='panel-body'> 
         By {{ article.writer }} 
        </div> 
       </div> 
       {% if article.pull_quote and not article.image %} 
        {{ article.article|truncwords:'50'|linebreaks }} 
        <blockquote> 
         <p> 
          {{ article.pull_quote }} 
         </p> 
        </blockquote> 
        {{ article.article|truncfrom:'50'|linebreaks }} 
       {% elif article.image and article.image_caption and not article.pull_quote %} 
        {{ article.article|truncwords:'50'|linebreaks }} 
        <img src='{{ article.image.url }}' class='img-responsive'/> 
        <em>{{ article.image_caption }}</em><br/><br/> 
        {{ article.article|truncfrom:'50'|linebreaks}} 
       {% elif article.image and not article.image_caption and not article.pull_quote %} 
        {{ article.article|truncwords:'50'|linebreaks}} 
        <img src='{{ article.image.url }}' class='img-responsive'/> 
        {{ article.article|truncfrom:50|linebreaks }} 
       {% elif article.image and article.image_caption and article.pull_quote %} 
        {{ article.article|truncwords:'50'|linebreaks }} 
        <blockquote> 
         <p> 
          {{ article.pull_quote }} 
         </p> 
        </blockquote> 
        {{ article.article|truncfromto:'50,100'|linebreaks }} 
        <img src='{{ article.image.url }}' class='img-responsive'/> 
        <em>{{ article.image_caption }}</em><br/><br/> 
        {{ article.article|truncfrom:'100'|linebreaks}} 
       {% elif article.image and article.pull_quote and not article.image_caption %} 
        {{ article.article|truncwords:'50'|linebreaks }} 
        <blockquote> 
         <p> 
          {{ article.pull_quote }} 
         </p> 
        </blockquote> 
        {{ article.article|truncfromto:'50,100'|linebreaks }} 
        <img src='{{ article.image.url }}' class='img-responsive'/> 
        {{ article.article|truncfrom:'100' }} 
       {% else %} 
        {{ article.article|linebreaks }} 
       {% endif %} 
      </li> 
     {% endif %} 
+0

이미지를 얻으려면 템플릿 html 코드가 어떻게 생깁니 까? –

+0

@AronYsidoro 질문을 템플릿으로 업데이트했습니다. 내 생각에 실제로 중요한 부분은 태그 중 하나이지만 주변 텍스트도 포함되어 있습니다. – cdipaolo

답변

1

하는 변경해보십시오 :

MEDIA_URL = '/media/' 

그리고 변화도 시도 :

<img src='{{ article.image.url }}' 

에 : 여기에 관련 템플릿 HTML입니다

<img src="{{ MEDIA_URL }}{{ article.image }}" > 
+0

고마워요! 나는 단지 첫 부분을 해보려고 노력했지만, 그걸로 잘 작동했지만, 진지하게 나에게 많은 좌절을 안겨 줬다! – cdipaolo

관련 문제