How to get an ImageField URL within a template?

前端 未结 4 424
执笔经年
执笔经年 2020-11-30 10:15

I\'ve got the following model in my Django app:

class Image(models.Model):
    image = models.ImageField(
        upload_to=\'images/\', 
        height_fiel         


        
4条回答
  •  情书的邮戳
    2020-11-30 10:46

    Create a folder "static" in you're django project and add this to you're settings file

    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static'),
    )
    

    If you don't have this line in you're settings file also add it

    BASE_DIR = os.path.dirname(os.path.dirname(__file__))
    

    Now in the template you can use

    {% static  image.image.url %}
    

提交回复
热议问题