简体   繁体   English

post_detail不显示图像

[英]post_detail doesnt display image

I have a really weird problem. 我有一个很奇怪的问题。 my index page shows image. 我的索引页面显示图像。 There isn't any error, codes are here: 没有任何错误,代码在这里:

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

but my detail page doesnt display image. 但是我的详细信息页面没有显示图像。 codes here: 代码在这里:

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

my post_detail.html page: 我的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>

my index.html 我的index.html

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

So what is my problem, thanks for time. 那我的问题是什么,谢谢您的时间。

firstly,make sure you have set the media dir in file settings.py conrrectly and then change your image url to: 首先,请确保已正确地在文件settings.py中设置了媒体目录,然后将图像网址更改为:

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

you missing a slash 你错过了一个斜线

You should use {{ post.image_thumb.url }} instead of {{ post.image_thumb }} . 您应该使用{{ post.image_thumb.url }}而不是{{ post.image_thumb }}

{{ post.image_thumb }} implicitly calls __unicode__ method of post, but what you want is the url attribute of image, you can get the absolute url of image with this. {{ post.image_thumb }}隐式调用post的__unicode__方法,但是您想要的是image的url属性,您可以由此获得image的绝对URL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NoReverseMatch at / Reverse for &#39;post_detail&#39; 未找到 - NoReverseMatch at / Reverse for 'post_detail' not found 评论未显示在post_detail视图中 - Comments not showing in post_detail view 未找到“post_detail”的反转。 “post_detail”不是有效的视图函数或模式名称 - Reverse for 'post_detail' not found. 'post_detail' is not a valid view function or pattern name 我无法创建post_detail和post_list - i can't create a post_detail and post_list NoReverseMatch at /post/new Reverse for &#39;post_detail&#39; 未找到。 “post_detail”不是有效的视图函数或模式名称 - NoReverseMatch at /post/new Reverse for 'post_detail' not found. 'post_detail' is not a valid view function or pattern name post_detail()视图缺少必需的位置参数: - post_detail() view missing required positional arguments: 未找到带有参数 &#39;(&#39;&#39;,)&#39; 的 &#39;post_detail&#39; 反转。 尝试了 1 个模式:[&#39;post/(?P<slug> [-\\\\w]+)/$&#39;] - Reverse for 'post_detail' with arguments '('',)' not found. 1 pattern(s) tried: ['post/(?P<slug>[-\\w]+)/$'] 反向使用关键字 arguments '{'pk': '', 'article_id': ''}' not found - Reverse for 'post_detail' with keyword arguments '{'pk': '', 'article_id': ''}' not found Django-在post_detail模板上显示每个用户的评论数 - Django - displaying each individual user's comment count on a post_detail template Django 2.3 在 get_absolute_url 中,找不到“post_detail”的反转 - Django 2.3 Inside get_absolute_url, Reverse for 'post_detail' not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM