Django表单字段不会被POST数据填充

时间:2021-12-22 15:59:23

I have a model form with custom constructor. It checks if a file is required and if false, it deletes the file field. It also has two hidden fields, which I initialize at my views. The form class is as follows:

我有一个带有自定义构造函数的模型表单。它检查是否需要一个文件,如果是false,则删除文件字段。它还有两个隐藏的字段,这是我在视图中初始化的。表格类别如下:

class SubmitTask(forms.ModelForm):
    task = forms.ModelChoiceField(queryset=Task.objects.all(), widget=forms.HiddenInput())
    student = forms.ModelChoiceField(queryset=UserProfile.objects.all(), widget=forms.HiddenInput())    

    def __init__(self, file_required=False, *args, **kwargs):
            super(SubmitTask, self).__init__(*args, **kwargs)
            if file_required is True:
                    file = forms.FileField(
                            label='Select a file',
                            help_text='max: 2.5 MB'
                    )
            else:
                    del self.fields['file']

    class Meta:
            model = Submission
            exclude = ('date_submitted',)

My problem is that the hidden fields are populated when I initialize the form (I have confirmed that its values are indeed initialized by viewing the HTML code). However, the values of the hidden fields are not populated during POST request. I have confirmed during POST request that the form is indeed bound and I also confirmed on Firebug that POST also contains the task and student values. This is the involved method in my views.py

我的问题是,当我初始化表单时,隐藏的字段会被填充(我已经确认它的值确实是通过查看HTML代码来初始化的)。但是,隐藏字段的值不会在POST请求期间填充。我已经在POST请求中确认了表单确实是绑定的,我也在Firebug中确认了POST也包含任务和学生值。这是我的views.py中涉及的方法

def view_task(request, id):
    task = Task.objects.get(pk=id)
    profile = request.user.get_profile()

    data = {}
    data['classroom'] = task.get_classroom()
    data['description'] = task.get_description()

    teacher_classrooms = Classroom.objects.filter(teacher=profile)
    teacher_tasks = Task.objects.filter(classroom__in=teacher_classrooms)

    if not submission and task not in teacher_tasks:
        form = SubmitTask(file_required=task.file_required, initial={'task':task.pk, 'student':profile.pk})

        data['form'] = form
        if request.method == 'POST':
            form = SubmitTask(request.POST, request.FILES)

            if form.is_valid():
                form.save()

    return render_to_response('classrooms/view_task.html',
        data, context_instance=RequestContext(request))

1 个解决方案

#1


1  

Your view function looks incorrect to me:

你的视图函数在我看来不正确:

Where does the submission variable come from, when initializing the posted form you are missing the file_required parameter and the form processing should perhaps be reorganized to something like:

提交变量来自哪里?在初始化已发布的表单时,您丢失了file_required参数,表单处理应该重新组织为以下内容:

if request.method == 'POST':
    form = SubmitTask(task.file_required, request.POST, request.FILES)
    ...
else:
    form = SubmitTask(task.file_required, ...)

data['form'] = form

#1


1  

Your view function looks incorrect to me:

你的视图函数在我看来不正确:

Where does the submission variable come from, when initializing the posted form you are missing the file_required parameter and the form processing should perhaps be reorganized to something like:

提交变量来自哪里?在初始化已发布的表单时,您丢失了file_required参数,表单处理应该重新组织为以下内容:

if request.method == 'POST':
    form = SubmitTask(task.file_required, request.POST, request.FILES)
    ...
else:
    form = SubmitTask(task.file_required, ...)

data['form'] = form