为什么 fileChoice.value() None?

Why is fileChoice.value() None?

我正在使用 Django 3.2.13 尝试为具有链式外键的文件模型创建一个简单的上传表单。我已经尝试使用基本表单模型和 ModelForm 创建表单,但是,即使在检查 request.FILES 时也没有数据并且 request_file = request.FILES['document'] if 'document' in request.FILES else None 产生 None.

views.py

def home(request):
    if handleAuth(request) is not None:
        return handleAuth(request)
    user = request.user

    if request.method == "POST":
        # if the post request has a file under the input name 'document', then save the file.
        form = FileSubmissionForm(user, request.POST, request.FILES)
        #print(request.FILES)
        #request_file = request.FILES['document'] if 'document' in request.FILES else None
        #if request_file:
        form.save()
        #else:
        #    print("NO FILE")

    userFiles = []
    if user.is_company_admin:
        temp = StorageFile.objects.filter(company_key=user.company_key)
    else:
        temp = StorageFile.objects.filter(company_key=user.company_key,
                                          profile__in=user.user_profiles.all())
    temp.order_by("profile")
    for file in temp:
        userFiles.append({"name": file.getFileName(), "path": file.getMediaPath()})

    form = FileSubmissionForm(user)

    return render(request=request,
                  template_name="fileCloudApp/home.html",
                  context={"userHasAdmin": user.is_company_admin, "files": userFiles, "form": form})

forms.py

from django import forms
from fileCloudApp.models import StorageFile
from loginApp.models import UserProfile


class FileSubmissionForm(forms.Form):
    fileChoice = forms.FileField()
    profileChoice = forms.ModelChoiceField(queryset=UserProfile.objects.none(), label="Choose profile")

    def __init__(self, user, *args, **kwargs):
        super(FileSubmissionForm, self).__init__(*args, **kwargs)

        if user.is_company_admin:
            temp = UserProfile.objects.filter(company_key=user.company_key)
        else:
            temp = user.user_profiles.all()
        temp.order_by("label")
        self.fields["profileChoice"].queryset = temp
        self.user = user

    def save(self):
        print(self["fileChoice"].value()) # None
        pro = UserProfile.objects.filter(id=int(self["profileChoice"].value()))
        if len(pro) > 0:
            pro = pro[0]
            print(pro)

models.py

class StorageFile(models.Model):
    date_updated = models.DateTimeField('Date Updated', default=timezone.now)
    company_key = models.ForeignKey(CompanyModel, verbose_name="Company", on_delete=models.CASCADE)
    file_upload = models.FileField(upload_to=company_directory_path)
    profile = ChainedForeignKey(
        UserProfile,
        chained_field="company_key",
        chained_model_field="company_key",
        show_all=False,
        auto_choose=True,
        sort=True,
        on_delete=models.PROTECT)

    def getLocalPath(self):
        return self.file_upload.name.split(str(self.company_key.id) + "/")[1]

    def getMediaPath(self):
        return "get/{0}/0/{1}".format(self.id, self.getFileName())

    def getFileName(self):
        return self.file_upload.name.split("/")[-1]

    def getFilePath(self):
        return settings.MEDIA_ROOT + "{0}/{1}".format(str(self.company_key.id), self.getFileName())

    def __str__(self):
        return str(self.company_key) + ": " + str(self.file_upload)

表格必须包含enctype="multipart/form-data"

<form action="" method="post" enctype="multipart/form-data">