In the last April of Heisei (April 27, 2019), Django released a library that may be a little useful, and the other day it exceeded 10,000 DL, so I would like to write an article.
Django-Boost
GitHub https://github.com/ChanTsune/django-boost
At INIAD, I had the opportunity to learn the Python web application framework Django in a lecture, so I decided to make it convenient to use the Django I used at that time.
It's a little easier to do what you want to do when developing with Django. In particular,
--Easy to use users who log in with their email address --Python built-in functions can be used in Django temptate --You can write the URL definition in an easy-to-read manner --Can throw exceptions other than Http404 --You can create a page that requires re-authentication at regular intervals. --Touch the information of the logged-in user in the Form class --Deploying an application on heroku is a little easier
There are many other small functions, but I will omit them this time because they are detailed and sober. For more information, see Documentation (https://django-boost.readthedocs.io/).
$ pip install django_boost
Enter with pip.
Add settings to enable django_boost
in your Django project.
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_boost', #Postscript
]
Add the following contents to the configuration file.
settings.py
AUTH_USER_MODEL = 'django_boost.EmailUser'
Replace the standard Django user model with users who log in with their email address instead of their username. It has the same fields as the standard Django user model.
By adding the following description to your template file, you can use Python's built-in functions as filters and tags in your Django template.
template.html
{% load boost %}
List of Python built-in functions https://docs.python.org/ja/3/library/functions.html
Personally, I often use the format
function when formatting strings.
{% load boost %}
{{ value|format:"," }} {#Display numbers every 3 digits separated by commas#}
zip
is also quite convenient.
Also, during development, I think it's convenient to see the list of variable types and attributes in the template, such as type
and dir
.
Although it is not a built-in function, there are also tags that allow you to create objects from string literals.
In the example below, a list is created.
template.html
{%load boost %}
{% literal "[1,2,3]" as lst %}
{% for i in lst %}
<p>{{ i }}</p>
{% endfor %}
If you create multiple models for one application, ʻurl patterns` will become harder to see. However, it can be used when it is troublesome to divide the file.
urls.py
from django_boost.urls import UrlSet
class YourModelUrlSet(UrlSet):
app_name = "YourModel"
urlpatterns = [
path('xxx/', ..., name="XXX")
path('yyy/', ..., name="YYY")
path('zzz/', ..., name="ZZZ")
]
urlpatterns = [
path('path/to/model/', include(YourModelUrlSet))
]
It will be easier to see if you divide it by CRUD view of each model. I think it's also convenient to be able to separate the namespace for each model.
I think it's a good idea to issue a 404 when you don't meet the viewing permission requirements. Other exceptions can be thrown as well.
To use it, you need to add a little to the configuration file.
settings.py
MIDDLEWARE = [
'django_boost.middleware.HttpStatusCodeExceptionMiddleware', #Postscript
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
I added one middleware.
In this state, if you throw an exception in the process of view, the status code will be returned.
The following example throws a Http302
exception to redirect.
views.py
from django.shortcuts import render
from django_boost.http.response import Http302
def my_view(request):
if request.user.email == "":
raise Http302("/register/email/")
return render(request, "mypage.html")
In addition to 302, it covers most status codes.
If you want to use a custom template, place [status code] .html
directly under the templates directory and it will be used.
If you are creating a View on a class basis, you can create a page that requires authentication at regular intervals by inheriting the mixin class as shown below.
view.py
from datetime import timedelta
from django_boost.views.mixins import ReAuthenticationRequiredMixin
class MyView(ReAuthenticationRequiredMixin,TemplateView):
template_name = '...'
auth_unnecessary = timedelta(hours=1)
Class variable ʻauth_unnecessary` By setting the re-authentication interval, re-authentication will be requested when there is a period longer than the set interval from the last login time.
For ʻauth_unnecessary, you can specify the number of seconds with ʻint
, or you can specify it with timedelta
.
When validating a form, you often want to change the validation conditions for each user.
In such a case, by inheriting the following mixin classes to the Form class and View class respectively, you can touch the user information with self.user
from within the form class.
forms.py
from django import forms
from django_boost.forms.mixins import FormUserKwargsMixin
class MyForm(FormUserKwargsMixin,Form):
email = forms.EmailField()
def email_clean(self):
if self.user.email: #Email address of the logged-in user
...
...
views.py
form django_boost.views.mixins import ViewUserKwargsMixin
from .forms import MyForm
class MyView(ViewUserKwargsMixin,FormView):
form_class = MyForm
...
You can use a command that automatically generates the configuration files needed to deploy a Django application on heroku.
$ python manage.py support_heroku
Generated : /Users/username/project/Procfile
Generated : /Users/username/project/runtime.txt
Generated : /Users/username/project/requirements.txt
Procfile
, runtime.txt
, requirements.txt
are automatically generated.
If a file with the same name exists, it will not be generated, so if you want to overwrite it and generate it, add the --overwrite
option.
$ python manage.py support_heroku --overwrite
As I mentioned at the beginning, there are many other small and sober features, so if you are interested, please see Documentation. Please give me.
Developers will be delighted to receive the stars! https://github.com/ChanTsune/django-boost
Recommended Posts