简体   繁体   English

在视图中更改初始表单字段值(Django)

[英]Change initial form field value in views (Django)

I have three form fields that I want to modify/change value of in my views - function one_labeling() before posting the form in the template. 我有三个要在视图中修改/更改其值的表单字段-函数one_labeling(),然后将模板发布到模板中。 The first field is label, where I want to change the initial value in views. 第一个字段是标签,我要在其中更改视图中的初始值。 Secondly I have two choice fields, pos_tag and head_tags where I use set_head_tags and set_post_tags in views from MultiplechoiceFields post_tags and head_tags in forms in order to post the value of each selected field instead of the number, eg "NN" instead of 1. At the moment I get the value "NN" for key 1 from pos_tags in forms but I wonder if I could set and modify an initial variable in views for field pos_tags and head_tags as with the field label. 其次,我有两个选择字段,即pos_tag和head_tags,在这些表单中,我在表单的MultiplechoiceFields post_tags和head_tags的视图中使用set_head_tags和set_post_tags,以便发布每个选定字段的值而不是数字,例如“ NN”而不是1。我从表单中的pos_tags中获得键1的值“ NN”,但是我不知道是否可以像设置字段标签一样在字段pos_tags和head_tags的视图中设置和修改初始变量。

in forms.py

class LabelingForm(forms.ModelForm):

    label = forms.CharField(widget=forms.HiddenInput(), initial="trytrytry22",required=False)

      POS_nodes = (('1','NN'),

          ('2','POSS'),
          ....

        )
pos_tags = forms.MultipleChoiceField(choices=POS_nodes, required=False)



Head_node_choices = (('1','NP'),

          ('2','VP'),
          ...


        )


head_tags = forms.MultipleChoiceField(choices=Head_node_choices,required=False)

 class Meta:
        model = OneLabeling
        fields = ('label', 'sentence' )


    def set_head_tags(self, head_tags):
        data = self.data.copy()
        data['head_tags'] = head_tags
        self.data = data

    def set_pos_tags(self, pos_tags):
        data = self.data.copy()
        data['pos_tags'] = pos_tags
        self.data = data

in views.py 在views.py中

def one_labeling(request, postID):
                form = LabelingForm(request.POST) 
                data1 =  form.cleaned_data['pos_tags']
                data2 =  form.cleaned_data['head_tags']
                if form.is_valid():
                 if data1 and data2:
                    l = data1[0]
                    ll = data2[0]
                    pos_tags = dict(form.fields['pos_tags'].choices)
                    head_tags = dict(form.fields['head_tags'].choices)
                    i = pos_tags.get(l)
                    j = head_tags.get(ll)

                    form.set_head_tags(j)
                    form.set_pos_tags(i)


                    post_one_labeling(request, one_labeling)

For giving initial values for multiple choice fields, you can try doing the following :- 要为多个选择字段提供初始值,您可以尝试执行以下操作:

    pos_tags = forms.MultipleChoiceField(choices=POS_nodes,
                     initial=[POS_node[0] for POS_node in POS_nodes])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM