Sunday, September 24, 2017

Re: Rendering DataBase Image File

Okk. I will like to know how to set the MEDIA_ROOT and MEDIA_URL.?

And then, my images are uploaded to a directory named 'img' inside my static files directory. Does it affect how you set the MEDIA_ROOT and MEDIA_URL?



Yingi Kem

On 24 Sep 2017, at 1:56 PM, Antonis Christofides <antonis@djangodeployment.com> wrote:

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.

--
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/ebced5a4-1274-f268-3b67-02ea5f82d2ae%40djangodeployment.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment