ImageField Django template

后端 未结 1 739
北海茫月
北海茫月 2020-12-12 03:01

I\'m new in Django. I read a lot on documentation and on this site about my question. But I really can not understand what I should do to make this. Can someone show me with

相关标签:
1条回答
  • 2020-12-12 03:38

    In template, use:

    {% for post in posts %}
    <h1> {{post.title}} </h1>
    <img src="{{ post.img.url }}"> 
    {% endfor %}
    

    You have to add {{ post.img.url }}.

    Also, make sure that in urls.py, you have:

    from django.conf import settings
    from django.conf.urls.static import static
    
    urlpatterns = [
        # ... the rest of your URLconf goes here ...
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    It will Work.

    0 讨论(0)
提交回复
热议问题