Django - 组合数据表视图和表单视图

时间:2015-04-28 12:49:15

标签: django django-crispy-forms

我正在尝试将一个数据表和一个松脆的表格放入一个视图中,以便我可以在django的单个页面上显示它。我完全坚持如何将它们结合起来,因为我还是一个新手。

非常感谢任何帮助!!

Views.py ------------- Datatable below
    class CustomerTable(XEditableDatatableView):
        template_name = "customers.html"
        model = Customer
        datatable_options = {
            'columns': [
                        ("Title", 'Title'),
                        ("Name", 'PoC', helpers.make_xeditable),
                        ("Email", 'PoCEmail', helpers.make_xeditable),
                        ("Location", 'Location', helpers.make_xeditable),
                        ("DateAdded", 'DateAdded', helpers.make_xeditable),
                        ],
            'hidden_columns': ['ID'],
        }

           -----------Crispy Form below
        def CustomerView(request):
            form = CustomersForm(request.POST or None)
            success = False
            if request.method == 'POST':
                if form.is_valid():
                    form.save()
                    form = CustomersForm()
                    success = True
            if request.POST.get('delete'):
                obj.delete()
            customer_form = CustomersForm()
            return render(request, 'customers.html', {'customer_form': customer_form})


urls.py ----------------
    urlpatterns = patterns('',
        url(r'^$', views.CustomerTable.as_view(), name='customertable'),
        url(r'^$', 'ISL.views.CustomerView', name='customersform'),
    )

感谢您的帮助! w ^

1 个答案:

答案 0 :(得分:0)

您可以尝试使用类的get_context_data()方法:

    def get_context_data(self, **kwargs):
        context = super(CustomerTable, self).get_context_data(**kwargs)
        # do your magic here <<<
        # then
        context['customer_form'] = customer_form
        return context

首先,您通过超级内置函数调用祖先的get_context_data方法,然后您可以将所需内容附加到上下文中,在您的示例中,它是自定义形式。在此之后,您可以访问&#39; custom_form&#39;在你的模板中。

不幸的是,我暂时无法测试它,但是这个方法必须由ContextMixin附加在祖先中(关于这个方法的django doc:https://docs.djangoproject.com/en/1.8/ref/class-based-views/mixins-simple/)。