API
Decorators
- @allow_guest_user[source]
Allow anonymous users to access the view by creating a guest user.
Usage example:
from guest_user.decorators import allow_guest_user @allow_guest_user def hello_world(request): return HttpResponse(f"Hello {request.user.username}!")
- @guest_user_required(function=None, anonymous_url=None, registered_url=None)[source]
Current user must be a temporary guest.
Since being a guest user is not a state that a registered user can ever revert back to, there is no “next” URL handling in this decorator.
- Parameters:
anonymous_url – Redirect target for anonymous users. Defaults to
GUEST_USER_REQUIRED_ANON_URL.registered_url – Redirect target for registered users. Defaults to
GUEST_USER_REQUIRED_USER_URL.
Usage example:
from guest_user.decorators import guest_user_required @guest_user_required(anonymous_url="/login/", registered_url="/dashboard/") def only_for_guests(request): pass
- @regular_user_required(function=None, login_url=None, convert_url=None, redirect_field_name='next')[source]
Current user must not be a temporary guest.
The redirected URL will get a “next” parameter added to the URL in order to redirect the user back to the page they were trying to access.
- Parameters:
login_url – Redirect target for anonymous users. Defaults to django:ref/settings:``login_url``.
convert_url – Redirect target for guest users. Defaults to
GUEST_USER_CONVERT_URL.redirect_field_name – URL parameter used to redirect to the origin page. Defaults to
django.contrib.auth.REDIRECT_FIELD_NAME(= “next”).
Usage example:
from guest_user.decorators import regular_user_required @regular_user_required def permanent_users_only(request): # This view will redirect anonymous users to the login page # and guest users to the convert page, respectively. # A redirect URL parameter will be added to users can jump right back here. pass
Mixins
- class AllowGuestUserMixin[source]
Allow anonymous users to access the view by creating a guest user.
This mixin does not require overriding any attributes.
Example usage:
from guest_user.mixins import AllowGuestUserMixin class HelloWorldView(AllowGuestUserMixin, View): def get(self, request): return HttpResponse(f"Hello {request.user.username}!")
- class GuestUserRequiredMixin[source]
Current user must be a temporary guest.
Since being a guest user is not a state that a registered user can ever revert back to, there is no “next” URL handling in this mixin.
Example usage:
from guest_user.mixins import GuestUserRequiredMixin class OnlyGuestView(GuestUserRequiredMixin, View): anonymous_url = "/login/" registered_url = "/dashboard/"
- anonymous_url: str = None
Redirect target for anonymous users. Defaults to
GUEST_USER_REQUIRED_ANON_URL.
- registered_url: str = None
Redirect target for registered users. Defaults to
GUEST_USER_REQUIRED_USER_URL.
- class RegularUserRequiredMixin[source]
Current user must not be a temporary guest.
Anonymous users will be redirected to login_url. Guest users will be redirected to the convert_url.
The redirected URL will get a “next” parameter added to the URL in order to redirect the user back to the page they were trying to access.
Example usage:
from guest_user.mixins import RegularUserRequiredMixin class RealUsersOnlyView(RegularUserRequiredMixin, View): login_url = "/login/" convert_url = "/convert/?from=RealUsersOnlyView"
- login_url: str = None
Redirect target for anonymous users. Defaults to django:ref/settings:``login_url``.
- convert_url: str = None
Redirect target for guest users. Defaults to
GUEST_USER_CONVERT_URL.
- redirect_field_name: str = 'next'
URL parameter used to redirect to the origin page. Defaults to
django.contrib.auth.REDIRECT_FIELD_NAME(= “next”).
Functions
Several helper functions are provided for advanced usage.
- maybe_create_guest_user(request)[source]
Create a guest user and log them in.
This function will create and authenticate a new guest user should the visitor not be authenticated already and their user agent isn’t on the block list.
Signals
- guest_created = <django.dispatch.dispatcher.Signal object>
A visitor accessed a view that created a guest user.
- Parameters:
user – The new guest user.
request – The request that created the guest user.
- converted = <django.dispatch.dispatcher.Signal object>
A guest user converted to a regular registered user.
- Parameters:
user – The now registered user.
Models
- class Guest(*args, **kwargs)[source]
A temporary guest user.
Users linked to a Guest instance are considered temporary guests and will be deleted by cleanup jobs after their expiration.
The age of a guest user is determined by the
created_atfield.This model is swappable with the
GUEST_USER_MODELsetting. Custom Guest models should use the GuestManager or a custom manager that implements the same custom methods.
- class GuestManager(*args, **kwargs)[source]
Manager for Guest objects.
- create_guest_user(request=None, username: str | None = None) User[source]
Create a guest user.
Returns the underlying User object.
- Parameters:
request – The current request object.
username – The preferred username for the user, may be None.
- convert(form: ModelForm) User[source]
Convert a guest user to a regular one.
The form passed in is expected to be a ModelForm instance, bound to the user to be converted.
- Raises:
TypeError if the user is not a temporary guest.
- Parameters:
form – The model form used to create the permanent user.
- Returns:
The converted
Userobject.
Forms
- class UserCreationForm(*args, **kwargs)[source]
A modelform that creates a standard Django user.
Custom implementations must implement
get_credentials().- get_credentials() dict[source]
Get the credentials required to log the user in after conversion.
The credentials are passed to Django’s
authenticate().- Returns:
Login credentials. This is usually a dict with “username” and “password”.
- property media
Return all media required to render the widgets on this form.