0

I ahve this code, the trouble is few of the images are having names, but corresponding to the names the files are not present, how can i know if the images are being displayed, i mean some if statement of something like that.

<img src="{{ STATIC_URL }}images/{{web.image}}" />

The above code is used to render my django image in a template. Thanks.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
Ram Kumar
  • 590
  • 2
  • 10
  • 27

5 Answers5

1

I would do this on the client side, with a javascript, just google something like: "javascript image loaded" or something.

balazs
  • 5,698
  • 7
  • 37
  • 45
0

Are you saying that the URL expanded from {{ STATIC_URL }}images/{{web.image}} is resulting in a 404 Not Found error? And you want to figure out which ones are doing this?

You'd really need to test those URLs in your view by using some python code - something from urllib maybe - but if that URL is on the same server as the one running the view then it might just hang on you.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
0

It would be quite difficult to determine in the template whether the file actually exists or not. That would be the job of the view, assuming that {{web.image}} actually returns anything in the first place.

def my_view(request):
    # some code here
    import os
    if os.path.exists(web.image_path): # for example
        context = { 'web': web }
    render_to_response('my_template.html', context, RequestContext(request)

This assumes you actually know what the full file system path to the image file is. That's not always going to be the case, especially with the staticfiles app in django 1.3.

However, I'd be a lot more concerned that sometimes images exist, and sometimes they don't. Why is that the case? Are images being deleted for some reason? If they are, you need a way of cleaning up the database.

Edit:

Since there doesn't yet seem to be an answer that you like, you should try this:

import os
class MyModel(models.Model):
    image = models.Image...
    # the rest of your fields

    def get_image_url(self):
        if os.path.exists(self.image.path):
            return u'/%s/img/%s' % (self.app_label, self.image.filename)
        return u'%s/img/default.jpg' % self.app_label

In your template, you then do the following:

<img src="{{ STATIC_URL }}{{ web.get_image_url }}" />
Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
  • no the trouble is i have a folder with say 200 images, and in a database field i have names of say 160 of them, for the rest 40 images i want to display a default image, i dont want to alter the database and want some quickfix in template, that which doesnot takes much resource. – Ram Kumar Aug 04 '11 at 12:08
  • Then you need to use the python file utilities to check that foo-12345.png exists, otherwise set the image path to default.png and have an image called that. – Spacedman Aug 04 '11 at 13:28
  • You can't fix this just in the template since the template doesnt have direct access to the file system. You need to write some code in the view or possibly in a template tag. – Spacedman Aug 04 '11 at 13:34
0

If web is actually a database object that you are accessing (as your comment seems to point out), and that "image" is an ImageField, you could use the following :

{% if web.image %}
    <img src="{% web.image.url %}" />
{% else %}
    <img src="{{ STATIC_URL }}images/MyDefaultImage.png" />
{% endif %}

This way, the template will only provide the image if it exists, and provide an alternate image if not.

I apologize in advance if I misunderstood your question, I'm not so sure that I understood everything correctly.

Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
  • well thanks for your effort, but the thing which you misunderstood was, for every web.image there is a valid name, but for every valid name there is not an image file. so {% if web.image %} will always give a true. – Ram Kumar Aug 05 '11 at 07:35
  • Are you trying to implement a little hack to solve the problem then, or is it a long term solution you're looking for? I mean, are you planning on having new broken links? If not, you might be better off writing a basic script that checks whether a file exists and if.not, deletes the reference to it from the database. – Thomas Orozco Aug 05 '11 at 08:58
  • trying to implement a small hack to solve the problem, as of now i don't want to touch the database. – Ram Kumar Aug 05 '11 at 11:53
0

finally i have found a solution using sorl thumbnail : http://thumbnail.sorl.net/examples.html#template-examples

{% thumbnail item.image my_size_string crop="left" as im %}
    <img src="{{ im.url }}">
{% empty %}
    <p>No image</p>
{% endthumbnail %}

it works for me.

Ram Kumar
  • 590
  • 2
  • 10
  • 27