简体   繁体   中英

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.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

{{ 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM