Django 3 url - view must be a callable or a list/tuple in the case of include()

I am beginner in Python and Django. I migrate project from 2.x to 3. I have this code:

urlpatterns += [
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
    ]

and this code return me error:

url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
  File "/mnt/c/Projekty/GO/.venvs/lib/python3.9/site-packages/django/conf/urls/__init__.py", line 22, in url
    return re_path(regex, view, kwargs, name)
  File "/mnt/c/Projekty/GO/.venvs/lib/python3.9/site-packages/django/urls/conf.py", line 73, in _path
    raise TypeError('view must be a callable or a list/tuple in the case of include().')
TypeError: view must be a callable or a list/tuple in the case of include().

How can i repair it? Please help me

You need to work with the view itself, not reference it by name:

from django.views.static import serve

urlpatterns += [
    url(
        '^media/(?P<path>.*)$',
        serve,
        {
            'document_root': settings.MEDIA_ROOT,
        },
    ),
]

But this path is not a good idea in the first place. The author here wanted to "bypass" the fact that Django does not serve static or media files in production (DEBUG = False), but that is for good reasons: serving files by Django is likely very inefficient both in processing time and memory usage, it also does not use caching effectively, and finally the view is not really made to be very safe.

You normally configure the webserver like nginx, apache, or some other to serve these files, see the serving static files from a dedicated server section [Django-doc] for more information.

Back to Top