我也读过类似的帖子,但什么也没找到。我的其余代码运行得很好。问题出在这里:我在views.py中生成了一个由2x个元组组成的列表list_to_prepopulate_the_form1,并需要将该列表传递给forms.py中的表单
views.py
from django.http import HttpResponse, HttpRequest, HttpResponseRedirect
from .models import *
def n01_size_kss(request):
if request.method == 'POST':
form = KSS_Form(request.POST)
context = {'form':form}
if form.is_valid():
filtertype, context = n01_context_cleaned_data(form.cleaned_data)
if filtertype != "none":
list_to_prepopulate_the_form1 = prepare_dictionary_form2(context)
form1 = KSS_Form1(initial={'list1'=list_to_prepopulate_the_form1})
context = {'form':form1, **context}
return render(request, 'af/size/01_kss_size2.html', context)
else:
context = {'form':KSS_Form()}
return render(request, 'af/size/01_kss_size1.html', context)forms.py
from django import forms
from django.conf.urls.i18n import i18n_patterns
class KSS_Form1(forms.Form):
druckstufe = forms.ChoiceField(\
required=True, \
label=_("Specify desired presure stage:"), \
initial="1", \
disabled=False, \
error_messages={'required':'Please specify desired pressure stage'}, \
choices = list1,
)什么才是正确的方法呢?谢谢
发布于 2020-07-14 00:23:07
将列表作为选项分配给表单域druckstufe,如下所示:
views.py
from django.http import HttpResponse, HttpRequest, HttpResponseRedirect
from .models import *
def n01_size_kss(request):
if request.method == 'POST':
form = KSS_Form(request.POST)
context = {'form':form}
if form.is_valid():
filtertype, context = n01_context_cleaned_data(form.cleaned_data)
if filtertype != "none":
list_to_prepopulate_the_form1 = prepare_dictionary_form2(context)
form1 = KSS_Form1()
# magic here
form1.fields['druckstufe'].choices = list_to_prepopulate_the_form1
context = {'form':form1, **context}
return render(request, 'af/size/01_kss_size2.html', context)
else:
context = {'form':KSS_Form()}
return render(request, 'af/size/01_kss_size1.html', context)https://stackoverflow.com/questions/62879987
复制相似问题