簡體   English   中英

post_detail不顯示圖像

[英]post_detail doesnt display image

我有一個很奇怪的問題。 我的索引頁面顯示圖像。 沒有任何錯誤,代碼在這里:

def index(request):
    post = Post.objects.get(id=1)
    return render_to_response("index.html", {"post":post}, context_instance=RequestContext(request))

但是我的詳細信息頁面沒有顯示圖像。 代碼在這里:

class PostDetailView(DetailView):
    context_object_name = "post"
    model = Post
    template_name = "post_detail.html"

    def get(self, request, *args, **kwargs):
        self.next = self.request.get_full_path()
        return super(PostDetailView, self).get(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(PostDetailView, self).get_context_data(**kwargs)
        context["form"] = CommentForm()
        context["next"] = self.next
        context["object_id"] = context['post'].id
        context["comments"] = Comment.objects.filter(
            content_type=ContentType.objects.get_for_model(self.model),
            object_id=context['post'].id, is_published=True)

        return context

我的post_detail.html頁面:

{{ post.title }}  -----here ok!
{{ post.content }} ------here ok!
<div class="img">
    {% if post.image %}
        <img src="media/{{post.image_thumb}}" /> -----but here not ok.Why?
    {% endif %}
</div>

我的index.html

{{ post.title }}
{{ post.content }}
<div class="img">
    {% if post.image %}
        <img src="media/{{post.image_thumb}}" />
    {% endif %}
</div>    

那我的問題是什么,謝謝您的時間。

首先,請確保已正確地在文件settings.py中設置了媒體目錄,然后將圖像網址更改為:

  <img src="/media/{{post.image_thumb}}" /> 

你錯過了一個斜線

您應該使用{{ post.image_thumb.url }}而不是{{ post.image_thumb }}

{{ post.image_thumb }}隱式調用post的__unicode__方法,但是您想要的是image的url屬性,您可以由此獲得image的絕對URL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM