3

I have uploaded images in django database, but I'm not able to show those images on the HTML page.

The image name is being fetched but it is not being shown. This is my code:

models.py

image=models.FileField(upload_to='images',blank=True)

def save(self, *args, **kwargs):
    super(Movie, self).save(*args, **kwargs)
    filename=self.image.url

results.html

<table>
  <td>
    <tr>{% for i in p %}</tr>
    <tr>{{i.movie_name}}</tr>
    <tr>{{i.description}}</tr>
    <tr>{{i.release_date}}</tr>
    <tr>{{i.ticket_price}}</tr>
    <img src="{{i.image}}"/>
    <tr>{% endfor %}</tr>
  </td>
</table>

Please tell me what changes I have to make in the views.py file

Luka Kerr
  • 4,161
  • 7
  • 39
  • 50
Navita Saini
  • 362
  • 3
  • 12
  • 3
    Possible duplicate of [How to get an ImageField URL within a template?](http://stackoverflow.com/questions/8850535/how-to-get-an-imagefield-url-within-a-template) (the question asks about `ImageField` but it is the same for `FileField`. You should consider using `ImageField` anyway.) – solarissmoke May 03 '16 at 06:23

1 Answers1

0

Instead of using

<img src="{{i.image}}"/>

in your results.html, use this :

<img src="{{ i.image.url }}"/>

Prateek Gupta
  • 2,422
  • 2
  • 16
  • 30