如何从模板访问Django项目之外的文件

时间:2019-07-20 11:24:20

标签: python django static storage

我正在研究一个python项目,其中我的树莓派将帧从相机发送到我的服务器。服务器将这些帧显示为视频,并在检测到运动时将其另存为'/mnt/cameras/"year-month-day"/"hour-min-sec".webm。一切正常,但由于这些保存的视频不在我的项目中,因此我无法在我的网页上使用它们。我确定可以播放这些视频,因为当我将它们放入“媒体”文件夹后,它确实可以工作。

在我的settings.py中:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(REPOSITORY_ROOT, 'static/')


MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(REPOSITORY_ROOT, 'media/')

CAMERAS_URL = '/cams/'
CAMERAS_ROOT = '/mnt/cameras/'

在我的urls.py中:

from django.urls import path
from . import views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', views.camerapage, name="cameraspage"),
    path('camera<cam_number>',views.livefe, name='camera'),
    path('browse/',views.browsepage, name='browse'),

]+static(settings.CAMERAS_URL, document_root=settings.CAMERAS_ROOT)

在我的views.py中:

videos_path = "/mnt/cameras"


def browsepage(request):

    class Video:
        path = ''
        title = ''

        def __init__(self,path,title):
            self.path = path
            self.title = title

        def __str__(self):
            return self.title

    class Date:
        date = ''
        path = ''
        videos = []

        def __init__(self,path,date):
            self.path = path
            self.date = date

        def __str__(self):
            return self.date

    dates = []

    for dir_or_file in os.listdir(videos_path):
        date_folder_path = os.path.join(videos_path,dir_or_file)


        if os.path.isdir(date_folder_path):
            new_date = Date(date_folder_path,dir_or_file)
            for possible_file in os.listdir(date_folder_path):

                file_path = os.path.join(date_folder_path,possible_file)
                if os.path.isfile(file_path):
                    new_video =  Video(os.path.join(dir_or_file,possible_file),possible_file)
                    new_date.videos.append(new_video)
            dates.append(new_date)


    return render(request=request,
                  template_name="cameras/browsepage.html",
                  context={"dates":dates})

在我的browserpage.html中:

{% block content %}

<video class="responsive-video z-depth-5" controls>
    <source src="/media/Cameras/12-00-43.webm" type="video/webm">
</video>
    <ul class="collapsible z-depth-5">
        {% for date in dates %}
            <li>
                <div class="collapsible-header grey darken-3 blue-text"><i class="material-icons"></i><strong>{{date}}</strong></div>

                <div class="collapsible-body grey darken-2 orange-text">
                    {% for video in date.videos %}
                        {% if date.date in video.path %}
                            <video class="responsive-video z-depth-5" controls>
                                <source src="/cameras/{{video.path}}" type="video/webm">
                            </video>
                            <h4>{{video.title}}</h4>
                        {% endif %}
                    {% endfor %}
                </div>
            </li>
        {% endfor %}
    </ul>
{% endblock %}

请注意,<source src="/media/Cameras/12-00-43.webm" type="video/webm">可以工作,<source src="/cameras/{{video.path}}" type="video/webm">不能工作。

例如,当我在浏览器中检查无法正常工作的文件时,src例如变为“ /cameras/2019-07-06/11-57-00.webm”,我认为这是正确的,因为“ / cameras /”实际上是>'/ mnt / cameras /',因此它变成了'/mnt/cameras/2019-07-06/11-57-00.webm',这是现有路径。

0 个答案:

没有答案