django 表单发布到另一个表单
Posted
技术标签:
【中文标题】django 表单发布到另一个表单【英文标题】:django form posting to another form 【发布时间】:2015-02-08 21:41:18 【问题描述】:我正在尝试创建一个提供另一个表单的表单。最新的迭代如下所示。流程如下:
用户从 form1 的两个下拉列表中选择一个学期。 发布该页面时,这些下拉列表中的值将用作下一个加载表单的查询过滤器。
我什至尝试将这些数据存储在会话中,但我无法让任何工作。我已经搜索了答案,但找不到任何答案。这很可能是因为我是 Django 新手,可能不理解我找到的一些信息。
class CopyClassEnrollmentForm1(forms.Form):
original_semester=forms.ModelChoiceField(queryset=Semester.objects.filter(is_active=True))
new_semester=forms.ModelChoiceField(queryset=Semester.objects.filter(is_active=True))
class CopyClassEnrollmentForm2(forms.Form):
original_class = forms.ModelChoiceField(queryset=Schedule.objects.filter(), empty_label=None)
new_class = forms.ModelChoiceField(queryset=Schedule.objects.filter(), empty_label=None)
def __init__(self, request, *args, **kwargs):
super (CopyClassEnrollmentForm2,self).__init__(*args,**kwargs)
self.fields['original_class'].queryset = Schedule.objects.filter(semester=original_semester)
@staff_member_required
def copy_class_enrollment1(request):
form = CopyClassEnrollmentForm1(request.POST)
if request.method == 'POST' and form.is_valid():
request.session['original_semester'] = form.cleaned_data['original_semester']
request.session['new_semester'] = form.cleaned_data['new_semester']
return HttpResponseRedirect(reverse('copy_class_enrollment2'))
else:
form = CopyClassEnrollmentForm1()
return render(request, "school/admin/copy_class_enrollment1.html", 'form': form,)
def copy_class_enrollment2(request):
form = CopyClassEnrollmentForm2(request.POST)
if request.method == 'POST' and form.is_valid():
original_semester = request.session['original_semester']
new_semester = request.session['new_semester']
original_semester = form.cleaned_data['original_semester']
new_semester = form.cleaned_data['new_semester']
else:
form = CopyClassEnrollmentForm2()
return render(request, "school/admin/copy_class_enrollment2.html", 'form': form,)
【问题讨论】:
好的,到目前为止看起来不错(除了你没有在第二种形式中定义original_semester
)。那么 copy_class_enrollment2 视图在哪里呢?当你使用它时究竟会发生什么?
添加了 copy_class_enrollment2
NameError at /school/CopyClassEnrollment2/ 未定义全局名称“original_semester”
【参考方案1】:
在第二个视图中,您做事的顺序错误。您需要将会话中的值传递到表单中,并且表单必须从 kwargs 中获取值:
class CopyClassEnrollmentForm2(forms.Form):
original_class = forms.ModelChoiceField(queryset=Schedule.objects.filter(), empty_label=None)
new_class = forms.ModelChoiceField(queryset=Schedule.objects.filter(), empty_label=None)
def __init__(self, *args, **kwargs):
original_semester = kwargs.pop('original_semester')
new_semester = kwargs.pop('new_semester')
super (CopyClassEnrollmentForm2,self).__init__(*args,**kwargs)
self.fields['original_class'].queryset = Schedule.objects.filter(semester=original_semester)
def copy_class_enrollment2(request):
original_semester = request.session['original_semester']
new_semester = request.session['new_semester']
if request.method == 'POST':
form = CopyClassEnrollmentForm2(request.POST, original_semester=original_semester, new_semester=new_semester)
if form.is_valid():
# do something
else:
form = CopyClassEnrollmentForm2(original_semester=original_semester, new_semester=new_semester)
return render(request, "school/admin/copy_class_enrollment2.html", 'form': form,)
【讨论】:
我在这一行遇到错误: original_semester = kwargs.pop('original_semester')。 /school/CopyClassEnrollment2/'original_semester'中的 KeyError 可能是因为表格没有被清理。 ***.com/questions/15846743/… 废话。没关系。我忘记在“else:”语句之后将变量添加到 CopyClassEnrollmentForm2。你的答案中有它,但我没有把它放在我的代码中。这行得通。非常感谢!以上是关于django 表单发布到另一个表单的主要内容,如果未能解决你的问题,请参考以下文章