dm03514 / django-cbv-toolkit

Django CSV download view, Django Multiple form view
3 stars 0 forks source link

django-cbv-toolkit

More Class Based Views for common uses

CSVDownloadView

Provides a simple CBV interface to creating views that allow data to be downloaded as CSV. Often find that I need to create views to allow user to download a CSV. This provides an OOP way of doing that using django's built in CBVs.

Usage:

Example:

from cbvtoolkit.views import CSVDownloadView

MyCSVDownloadView(CSVDownloadView):
    columns = ('name', 'age')
    filename = 'yourfile.csv'

    def get_csv_data(self):
        """
        Generates 10 random dictionaries, using the keys specified in keys.
        @return must return iterable of dictionaries
        """
        for i in range(10):
            yield dict((column, str(i)) for column in self.columns) 

MultiFormView

Allows one view to render and validate multiple different form classes. Sometimes it is required that a single view has different forms on it. I didn't see an easy built in way to do this so MultiFormView was created to assist with the task.

Usage:

Example:

from cbvtoolkit.views import MultiFormView
# DEFINE your forms
from yourapp.forms import EmailForm, UsernameForm

class MyMultiFormView(MultiFormView):
    forms = (EmailForm, UsernameForm) 
    success_url = '/someurl/'

    def emailform_valid(self, form):
        # do something
        return 

    def usernameform_valid(self, form):
        # do something
        return

Forms are availble inside your template through the forms variable. Given the above example, a form can be rendered using {{ forms.emailform.as_p }}.

Each form in the templaate must include a hidden input, which identifies that form.

<input type="hidden" name="form_name" value="emailform" />