Skip to content

RedirectActionMixin

RedirectActionMixin

djadmin.actions.base.RedirectActionMixin

Mixin for actions that redirect to a URL.

When combined with RedirectViewActionMixin, creates a Django RedirectView that redirects immediately without displaying content. The redirect URL is determined via the RedirectViewMixin using method dispatch.

The ViewFactory automatically: 1. Sets RedirectView as the base class (via RedirectViewActionMixin) 2. Adds RedirectViewMixin which implements get_redirect_url() dispatch 3. Creates a view that redirects based on action configuration

Redirect URL can be specified via: - redirect_url = 'some/url' (simple static URL) - def get_redirect_url(self, args, *kwargs) (dynamic method)

Examples

Static redirect URL
class StaticRedirectAction(GeneralActionMixin, RedirectViewActionMixin, BaseAction):
    label = 'Go to Dashboard'
    redirect_url = '/dashboard/'

# Dynamic redirect URL using method
class ExternalLinkAction(RecordActionMixin, RedirectViewActionMixin, BaseAction):
    label = 'View External'

    def get_redirect_url(self, *args, **kwargs):
        # self is the VIEW instance, not the action
        pk = self.kwargs.get('pk')
        return f'https://example.com/items/{pk}'

Note: When implementing get_redirect_url(), remember that it will be called with the view as self (not the action). Access view attributes like self.request, self.kwargs, self.action, etc.

Method Resolution Order

  1. djadmin.actions.base.RedirectActionMixin

Attributes

Attribute Value Defined in
__annotations__ {'redirect_url': str | None} djadmin.actions.base.RedirectActionMixin
redirect_url None djadmin.actions.base.RedirectActionMixin

Methods

get_redirect_url(self, *args, **kwargs) -> str

Defined in: <class 'djadmin.actions.base.RedirectActionMixin'>

Get URL to redirect to (will be called via RedirectViewMixin dispatch).

This method is called by RedirectViewMixin with the view instance as self.

The view instance (self) has: - self.action: The action instance - self.request: The current request - self.kwargs: URL kwargs (e.g., {'pk': 1}) - self.args: URL args - self.model: The model class - self.model_admin: The ModelAdmin instance - self.admin_site: The AdminSite instance

Args: args: URL args from Django's RedirectView *kwargs: URL kwargs from Django's RedirectView

Returns: URL string (absolute or relative)

Raises: NotImplementedError: If not overridden and no redirect_url attribute set

Source code in base.py line 583
    def get_redirect_url(self, *args, **kwargs) -> str:
        """
        Get URL to redirect to (will be called via RedirectViewMixin dispatch).

        This method is called by RedirectViewMixin with the view instance as self.

        The view instance (self) has:
        - self.action: The action instance
        - self.request: The current request
        - self.kwargs: URL kwargs (e.g., {'pk': 1})
        - self.args: URL args
        - self.model: The model class
        - self.model_admin: The ModelAdmin instance
        - self.admin_site: The AdminSite instance

        Args:
            *args: URL args from Django's RedirectView
            **kwargs: URL kwargs from Django's RedirectView

        Returns:
            URL string (absolute or relative)

        Raises:
            NotImplementedError: If not overridden and no redirect_url attribute set
        """
        raise NotImplementedError(
            f'{self.__class__.__name__} must implement get_redirect_url() ' f'or set redirect_url attribute'
        )

Fields

Field Type Related To
__dict__ getset_descriptor -
__weakref__ getset_descriptor -