How do I filter tables with Django generic views?

后端 未结 2 1589
攒了一身酷
攒了一身酷 2020-12-14 05:03

I am trying to create a table view with pagination, sorting, and filtering, using the most common/standard/recommended approach for Django 1.6. This seems to be Generic Cla

2条回答
  •  -上瘾入骨i
    2020-12-14 05:31

    Your answer help me, so i edited your code to include the submit button from crispy-forms

    forms.py. I couldn't figure out how/where to get the form submit button out of django_filters in combination with everything else, so this code suppresses the form wrapper tags from crispy, and then we provide that HTML in the template, which is probably the kludgiest part of this.

    forms.py (UPDATED)

    from django import forms
    from .models import Author
    from crispy_forms.helper import FormHelper
    from crispy_forms.layout import Layout, ButtonHolder, Submit
    
    class AuthorListFormHelper(FormHelper):
        model = Author
        form_tag = False
        # Adding a Filter Button
        layout = Layout('name', ButtonHolder(
            Submit('submit', 'Filter', css_class='button white right')
        ))
    

    author-list.html (UPDATED)

    {% extends "base.html" %}
    {% load render_table from django_tables2 %}
    {% load crispy_forms_tags %}
    
    {% block content %}
    
    {% render_table table %}
    
    
    {% crispy filter.form filter.form.helper %}
    {% endblock content %}

提交回复
热议问题