Sunday, September 24, 2017

Re: Rendering DataBase Image File

Hi,

You need to put this in your template:

<img src="{{ object.photo.url }}">

You must make sure that settings MEDIA_ROOT and MEDIA_URL are set correctly.

Regards,

Antonis

Antonis Christofides  http://djangodeployment.com
On 2017-09-24 12:04, yingi keme wrote:
Hello

Ok i have a simple model that has an ImageField to store image files

class Product(models.Model):
    category = models.ForeignKey('CatalogCategory',
    related_name='products')
    name = models.CharField(max_length=300)
    photo = models.ImageField(upload_to='Home/static/Home/img',
    blank=True)
    price_in_dollars = models.DecimalField(max_digits=6,
    decimal_places=2)

The images are uploaded to static img folder.

How do you render the image in your template?

My Template file looks like this

<h1>{{ object.name }}</h1>
<p>{{ object.description }}</p>
    <br>
    {{ object.photo }}
<p>Price: ${{ object.price_in_dollars }}</p>

But the image file doesnt display {{object,photo}} , only the other items get to show.

How do you render ImageField in your templates?
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/71d39500-4e57-4220-bdd0-dd2900b7e499%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment