0

What I have done: in models.py:

class Image(models.Model):
  img = models.ImageField(blank=True, Null=True)

And now I want to acces the image in the template. I tried it, but it didn't work: (myimg is an instance of Image.)

<img src="{{ myimg.img }}">

So: How do I acces the image from the template?


Another thing: when I used my model like that the images where stored at the projects root. This doesn't seem to be a good idea. Where should they be stored? And how do I store them somewhere else.


Some other little questions which deal with that problem:

  • What is MEDIA_URL good for and how does it work?
  • What is MEDIA_ROOT good for and how does it work?
  • Where should files that aren't static (that means files a user or an admin uploads) be stored?
Asqiir
  • 607
  • 1
  • 8
  • 23

1 Answers1

0

MEDIA_ROOT is the location where your images are stored. If they are currently being stored at the project root, that is because that's what you have defined as MEDIA_ROOT; don't do that. You can put them wherever makes sense on your system, though.

MEDIA_URL is the URL at which your images are served. It's up to you to configure your webserver to do that; Django won't (although it can be made to do it in development mode only.)

To access the URL of an image field in the template, use the url property:

<img src="{{ myimg.img.url }}">
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895