Django Code Snippets

Powered by PythonAnywhere and Django 1.8.6

Cancel Action Mixin

Date Added: Nov. 12, 2015, 4:23 p.m.

Author: django-snippets



class CancelActionMixin(object):
    default_cancel_url = '/'

    def get_context_data(self, arg1=None, **kwargs):
        if arg1 is None:
            context = super(CancelActionMixin, self).get_context_data(**kwargs)
        else:
            # For WizardViews
            context = super(CancelActionMixin, self).get_context_data(arg1, **kwargs)

        # First try the referrer URL
        referrer = self.request.META.get('HTTP_REFERER', None) # Known typo
        if referrer is None:
            context['cancel_url'] = self.default_cancel_url
        else:
            context['cancel_url'] = referrer

        return context

 

Tags: django

Return to List