I'm trying to serve an image from a docker volume, but I can't quite get a hang of it.
error message
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/fergana_api/files/36/movies/movie.mp4
Using the URLconf defined in fergana_api.urls, Django tried these URL patterns, in this order:
admin/
api/schema/ [name='api-schema']
api/docs/ [name='api-docs']
api/
[name='all-runs']
tests/<slug:test_session_id> [name='single-run']
tests/<slug:test_session_id>/<slug:test_name> [name='single-test']
^static/(?P<path>.*)$
^files/(?P<path>.*)$
The current path, fergana_api/files/36/movies/movie.mp4, didn’t match any of these.
You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
settings.py
STATIC_URL = 'static/'
MEDIA_URL = 'files/'
MEDIA_ROOT = '/var/web/media/'
STATIC_ROOT = BASE_DIR / 'static_files'
# location of static files
STATICFILES_DIRS = [
BASE_DIR / 'static'
]
app/views.py
class SingleTestView(View):
def get(self, request, test_session_id, test_name):
run = Runner.objects.get(id=test_session_id)
path_to_session = to_file('files/', f'{test_session_id}')
movies_dir_path = to_file(path_to_session, 'movies')
movie_path = to_file(movies_dir_path, test_name.replace('-', '_') + '.mp4')
context = {
'movie_path': movie_path
}
return render(request, "presenter/single_test.html", context)
project/url.py
if settings.DEBUG:
urlpatterns += static(
settings.STATIC_URL, document_root=settings.STATIC_ROOT
)
urlpatterns += static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT
)
single_test.html
<video width="320" height="240" controls>
<source src="{{ movie_path }}" type="video/mp4">
</video>
It seems like the app uses the correct URL to serve files, but it doesn't seem like it can find/access the MEDIA_ROOT
http://127.0.0.1:8000/fergana_api/files/36/movies/movie.mp4
How do I actually make it serve the file located at /var/web/media/36/movies/movie.mp4
given that the file 100% exists?
- Please, tell me if you need any more info
- app is included into the INSTALLED_APPS
- I'm talking about developer mode only; not asking how to serve files in prod
Added docker-compose
services:
app:
build:
context: .
dockerfile: fergana-api.dockerfile
args:
- DEV=true
ports:
- '8000:8000'
volumes:
- ./fergana_api:/fergana_api
- static-data:/vol/web
volumes:
static-data: