How do I get to know if an image is rendered in a webpage? (django templates)?

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

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.

Yeah, the url is the one on the same server. – Rahul Sharma Aug 4 '11 at 12:05 testing by URL is a bad thing to do if you know the full path that the image should be on the server... see other answers – Spacedman Aug 4 '11 at 13:29.

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 %} {% else %} {% 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.

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. – Rahul Sharma Aug 5 '11 at 7: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 5 '11 at 8:58 trying to implement a small hack to solve the problem, as of now I don't want to touch the database.

– Rahul Sharma Aug 5 '11 at 11:53.

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.

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 don't want to alter the database and want some quickfix in template, that which doesnot takes much resource. – Rahul Sharma Aug 4 '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 4 '11 at 13:28 You can't fix this just in the template since the template doesn't have direct access to the file system. You need to write some code in the view or possibly in a template tag.

– Spacedman Aug 4 '11 at 13:34.

Finally I have found a solution using sorl thumbnail : thumbnail.sorl.net/examples.html#templat... {% thumbnail item. Image my_size_string crop="left" as im %} {% empty %} No image {% endthumbnail %} it works for me.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions