前端获取服务器文件列表,Django-在模板中显示文件列表,从服务器的MEDIA文件夹中获取文件...

我想以Django应用程序前端的形式显示服务器上MEDIA_ROOT中的文件列表。

以下是我要完成的工作:

eo8Mn.png

我的实际代码如下。我只显示受此问题影响的类,函数和文件。在问题的结尾,如果缺少某些内容,您将找到指向整个项目的链接。

views.py(我有两个功能,因为我尝试了两种方法)

class SelectPredFileView(TemplateView):

"""

This view is used to select a file from the list of files in the server.

"""

model = FileModel

fields = ['file']

template_name = 'select_file_predictions.html'

success_url = '/predict_success/'

files = os.listdir(settings.MEDIA_ROOT)

def my_view(request):

my_objects = get_list_or_404(FileModel, published=True)

return my_objects

# TODO: file list not displayed in the HTML, fix

def getfilelist(self, request):

filepath = settings.MEDIA_ROOT

file_list = os.listdir(filepath)

return render_to_response('templates/select_file_predictions.html', {'file_list': file_list})

settings.py

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static")

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, "media")

models.py

from django.db import models

from django.conf import settings

class FileModel(models.Model):

file = models.FileField(null=True, blank=True)

timestamp = models.DateTimeField(auto_now_add=True)

path = models.FilePathField(path=settings.MEDIA_ROOT, default=settings.MEDIA_ROOT)

urls.py

from django.contrib import admin

from django.conf import settings

from django.urls import path, re_path

from django.views.static import serve

from django.conf.urls import url, include

from django.conf.urls.static import static

from App.views import UploadView, UploadSuccessView, IndexView, SelectPredFileView, PredictionsSuccessView

urlpatterns = [

url(r'^admin/', admin.site.urls),

url(r'^App/', include('App.urls'), name="App"),

url('index/', IndexView.as_view(), name='index'),

# Urls to upload the file and confirm the upload

url('fileupload/', UploadView.as_view(), name='upload_file'),

url('upload_success/', UploadSuccessView.as_view(), name='upload_success'),

# Urls to select a file for the predictions

url('fileselect/', SelectPredFileView.as_view(), name='file_select'),

url('predict_success/', PredictionsSuccessView.as_view(), name='pred_success'),

]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

if settings.DEBUG:

import debug_toolbar

urlpatterns = [

path('__debug__/', include(debug_toolbar.urls)),

re_path(r'^media/(?P.*)$', serve, {'document_root': settings.MEDIA_ROOT, }),

] + urlpatterns

select_file_predictions.html

{% extends "index.html" %}

{% block content %}

{% csrf_token %}

{{ form.as_p }}

{% for file in my_objects %}

{% endfor %}

Upload file

{% endblock %}

问题:该文件未显示在html模板中。

2Yyvp.png

我无法解决此问题的问题:

解决方案

我会做这样的事情:

views.py

from os import listdir

from os.path import isfile, join

import settings

from django.views.generic.base import TemplateView

class MyFilesView(TemplateView):

template_name = "select_file_predictions.html"

def get_context_data(self, **kwargs):

context = super().get_context_data(**kwargs)

# List of files in your MEDIA_ROOT

media_path = settings.MEDIA_ROOT

myfiles = [f for f in listdir(media_path) if isfile(join(media_path, f))]

context['myfiles'] = myfiles

return context

select_file_predictions.html

{% extends "index.html" %}

{% block content %}

{% csrf_token %}

{{ form.as_p }}

{% for myfile in myfiles %}

{% endfor %}

Upload file

{% endblock %}