Search code examples
pythondjangodjango-urls

Is it possible to have a media url and a satic url in django


This is my main urls.py file

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('base.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

can I have a url pattern for static files as well as a url pattern for media files?. Thanks


Solution

  • Yes that is is in fact very common, you simply add static twice: once for the media and once for the static files:

    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', include('base.urls'))
    ]
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

    Django however does not serve static files or media files in production: if you set the DEBUG setting [Djang-doc] to False, then these views will no longer serve data. You need to configure the webserver (like apache, nginx, etc.) to serve the files then.