B
B
bogdan_242016-04-17 15:47:06
Django
bogdan_24, 2016-04-17 15:47:06

How to work with images in Django?

Started learning Django, can't figure out how to work with images.
models.py:

class Article(models.Model):
    title = models.CharField(max_length=100)
    text = models.TextField()
    image = models.ImageField(upload_to='images/', blank=True, null=True)
    is_published = models.BooleanField(default=False)

settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

html:
{% for a in article %}
            <div>
                <h1><a href="#">{{ a.title }}</a></h1>
                <img src="{{MEDIA_URL}}{{ a.image }}" />
            </div>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question