ModelForm的使用

from django.forms import Form,ModelForm,fields,widgets as wd
class QueModelForm(ModelForm):
    class Meta:
        model=models.Question  #
        fields=['caption','tp']
        error_messages = {
            'caption':{'required':'名称不等为空','invalid':'格式错误'},
            'tp':{'required':'名称不等为空','invalid':'格式错误'}
        }
        widgets = {
            'caption':wd.TextInput(attrs={'class':'form-control',}),
            # 'tp':wd.TextInput(attrs={'class':'form-control'})
        }

class UserTypeModeForm(ModelForm):
    title = fields.CharField(max_length=6,required=True,widget=wd.Textarea())

    class Meta:
        model = models.UserType
        fields = "__all__"

        error_messages = {
            'title':{'required':'名称不能为空','invalid':'格式错误'}
        }
        widgets = {
            'title':wd.TextInput(attrs={'class':'c1'})
        }


def edit_que(request,que_id):
    # if request.is_ajax():
    #     print(111)
    #     models.Question.objects.create(questionnaire_id=que_id,)
    # que_list=models.Question.objects.filter(questionnaire_id=que_id)
    # return render(request,'edit_que.html',{'que_list':que_list,'que_id':que_id})

    def inner():
        que_list=models.Question.objects.filter(questionnaire_id=que_id)
        if not que_list:
            form=QueModelForm()
            yield {'form':form,'obj':None}
        else:
            for que in que_list:
                form=QueModelForm(instance=que)
                Opform=OptionModelForm()
                temp={'form':form,'Opform':Opform,'obj':que,'option_class':'hide','options':None}
                if que.tp == 2:
                    temp['option_class']=''
                    def inner_loop(quee):
                        option_list=models.Option.objects.filter(qs_id=quee)
                        for v in option_list:
                            yield {'form':OptionModelForm(instance=v),'obj':v}
                    temp['options']=inner_loop(que)
                print('temp',form)
                yield temp
    return render(request,'edit_que.html',{'form_list':inner()})
#############################################
#HTML部分

                <ol>


                    
                    {% for item in form_list %}

                        <p></p>
                                <button type="button" class="close X_btn" data-dismiss="modal" aria-label="Close" X_id="{{ item.obj.id }}"><span aria-hidden="true">&times;</span></button>

                    <li>
                    <div>

                        <p>问题名称:{{ item.form.caption }}</p>
                        <p>问题类型:{{ item.form.tp }} <a href="#" class=""data-toggle="modal" data-target="#myModal">添加选项</a></p>
                        <ul>{% for v in item.options %}
                        <li id="li_{{ v.obj.id }}">{{ v.form }}</li>
                        {% endfor %}
                        </ul>
                    </div>
                    </li>
                    {% endfor %}

                </ol>

 

posted @ 2017-12-06 20:41  TAMAYURA  阅读(723)  评论(0编辑  收藏  举报