页面未找到|无法使用Django提供媒体文件

zphenhs4  于 2023-05-30  发布在  Go
关注(0)|答案(1)|浏览(114)

这就是导致问题的代码:

<video width="320" height="240" controls>
  <source src="{{ movie_path }}" type="video/mp4">
</video>

错误信息:

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']
^files/(?P<path>.*)$
^static/(?P<path>.*)$
The current path, vol/web/media/36/movies/movie.mt4, didn’t match any of these.

我试图从项目之外的卷提供mp4文件。我想我可以通过提供文件的绝对路径来呈现页面,因为如果我试图在浏览器中将该页面作为html文件打开,我就会这样做。
但这似乎并不奏效,虽然我看到**'当前路径'看起来完全像它应该看起来。
不确定在本例中是否需要,但它是应用程序的
urls.py**

urlpatterns = [
    path('', views.AllRunsView.as_view(), name='all-runs'),
    path('tests/<slug:test_session_id>', views.SingleRunView.as_view(), name='single-run'),
    path('tests/<slug:test_session_id>/<slug:test_name>', views.SingleTestView.as_view(), name='single-test'),
]

views.py

class SingleTestView(View):
    def get(self, request, test_session_id, test_name):
        run = Runner.objects.get(id=test_session_id)

        movies_dir_path = to_file(run.artifacts_dir_path, '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)

settings.py

# under what url to serve static files in prod
STATIC_URL = '/static/'
MEDIA_URL = '/files/'

MEDIA_ROOT = BASE_DIR / 'media'
STATIC_ROOT = BASE_DIR / 'static_files'

# location of static files
STATICFILES_DIRS = [
    BASE_DIR / 'static'
]

电影目录的绝对路径:/vol/web/media/36/movies/movie.mt4

mrzz3bfm

mrzz3bfm1#

你需要提供一个url而不是路径作为src:

<source src="{{ movie_path }}" ....

movie_path需要类似于

files/36/movies/movie.mp4

因为您将MEDIA_URL='files'设置为

^files/(?P<path>.*)$

相关问题