我试图从一个docker卷中提供一个图像,但我不能完全掌握它。
错误信息
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)
项目/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>
应用程序似乎使用了正确的URL来提供文件,但似乎无法找到/访问MEDIA_ROOT
http://127.0.0.1:8000/fergana_api/files/36/movies/movie.mp4
如果文件100%存在,我如何使它实际上服务于位于/var/web/media/36/movies/movie.mp4
的文件?
1.请告诉我如果你需要更多的信息
1.应用程序包含在INSTALLED_APPS中
1.我说的只是开发者模式;不询问如何在生产中提供文件
新增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:
1条答案
按热度按时间ao218c7q1#
在Django + Docker中提供静态文件。你需要确保多个事情
1.卷在主机和客户机之间Map为静态卷。在您的情况下,您需要将类似
假设使用docker-compose(因为无论如何您都将拥有一个DB服务器)
或者在
Dockerfile
中Map它(如果不使用compose)