Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 如何列出自定义目录的所有文件_Django_Python 3.x_Django Models_Django Forms - Fatal编程技术网

Django 如何列出自定义目录的所有文件

Django 如何列出自定义目录的所有文件,django,python-3.x,django-models,django-forms,Django,Python 3.x,Django Models,Django Forms,我想让用户从列表中选择一个courseName并显示此目录下的所有文件,我如何实现这一点?我正在努力寻找一种方法来显示特定课程的所有文件。 以下是我迄今为止所做的工作: 视图.py def showDocuments(request): if request.POST: if Document.objects.filter(courses__exact=request.POST["course"]).exists(): print(request.POST["co

我想让用户从列表中选择一个courseName并显示此目录下的所有文件,我如何实现这一点?我正在努力寻找一种方法来显示特定课程的所有文件。 以下是我迄今为止所做的工作:

视图.py

def showDocuments(request):
    if request.POST:
    if Document.objects.filter(courses__exact=request.POST["course"]).exists():
        print(request.POST["course"])
    else:
        print("there is no files for this course yet!")

    documents = Document.objects.all()
    courses = Course.objects.all()
    context = {
        "documents" : documents ,
        "courses" : courses,
    }
    return render(request , 'dashboard.html' , context )
型号.py

class Document(models.Model):
    #to do : enum class ! 
    DB = "data structure"
    SF = "software enginering"
    DS = "discrete structure " 
    WD = "web dev"
    OPTIONS = "options"
    courseChoices = (
        (DB , "data structure"),
        (SF , "software enginering "),
        (DS , "discrete structure"),
        (WD , "web dev"),
        (OPTIONS , "options"),
    )
    courses     = models.CharField(max_length=50, choices=courseChoices, default=OPTIONS)
    description = models.TextField(help_text="A little description can be very helpful for others!")
    document    = models.FileField(upload_to=content_file_name)
    uploaded_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "{}".format(self.document)

    def content_file_name(self, courses):
        file_path = "documents/{courses}/{filename}".format(
            courses=self.courses, filename=courses)
        return file_path
dashboard.html

<div class="col-sm-4">
       <select id="crs" class="custom-select custom-select-sm" name="course">
          <option selected>option</option>
          {% for obj in courses %}
                    <option value="{{ obj.courseName }}">{{ obj.courseName }}</option>
            {% endfor %}
        </select>
    </div>

选项
{课程%中的obj为%1}
{{obj.courseName}}
{%endfor%}

因此,以下是我的解决方案:

def showDocuments(request, *args, **kwargs):
    """ shows all the files of the requested course  """
    crs_names = CoursesNames.objects.all()
    context = {
        "crs_names": crs_names,
        "files": [],
        "not_found" : "",
    }
    if request.POST:

        file_name = request.POST["course"]
        not_found = "There are no files for {0} yet!".format(file_name)
        documents = Document.objects.all().filter(course_name__courses__iexact=file_name)
        context["documents"] = documents
        context["filename"] = file_name

        documentObjectList = Document.objects.all().values_list("document").filter(course_name__courses__iexact=file_name)
        queryset = CoursesNames.objects.filter(courses__exact=file_name).exists()

        if queryset:
            while len(context["files"]) > 0: context["files"].pop()
            for i in documentObjectList:
                context["files"] = i
                print(context["files"])
            if len(context["documents"]) == 0: context["not_found"] = not_found
    return render(request, 'dashboard.html', context)

你的
showDocuments
是否有额外的参数(课程名称)?@WillemVanOnsem我应该以某种方式从使用请求的用户那里获得它。post[]据我所知,是吗?是的,这是一种可能性(虽然通常在搜索中使用
get
,因为这样可以共享搜索URL)。
元素的名称是什么(映射到元素上以进行搜索的键)?@WillemVanOnsem我刚刚更新了我的问题,请看一下。