帖子细节不显示图像

时间:2013-10-20 09:28:26

标签: python django

我有一个非常奇怪的问题。我的索引页面显示图像。没有任何错误,代码在这里:

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>    

所以我的问题是什么,谢谢你的时间。

2 个答案:

答案 0 :(得分:0)

首先,确保您已在文件settings.py中正确设置媒体目录,然后将图片网址更改为:

  <img src="/media/{{post.image_thumb}}" /> 
你错过了一个斜杠

答案 1 :(得分:0)

您应该使用{{ post.image_thumb.url }}代替{{ post.image_thumb }}

{{ post.image_thumb }}隐式调用__unicode__发布方法,但你想要的是图片的url属性,你可以用这个获得图像的绝对网址。