Display images in Django

前端 未结 5 902
遇见更好的自我
遇见更好的自我 2020-12-11 05:53

I have a django app which allows users to submit an image with it. Right now my model looks like

class Posting(models.Model):
    title = models.CharField(m         


        
5条回答
  •  难免孤独
    2020-12-11 05:59

    This is how i got it working.

    settings.py

    import os
    BASE_DIR = os.path.dirname(os.path.dirname(__file__))
    STATIC_URL = '/static/'
    
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, "static"),
    )
    
    MEDIA_ROOT = (
    BASE_DIR
    )
    
    
    MEDIA_URL = '/media/'
    

    models.py ...

    image = models.ImageField(upload_to='img')
    

    urls.py(project's)

    if settings.DEBUG:
    urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    template (.html)

    img
    

提交回复
热议问题