好的,所以我需要实现一个下拉菜单,我可以从文件夹目录中选择一些文件。我已经有显示文件的下拉列表,但是当我进行查询时,我收到此错误 django.utils.datastructures.MultiValueDictKeyError:'xxxxxx'
我不确定是否传递整个文件或仅传递文件名。我可以做点什么吗 <select type="file">
?或者我可以将隐藏的输入类型="file"连接到选择吗?谢谢
最佳答案
您的下拉列表应该只是一个文件名列表,然后您可以在清理输入时将其转换为路径
class FileSelectForm(forms.Form):
file = forms.ChoiceField(choices=list_of_files)
def clean_file(self):
file_name = self.cleaned_data['file']
file_path = ... # You'll need to convert the name to a full path here
return open(file_path)
现在,当您的表单提交并且有效时,您将在清理后的数据中为所选文件拥有一个打开的文件对象
form = FileSelectForm(request.POST)
if form.is_valid():
form.cleaned_data['file'] # This will be a file object
关于javascript - 我可以选择类型 ="file"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58963277/