Serving static media during Django development: Why not MEDIA_ROOT?

前端 未结 3 1339
不知归路
不知归路 2020-12-08 15:35

I read this guide about serving static media with Django during development.

I noticed that MEDIA_URL and MEDIA_ROOT were not used in this.

3条回答
  •  死守一世寂寞
    2020-12-08 16:14

    The Django docs recommend the following approach I've modified for my use case:

    urlpatterns = [
        # url patterns
    ]
    
    from django.conf import settings
    
    if settings.DEBUG:
        from django.conf.urls.static import static
        urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    Note: the above assumes you've set your MEDIA_URL and MEDIA_ROOT correctly

    ... and here's the djangodocs linkslap.

提交回复
热议问题