Home > Software engineering >  How to use both static files and media together
How to use both static files and media together

Time:12-20

Is it possible to use both static files and media in a project? because all tutorials use only one of them.

MEDIA_URL= 'media/'
MEDIA_ROOT = BASE_DIR / 'media'

STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'static/'
STATICFILES_DIRS = BASE_DIR / 'static/

I wrote this to setting. How am supposed to modify urls.py?

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('pages.urls')),
    path('users/',include('users.urls')),
    path('users/',include('django.contrib.auth.urls')),
    ]   static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

I wrote it this way but how should I add static urls?

CodePudding user response:

You add the two lists generated by the static functions, so:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('pages.urls')),
    path('users/', include('users.urls')),
    path('users/', include('django.contrib.auth.urls')),
]
urlpatterns  = static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns  = static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Note however that Django does not serve static or media files in production, and that you this will have to set up nginx, apache, or another webserver to do so.

  • Related