0

Do I return it with httpResponse? Is there another way to call my image that's in a static folder?(in python file directly: usual process I follow is to save images in a static file. Then call that image in html file) but I'm trying to return an image in python file. Sorry if this isn't clear. I'll demonstrate with this code.

def extract(url):
    g = Goose()
    article = g.extract(url=url)
    if article.top_image is None:
        return "none" 

    else:
        if article.top_image.src is None:
          return "none"
        else:
            resposne = {'image':article.top_image.src}
            return article.top_image.src

Here instead of returning "none" I want to insert an image.

In views.py

 def form_valid(self, form):
        self.object = form.save(commit=False)
        # any manual settings go here
        self.object.moderator = self.request.user
        self.object.image = extract(self.object.url) 

        self.object.save()
        return HttpResponseRedirect(reverse('post', args=[self.object.slug]))

Then in html file, I use post.image.

Again, I'm just trying to return some random image that's in my static file to be displayed instead of "none". Not sure how to do this...thanks in advance

Edit: What I'm trying to do is this. in first block of code, I'm returing "none" right/instead of none I want to return an image I saved on my static folder. ex) image = "some code" and that image to be rendered in views.py and use post.image in html file/ right now if I do post.image it shows broken image because I'm returning "none"

class PostCreateView(CreateView):

     model = Post
     form_class = PostForm
     template_name = 'main/add_post.html'

     def form_valid(self, form):
            self.object = form.save(commit=False)
            # any manual settings go here
            self.object.moderator = self.request.user
            self.object.image = extract(self.object.url) 

            self.object.save()
            return HttpResponseRedirect(reverse('post', args=[self.object.slug]))

     @method_decorator(login_required)
     def dispatch(self, request, *args, **kwargs):
        return super(PostCreateView, self).dispatch(request, *args, **kwargs)       
mike braa
  • 647
  • 1
  • 12
  • 33
  • D you want to return an image in a view? That's already [asked and answered on SO](http://stackoverflow.com/questions/3003146/best-way-to-write-an-image-to-a-django-httpresponse) (and elsewhere on the internet). –  Jan 24 '16 at 05:12
  • @Evert yes I read that actually, and tried it but failed...so I got confsued that I uploaded the question. What I'm trying to do is this. in first block of code, I'm returing "none" right/instead of none I want to return an image I saved on my static folder. ex) image = "some code" and that image to be rendered in views.py and use post.image in html file/ right now if I do post.image it shows broken image because I'm returning "none" – mike braa Jan 24 '16 at 05:21
  • You're question is very confusing. What error are you getting. Reduce your question to the *actual* problem; you're trying to give too much context, distracting from the actual problem. –  Jan 24 '16 at 06:17

0 Answers0