Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accepting input in Django ListView

Tags:

django

In a Django website, I have a paginated ListView that shows a listing of certain objects, 100 per page.

I want to include a text box on this page and accept+process user input. But in Django, this would require me to either:

i) Use a FormView (in which case I lose pagination), or

ii) Write function-based views where I handle pagination and form processing manually.

Is there a third way to achieve what I'm trying to do, ideally without disrupting the ListView?

like image 658
Hassan Baig Avatar asked Jul 02 '26 19:07

Hassan Baig


1 Answers

In my opinion each view should do have one purpose. Doesn't mean you cannot have multiple form elements in a single page. You can do it by

Make a normal view that would only process the data. You would set the url of the view, and then make an ajax call to that view. This can be easily achievable if you are willing to use JavaScript for that as well.(simple xhr methods, blur,change events)

Ex:-

class FormElementProcess(View):
    def post(self,request,*args,**kwargs):
        #Do your processing and return httpresponse

You can further customise the above class and also use formviews for processing and returning of form data, if a form class is also used.

This would be the way out of this I guess. Suppose I have dozens of forms in my Homepage. Forms like -> search,login,register etc. I wouldn't be handling everything through my IndexView. That would over complicate everything when the application would get larger in scale.

2) If page refreshes, then I would suggest you use a FormMixin. There's a good implementation I found for making FormListView

from django.http import Http404
from django.utils.translation import ugettext as _
from django.views.generic.edit import FormMixin
from django.views.generic.list import ListView

class FormListView(FormMixin, ListView):
    def get(self, request, *args, **kwargs):
        # From ProcessFormMixin
        form_class = self.get_form_class()
        self.form = self.get_form(form_class)

        # From BaseListView
        self.object_list = self.get_queryset()
        allow_empty = self.get_allow_empty()
        if not allow_empty and len(self.object_list) == 0:
            raise Http404(_(u"Empty list and '%(class_name)s.allow_empty' is False.")
                          % {'class_name': self.__class__.__name__})

        context = self.get_context_data(object_list=self.object_list, form=self.form)
        return self.render_to_response(context)

    def post(self, request, *args, **kwargs):
        return self.get(request, *args, **kwargs)


class MyListView(FormListView):
    form_class = MySearchForm
    model = MyModel
    # ...

Detailed Code Explanation

You would modify the post method further to do your processing. You could directly use the form class, include parameters from request for processing.

like image 167
Shaurya Chaudhuri Avatar answered Jul 05 '26 09:07

Shaurya Chaudhuri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!