python fields函数_根据多个echoicefields(Django)中的选择更改视图函数中的表单

我有三个相似的表单Form1包含多个echoicefields和带有按钮的BooleanFields。在views-function中,我希望在从每个字段中获取每个multipleechoicefield-choice并使用干净的数据后,将form从Form1(form=Form1)更改为Form2(form=Form2),并将Form3从Form2(Form3)更改为Form3。也就是说,模板有一个相同的表单,但是在三轮选择每个表单之后,表单/表单内容会发生变化。非常感谢任何帮助。在# in views.py

def function1(request,postID):

# get id for objcet in class Label

label = Label.objects.get(id=postID)

template = template1.html

if request.method == "POST":

form = Form1(request.POST, prefix="form1")

if form.is_valid and 'button' in request.POST:

# Fetch chosen field in Form1 with cleaned_data

a = form.cleaned_data['choices1']

# get the dict with all of the choices in formfield "choices1" in

# Form1 as keys

choices_dict = dict(form.fields['choices1'].choices)

# get the value for choice a in var a in choices_dict

i = choices_dict.get(a)

# Update the modelfield "labeled" in class Labeling in models.py

# with the string/value for a in var i

Labeling.objects.filter(labeled="").update(labeled=i)

# call function post_one_label for posting and saving in db

post_one_label(request, label)

# change form from Form1 to Form2

form = Form2(request.POST, prefix = "Form2")

# validate the changed form after pressing button with name button in

# the template

if form.is_valid() and 'button' in request.POST:

# Fetch chosen field in Form2, choices2 is MultipleChoiceField in Form2 alike choices1 in Form1, with cleaned_data

# call function post_one_label for posting and saving in db

post_one_label(request, label)

# Problem; the form changes from Form1 to Form2 but after submitting the cleaned_data for Form2 isn't processed, it returns None

#

b = form.cleaned_data['choices2']

else:

form = Form1()

# CSRF update

c = {'form':form, 'label':label }

c.update(csrf(request))

# return RequestContext - request for template template1.html

return render(request,template,c,context_instance=RequestContext(request))

def post_one_label(request, label):

label = request.POST.get('label',"")

oneLabel = Labeling(belongsTo=label, label=label)

oneLabel.save()

在表单.py在

^{pr2}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值