Django Crispy表单Submit不适用于TabHolder,但适用于字段集

时间:2018-06-20 18:52:57

标签: python django tabs django-crispy-forms

由于某种原因,Submit适用于Fieldsets,但不适用于Django Crispy表单中的Tabholder。 选项卡工作正常,有条件的字段隐藏也很好。只是似乎无法将任何更改保存到数据库。有什么想法吗?

这里是我用来测试功能以提高Django技能的代码。

Models.py

class TestCondition(models.Model):
    some_name = models.ForeignKey(key_name, on_delete=models.CASCADE)
    A_type_choices = (
        ('1','1'),
        ('2','2'),
    )
    Field_A = models.CharField(max_length=20,choices= A_type_choices,default='1')
    B_field_choices = (
        ('abc','ABC'),
        ('cba','CBA'),
    )
    Field_B = models.CharField(max_length=20,choices= B_field_choices,default='abc',blank=True,)
    Field_C = models.CharField(max_length=40, blank=True, default='')
    Field_D = models.CharField(max_length=40, blank=True, default='')
    Field_E = models.CharField(max_length=40, blank=True, default='')

Forms.py

class TestConditionTabForm(ModelForm):
    class Meta:
        model = TestCondition
        fields = ('some_name','Field_A','Field_B','Field_C','Field_D','Field_E',)
    def __init__(self, *args, **kwargs):
        super(TestConditionTabForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper(form=self)
        self.helper.form_class = 'form-horizontal'
        self.helper.label_class = 'col-lg-2'
        self.helper.field_class = 'col-lg-8'
        self.helper.form_method = 'post'
        self.helper.layout = Layout(
            TabHolder(
            Tab(
                'Tab1',
                Field('some_name', type='hidden'),
                'Field_A','Field_B','Field_C',
            ),
            Tab(
                'Tab2',
                'Field_D',
            ),
            Tab(
                'Tab3',
                'Field_E',
            )),
        Fieldset(
            'Test Fieldset submit',
            Field('some_name', type='hidden'),
            'Field_A','Field_B','Field_C','Field_D','Field_E',
                ),

            )   
        self.helper.add_input(Submit('save', 'Submit'))

Views.py

class ViewUpdateTestForm(LoginRequiredMixin,UpdateView):
    model = TestCondition
    template_name = 'test/update.html'
    form_class = TestConditionTabForm

    def form_valid(self, form):
        model = form.save(commit=False)
        model.save()
        return HttpResponseRedirect(reverse('test:testds-overview', args=[model.some_name.id]))

Update.html

{% extends 'base.html' %}
    {% load static %}
    {% block content %}
     <script type="text/javascript" src="{% static 'js/bootstrap-tab.js' %}">  </script>
     <script src="{% static 'formset/jquery.formset.js' %}"></script>
     <script type="text/javascript" src="{% static 'js/jquery.min.js' %}">  </script>
        <script>
    function Hide() {
        if(document.getElementById('id_Field_A').options[document.getElementById('Field_A').selectedIndex].value == "1") {
             document.getElementById('id_Field_B').style.display = 'none';
             document.getElementById('id_Field_C').style.display = '';
        } else {
             document.getElementById('id_Field_B').style.display = '';
             document.getElementById('id_Field_C').style.display = 'none';
        }
    };  </script>
     {% load crispy_forms_tags %} 
     {% crispy form form.helper%}


    <script type="text/javascript">
     window.addEventListener("load", Hide);</script>
     <script type="text/javascript">
     var el = document.getElementById("div_id_weigth_type");
     el.addEventListener("click", Hide);</script>
{% endblock %}

1 个答案:

答案 0 :(得分:0)

显然ButtonHolder起作用了。仍然不知道为什么,但是有很多试验和错误。

pd.Index(
    pd.Series(
        df.columns
    ).replace('Unnamed:\d+', np.nan, regex=True).fillna(method='ffill')
)
#Index(['a', 'a', 'a', 'b', 'b'], dtype='object')

df.columns = pd.Index(pd.Series(df.columns).replace('Unnamed:\d+', np.nan, regex=True).fillna(method='ffill'))
df.head() 
#   a  a  a  b  b
#0  1  1  1  1  1