How do I include image files in Django templates?

前端 未结 11 1694
时光说笑
时光说笑 2020-11-27 10:35

I\'m new to Django and I\'m trying to learn it through a simple project I\'m developing called \'dubliners\' and an app called \'book\'. The directory structure is like this

11条回答
  •  萌比男神i
    2020-11-27 11:13

    If your file is a model field within a model, you can also use ".url" in your template tag to get the image.

    For example.

    If this is your model:

    class Foo(models.Model):
        foo = models.TextField()
        bar = models.FileField(upload_to="foo-pictures", blank = True)  
    

    Pass the model in context in your views.

    return render (request, "whatever.html", {'foo':Foo.objects.get(pk = 1)})
    

    In your template you could have:

    
    

提交回复
热议问题