View Mixins
- synopsis:
View mixin class in django-boost
Access Mixins
CSRFExemptMixin
CSRFExemptMixin remains available. For projects that do not need the
django-boost mixin API, Django’s native decorator form is the recommended
idiom:
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView
@method_decorator(csrf_exempt, name='dispatch')
class MyView(TemplateView):
template_name = 'path/to/template'
AllowContentTypeMixin
Restrict the content type of http request.
from django.views.generic import TemplateView
from django_boost.views.mixins import AllowContentTypeMixin
class PostView(AllowContentTypeMixin, TemplateView):
allowed_content_types = ["application/xml"]
template_name = "path/to/template"
Restrict request based on Content-Type of http header.
If the content type is not allowed, http415 response will be returned.
You can disable restrictions by specifying strictly = False
LimitedTermMixin
from datetime import datetime
from django.views.generic import TemplateView
from django_boost.views.mixins import LimitedTermMixin
class MyView(LimitedTermMixin, TemplateView):
template_name = 'path/to/template'
start_datetime = datetime(year=2019, month=1, day=1)
end_datetime = datetime(year=2019, month=12, day=31)
Restrict the period of access.
start_datetime specifies the date and time when access will be available, and end_datetime with the last date and time when access is available.
You can change the date and time that can be accessed dynamically by overriding the get_start_datetime and get_end_datetime methods, respectively.
You can specify the exception class to be thrown when the condition accessible to exception_class is not met.
The default is the Http404 exception.
ReAuthenticationRequiredMixin
- class django_boost.views.mixins.ReAuthenticationRequiredMixin
Require authentication again on access.
from django.views.generic import TemplateView from django_boost.views.mixins import ReAuthenticationRequiredMixin class RecentLoginView(ReAuthenticationRequiredMixin, TemplateView): template_name = "my_page.html" interval = 3600
from datetime import timedelta from django.views.generic import TemplateView from django_boost.views.mixins import ReAuthenticationRequiredMixin class RecentLoginView(ReAuthenticationRequiredMixin,TemplateView): template_name = "my_page.html" interval = timedelta(hours=1)
intervalis the grace period until recertification.Can specify
intordatetime.timedelta.logout=True, Logout if the specified time limit has passed.logout=False, Do not logout Even if the specified time limit has passed.
StaffMemberRequiredMixin
- class django_boost.views.mixins.StaffMemberRequiredMixin
Request staff authority.
Set the class variable
superusertoTrueto allow superuser.
SuperuserRequiredMixin
- class django_boost.views.mixins.SuperuserRequiredMixin
Request super user authority.
AnonymousRequiredMixin
- class django_boost.views.mixins.AnonymousRequiredMixin
Require the user to be anonymous.
The inverse of Django’s
LoginRequiredMixin: redirect an already authenticated user away, e.g. from a login or sign-up page.from django.views.generic import TemplateView from django_boost.views.mixins import AnonymousRequiredMixin class SignUpView(AnonymousRequiredMixin, TemplateView): template_name = "sign_up.html" redirect_authenticated_url = "/dashboard/"
redirect_authenticated_urlis where an authenticated user is sent; when it isNoneit falls back tosettings.LOGIN_REDIRECT_URL.
Redirect Control Mixins
DynamicRedirectMixin
You can control the redirect destination with next=~~ in the URL query string like LoginView.
from django.views.generic import FormView
from django_boost.views.mixins import DynamicRedirectMixin
class MyFormView(DynamicRedirectMixin, FormView):
redirect_field_name = 'next' # default keyword is 'next'
...
You can change the query string parameter name by changing redirect_field_name.
Additional Attribute Mixins
UserAgentMixin
from django.views.generic import TemplateView
from django_boost.views.mixins import UserAgentMixin
class SameView(UserAgentMixin, TemplateView):
template_name = "default_template"
pc_template_name = "pc_template.html"
tablet_template_name = "tablet_template.html"
mobile_template_name = "mobile_template.html"
Assign user_agent attribute to self.request and switch the template file to be displayed by user agent.
If the user agent can not be determined, the template specified in template_name will be used.
pc_template_name, tablet_template_name and mobile_template_name are optional, but template_name is required.
JsonRequestMixin
A specialized mixin for AllowContentTypeMixin for json.
from django.views.generic import TemplateView
from django_boost.views.mixins import JsonRequestMixin
class PostView(JsonRequestMixin, TemplateView):
template_name = "path/to/template"
def get_context_data(self,**kwargs):
posted_data = self.json
# {"send" : "from client"}
return posted_data
You can access the dictionary object parsed from the Json string sent by the client in self.json
By default this mixin does not restrict the request’s Content-Type. Specify strictly = True if you want to limit the Content-Type to Json only.
If you use for the purpose of API, JsonView is recommended.
ResponseMixins
JsonResponseMixin
Returns the response in Json format
from django.views.generic import TemplateView
from django_boost.views.mixins import JsonResponseMixin
class JsonResponseView(JsonResponseMixin, TemplateView):
extra_context = {"context" : "..."}
def get_context_data(self,**kwargs):
context = {}
context.update(super().get_context_data(**kwargs))
return context
The usage of extra_context and get_context_data is basically the same as TemplateView.
The difference is that TemplateView is passed directly to the template context, whereas JsonResponseMixin is a direct response.