---
title: Django
date: 2024-05-15 10:12:25
background: bg-[#214a35]
label: Python
tags:
- python
- web
categories:
- Programming
intro: |
A Django cheat sheet.
plugins:
- copyCode
---
## Getting Started
### Start a new Django Project
{.row-span-2}
```python
# Create et access project folder
~$ mkdir project_name
~$ cd project_name
# Create Python virtual env
~$ python3 -m venv venv
# Activate virtual env
~$ source venv/bin/activate
# If you want to deactivate virtual env
~$ deactivate
# Install django (~= same as 4.2.*)
~$ pip install django~=4.2.0
# New django project (from project_name folder)
~$ django-admin startproject config
# Create app (from project_name folder)
~$ python manage.py startapp app_name
```
### Migration:
Django create a database table for each models present in your app using those commands:
Makemigrations: Create a file under app_name/migrations with the database structure to create
```python
~$ python manage.py makemigrations
```
Migrate: Will read the migrations files and create the actual database and tables
```python
~$ python manage.py migrate
```
### Create superuser for authenficiation/admin panel
```python
~$ python manage.py createsuperuser
```
### Start server
```python
~$ python manage.py runserver => ex. http://127.0.0.1:8000
```
### Requirements
```python
# Create a requirements file that contain all your project dependencies
~$ pip freeze > requirements.txt
# Install your project requirements (if a requirements file exist)
~$ pip install -r requirements.txt
```
### Other commands
```python
# Django shell (Run project code directly)
~$ python manage.py shell
# example of code to run in the shell:
>>> from app_name.models import User
>>> user1 = User.objects.first()
# Prepare static folders for production
~$ python manage.py collectstatic
# Take all data from app blog and export in json
~$ python manage.py dumpdata blog > myapp.json
# Take all data in json file and import in app data table
~$ python manage.py loaddata myapp.json
# To Create or Update a message file for Translation, run this command
~$ django-admin makemessages -l de
# Updating message files
~$ django-admin compilemessages
```
## Project config {.cols-1}
### Configuration settings
##### Project base templates directory and Apps templates directory:
- create folder project/templates
- create folder appfolder/templates/appname
##### Create Static folder in base root and separate for apps:
- project_name\static\root
- app_name\static\app_name
```python
# Add app to settings.py
INSTALLED_APPS = [
… ,
'app_name.apps.App_nameConfig',
]
# template directory config
Project templates settings.py:
TEMPLATES = [
{ …
'DIRS': [BASE_DIR / 'templates'],
… }
# Static folder:
STATIC_URL = '/static/'
STATICFILES_DIRS = [ BASE_DIR / 'static' ]
STATIC_ROOT = 'static_root'
# Media folder:
MEDIA_URL = '/media/'
STATICFILES_DIRS = (
(BASE_DIR / 'static'),
)
MEDIA_ROOT = BASE_DIR / 'media'
# To use PostgresSQL
# pip install psycopg2
# settings.py
DATABASE = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'blog',
'USER': 'admin',
'PASSWORD': '123456',
'HOST': 'localhost',
'PORT': '5432'
}
}
TIME_INPUT_FORMATS = [
'%H:%M:%S', # '14:30:59'
'%H:%M', # '14:30'
]
DATE_FORMAT = "Y/M/d"
# locale for Translations
LANGUAGES = [
('en', 'English'),
('de', 'German'),
]
LOCALE_PATHS = (
BASE_DIR / 'locale',
)
# CK Editor Configuration
CKEDITOR_UPLOAD_PATH = 'uploads/'
CKEDITOR_RESTRICT_BY_USER = True
CKEDITOR_BROWSE_SHOW_DIRS = True
CKEDITOR_ALLOW_NONIMGE_FILES = True
CKEDITOR_IMAGE_BACKEND = "pillow"
CKEDITOR_CONFIGS = {
'default': {
'toolbar': 'full',
}}
# SSL
# If you have an SSL certificate
SECURE_SSL_REDIRECT = True
# cookies will only be sent via HTTPS connections
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
```
## Data Models
### Create Data Model {.col-span-2}
Theses models can be created as database tables with the migrations commands
```python
# models.py
# The id fields is automaticly created by Django
# for each model that why it's not show below
from django.db import models
class Customer(models.Model)
name = models.Charfield('Customer', max_length=120)
age = models.IntegerField()
note = models.TextField(blank=True, null = True)
email = models.EmailField(max_length=255, blank=True, null=True)
credit = models.FloatField(blank=True)
is_active = models.BooleanField(default=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
img = models.ImageField(upload_to ='uploads/')
# Select Field (return value, display value)
TYPE_CHOICES = (
('Customer', _('Customer')),
('Supplier', _('Supplier')),
('Student', _('Student')),
)
type = models.CharField(choices=TYPE_CHOICES)
class Meta:
verbose_name = "Customer"
verbose_name_plural = "Customers"
# Model string representation
def __str__(self):
return self.name
# the URL that points to a resource or page on your website
def get_absolute_url(self):
return reverse("customer_detail", kwargs={"pk": self.pk})
```
#### We can also use this method to define the ChoiceField value.
```python
class Customer(models.Model)
class TypeList(models.IntegerChoices):
customer = (1, _('Customer'))
supplier = (2, _('Supplier'))
student = (3, _('Student'))
.
.
.
type = models.CharField(choices=TypeList.choices, default=1)
```
#### To access the ChoiceField value in the template, we need to do the following in the template:
- In Django templates you can use the "`get_FOO_display()`" method, that will return the readable alias for the field,
where `'FOO'` is the name of the field.
- If the choices are stored in the variable `CHOICES` and the model field storing the selected choice is `'type'` then
you can directly use
```html
{{ X.get_type_display }}
{% trans X.get_type_display %}
```
#### Relationship between models
```python
# One-to-Many: (use double quotes if the entity is not yet declare) ex. "Supplier"
supplier = models.ForeignKey(Supplier, blank=True, null=True, on_delete=models.CASCADE,related_name="supplierother")
# on_delete can be set to models.CASCADE, models.ST_DEFAULT or models.SET_NULL
# The related_name attribute specifies the name of the reverse relation from the supplier model back to your model
# Many-to-Many:
tags = models.ManyToManyField(Tag, blank=True)
# One to One
User = models.OneToOneField(User, on_delete=models.CASCADE)
# Overwrite save method
def save(self, (*args, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
super().save(*args, **kwargs)
```
## Lookup
### Field Lookups {.col-span-2}
In Django, the `field__lookuptype` syntax is used to perform lookups on fields in queries. The `field` represents the
name of the field you want to perform the lookup on, and `lookuptype` represents the type of lookup you want to perform.
For example, let's say you have a model called `Book` with a field called `title` . You can use the `field__lookuptype`
syntax to perform different types of lookups on the `title` field.
Here are a few examples:
```python
# 1. Exact Match Lookup:
Book.objects.filter(title__exact='Python Tricks')
# 2. Case-insensitive Lookup:
Book.objects.filter(title__iexact='python tricks')
# 3. Contains Lookup:
Book.objects.filter(title__contains='Python')
# 4. Startswith Lookup:
Book.objects.filter(title__startswith='Python')
# 5. Endswith Lookup:
Book.objects.filter(title__endswith='Tricks')
```
### Field Lookups Reference
| Keyword | Description |
| -------------- | :----------------------------------------------- |
| `contains` | Contains the phrase |
| `icontains` | Same as contains, but case-insensitive |
| `date` | Matches a date |
| `day` | Matches a date (day of month, 1-31) (for dates) |
| `endswith` | Ends with |
| `iendswith` | Same as endswidth, but case-insensitive |
| `exact` | An exact match |
| `iexact` | Same as exact, but case-insensitive |
| `in` | Matches one of the values |
| `isnull` | Matches NULL values |
| `gt` | Greater than |
| `gte` | Greater than, or equal to |
| `hour` | Matches an hour (for datetimes) |
| `lt` | Less than |
| `lte` | Less than, or equal to |
| `minute` | Matches a minute (for datetimes) |
| `month` | Matches a month (for dates) |
| `quarter` | Matches a quarter of the year (1-4) (for dates) |
| `range` | Match between |
| `regex` | Matches a regular expression |
| `iregex` | Same as regex, but case-insensitive |
| `second` | Matches a second (for datetimes) |
| `startswith` | Starts with |
| `istartswith` | Same as startswith, but case-insensitive |
| `time` | Matches a time (for datetimes) |
| `week` | Matches a week number (1-53) (for dates) |
| `week_day` | Matches a day of week (1-7) 1 is Sunday |
| `iso_week_day` | Matches a ISO 8601 day of week (1-7) 1 is Monday |
| `year` | Matches a year (for dates) |
| `iso_year` | Matches an ISO 8601 year (for dates) |
### Admin Panel {.col-span-2}
Every Django projects come with an Admin Panel that can be open at /admin url (ex: localhost:8000/admin) Hello Admin Hello John Doe Hello User row: {{ forloop.counter }} # starting index 1 {{ forloop.counter0 }} # starting index 0 The product name is {{ product.name }} The product name is {{ product.price }}
{% endfor %}
{{ var_name }}
{{ title | lower }} {{ blog.post | truncatwords:50 }} {{ order.date | date:"D M Y" }} {{ list_items | slice:":3" }} {{
total | default:"nil" }}
{{ request.path }}
{% url 'posts.delete' id=post.id %}
{% load static %} {% static 'css/main.css' %}
{% with name="World" %}
To display
the model in the Admin panel register the model in the app_name/admin.py file
For each models you can specify the
fields you want to use
```python
from django.contrib import admin
from app_name.models import Blog
# Custom model Admin (admin.py):
@admin.register(Blog) # Register app
class BlogAdmin(admin.ModelAdmin)
fields = ("title", "description") # Fields to use for add/edit/show page
list_display = ("title", "description") # fields to display in search page
list_display_links = ("title",) # fields that will be a link in search page
ordering = ("date_created",) # Ordering allowed in the search page
search_fields = ("title", "description") # Search fields allowed in the search page
list_filter =("title",) # define list filters that appear in the right sidebar
```
### Routing
Django routing info is store in project_folder/urls.py file
```python
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls), # pre-created admin urls routes
path('', include('app_name.urls')) # include your app urls
]
```
The 'include()' method allow to link another urls.py file created in your app folder (app_name/urls.py)
```python
from django.urls import path
from app_name import views
urlpatterns = [
path('posts', views.index, name='posts.index'),
path('posts/create/', views.create, name='posts.create',
path('posts/app_folder/templates/app_name/\*.html
```html
{% extends 'base.html' %}
{% block contents %} {% endblock contents %}
{% include 'partials/header.html' %}
{% include 'body.html' with key1=value1 key2=value2 %}
{% if user.username = 'Mike' %}
Model Class Name: {{ objectmodel }}
``` ## Context Processor {.cols-1} ### Creating custom Context Processor Anywhere, create a `context_processors.py` file ``` project_name └───app_name ├───... └───context_processors.pyy ``` Create a function in context_processors.py that accepts a HttpRequest object as an argument and returns a dictionary A context processor is just a function that accepts an HttpRequest object as an argument and returns a dictionary. Like this: ```python # app_name/context_processors.py def site_email(request): return { 'site_email' : 'example@gmail.com' } ``` 3. Add this to your context_processors setting in settings.py (at the bottom for security reasons) ```python TEMPLATES = [ { … 'OPTIONS': { 'context_processors': [ … 'app_name.context_processors.site_email', # New context processor here … ], … }, # Now be able to access the 'site_email' template variable on every single django template across your whole site. ``` ## Model Managers and Querysets ### Model Manager Model manager allow model database reads and writes ```python # One line create and save Article.objects.create(name='Item 1', price=19.95) # Read all Article.objects.all() # Create user = User.objects.first() article = Article(user=user, name='Item 1', price=19.95) # Save article.save() # Read one Article.objects.get(id=1) # Select Related (to avoid n+1 query) posts = Post.objects.select_related('user', 'category').all() # Read or render a 404 not found page from django.shortcuts import get_object_or_404 article = get_object_or_404(Article, id=512) # Filter Article.objects.filter(model='dyson', name__icontains='dyson') # __icontains Article.objects.filter(year__gt=2016) # __gt = greater than Article.objects.filter(year__lt=2001) # __lt = less than # Filter on relationship sub model field Article.objects.get(user__username='mike') # Ordering Article.objects.order_by('name') # ascending Article.objects.order_by('-name') # descending # Slicing return first Article.objects.all().order_by('name')[0] # Slicing return last Article.objects.all().order_by('-name')[0] # Slicing limit/offset Article.objects.all().order_by('name')[1..10] # Updating article = Article.objects.first() article.name = 'new name' article.save() # One line update Article.objects.filter(id=4).update(name='new name') # Deleting article = Article.objects.first() article.delete() # One line delete article.objects.get(id=1).delete() # Delete all Article.objects.all().delete() # Set ForeignKey field value model1 = Model(name='dyson') article.model = model1 # Get ForeignKey value article1.model.name model1.article_set.all() # Add Many-to-Many article1.tags.add(tag1) article1.tags.all() tag1.articles_set.all() ``` ## Form ### Form creation {.col-span-2} In HTML, a form is a collection of elements inside `` that allow a visitor to do things like enter text, select options, manipulate objects or controls, and so on, and then send that information back to the server. ```python # app_name/forms.py from django import forms class ArticleForm(forms.Form): name = forms.Charfield(max_length=100) description = forms.Charfield(blank=True, null = True) # Model Form from django.forms import ModelForm from app_name.models import Article class ArticleForm(ModelForm): class Meta: model = Article fields = ['name', 'description', 'price'] # Use '__all__' for all fields widgets = { 'name': forms.TextInput(attrs={'class': 'form-control'}), } labels = { 'name': 'First Name', } error_messages = { 'name': {'required': 'This field is required',}, } help_texts = { 'name': 'Enter Your First Name', } ``` ```html ``` - NOTE: If the form contains a file field, your form MUST contain `enctype="multipart/form-data"`, eg: ```html ``` ##### A Tailwind CSS template pack for the wonderful django-crispy-forms. ```bash pip install crispy-tailwind ``` ```python # settings.py INSTALLED_APPS = ( ... "crispy_forms", "crispy_tailwind", ... ) CRISPY_ALLOWED_TEMPLATE_PACKS = 'tailwind' CRISPY_TEMPLATE_PACK = 'tailwind' ``` ```html {% load tailwind_filters %} {{ form|crispy }} ``` ### Form Validation ```python # forms.py from django.core.exceptions import ValidationError # field validation def clean_first_name(self): data = self.cleaned_data['first_name'] if data = 'Mike': raise ValidationError('Your name must not be Mike') return data # form validation def clean(self): first_name = self.cleaned_data['first_name'] last_name = self.cleaned_data['last_name'] if first_name + last_name = 'MikeTaylor': raise ValidationError('Your name must not be Mike Taylor') ``` ## Flash messages {.cols-1} ### Displaying messages ```python # Message tags # debug, info, success, warning and error messages.success(request, 'Login successful') messages.error(request, 'Login error') ``` ```html {% if messages %} {% for message in messages %} {% message %} {% message.tags %} {% endfor %} {% endif %} ``` ## User Model ### Pre-created User Model (pre-created) ```python # Get a reference to Django pre-created User model from django.contrib.auth import get_user_model User = get_user_model() # Or if you want to custom user model from django.contrib.auth.models import AbstractUser class User(AbstractUser): # add custom fields and methods # To make Django use that model go to settings.py and add: AUTH_USER_MODEL = 'app_name.User' ``` ## Authentication ### Authentication : LoginView ```python # LoginView is already pre-created by Django from django.contrib.auth.views import LoginView # Add a url to reach that view path('login/', LoginView.as_view(), name='login') # By default the LoginView will try to open a template name 'registration/login.html' and send a login form with it. ``` ```html {% extends "base.html" %} {% block content %} {% endblock content %} ``` #### Authentication : LogoutView ```python # LogoutView is already pre-created by Django from django.contrib.auth.views import LogoutView # Add a url to reach that view path('logout/', LoginView.as_view(), name='logout') # Include a link in a template href="{% url 'logout' %}">Logout # After link is execute, the user will be logout and redirect to LOGOUT_REDIRECT_URL specified in your settings.py ``` #### Authentication : SignupView ```python # Create a SignupView (that view is not created by default) # import sinupview form pre-created by Django from django.contrib.auth.forms import UserCreationForm from django.views.generic import CreateView class SignupView(CreateView): template_name = 'registration/signup.html' form_class = UserCreationForm def get_success_url(self): return reverse("login") ``` ```html {% extends "base.html" %} {% block content %} {% endblock content %} ``` ```python # Add a url to reach that view from posts.views import SignupView path('signup/', SignupView.as_view(), name='signup') ``` ```python # Optional: Customize the UserCreationForm # (forms.py) from django.contrib.auth import get_user_model from django.contrib.auth.forms import UserCreationForm User = get_user_model() class CustomUserCreationForm(UserCreattionForm): class Meta: model = User fields = ['username'] fields_classes = {'username': UsernameField} ``` #### Optional pre-created Authentication routes ```python # urls.py urlpatterns += path('', include('django.contrib.auth.urls')) # /login, /lougout, /signup, etc. ``` #### Template Authentication helpers ```html Login Signup Logout {% if request.user.is_authenticated %} Logged in as: {{ request.user.username }} {% endif %} ``` #### Authorization: LoginRequiredMixin and login_required ```python from django.contrib.auth.mixins import LoginRequiredMixin # Restrict views to auth user only (views.py) class PostsCreateView(LoginRequiredMixin, generic.CreateView): ... ... from django.contrib.auth.decorators import login_required @login_required(login_url='/login') def search_page(request): ... ... ``` #### Manual Authentication , Login and Logout ```python from django.contrib.auth import authenticate, login def login_page(request): if request.method == "POST": username = request.POST.get("username") password = request.POST.get("password") user = authenticate(request, username=username, password=password) if user is not None: login(request, user) return redirect("index") return render(request, "registration/login.html", {}) def logout_page(request): logout(request) return redirect("index") ``` #### User Change password ```python # set_password will hash the password user.set_password('raw password') ``` ## Create Custom Accounts Model ```python ~$ python manage.py startapp accounts ``` ```python # Add accounts app to settings.py INSTALLED_APPS = [ … , 'accounts.apps.AccountsConfig', ] . . . AUTH_USER_MODEL = 'accounts.User' ``` ```python # accounts/models.py from django.db import models from django.contrib.auth.models import BaseUserManager, AbstractBaseUser, PermissionsMixin class UserManager(BaseUserManager): def create_user(self, username, email, password=None, **extra_fields): if not username: raise ValueError('Users must have an username') if not email: raise ValueError('Users must have an email') user = self.model(email=email, **extra_fields) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, username, email, password=None, **extra_fields): extra_fields.setdefault('is_staff', True) extra_fields.setdefault('is_superuser', True) extra_fields.setdefault('is_active', True) user = self.create_user(username, email, password=password, **extra_fields) user.save(using=self._db) return user class User(AbstractBaseUser, PermissionsMixin): username = models.CharField(verbose_name='Username', max_length=255, unique=True) email = models.EmailField(verbose_name='email', max_length=255, unique=True) phone = models.BigIntegerField(verbose_name='Phone Number', unique=True, blank=True, null=True) is_active = models.BooleanField(default=True, verbose_name="is_active") is_staff = models.BooleanField(default=False, verbose_name="is_staff") is_superuser = models.BooleanField(default=False, verbose_name="is_superuser") objects = UserManager() # We can use any field for username USERNAME_FIELD = 'username' # or 'email' or 'phone' REQUIRED_FIELDS = ['username', 'email'] def __str__(self): return self.username class Meta: verbose_name = "User" verbose_name_plural = "Users" ``` ```python # accounts/forms.py from django import forms from django.contrib.auth.forms import ReadOnlyPasswordHashField from django.core.exceptions import ValidationError from accounts.models import User from django.utils.translation import gettext_lazy as _ class UserCreationForm(forms.ModelForm): password1 = forms.CharField(label='Password', widget=forms.PasswordInput) password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput) class Meta: model = User fields = ('username', 'email', 'phone', 'is_active', 'is_staff') def clean_password2(self): password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") if password1 and password2 and password1 != password2: raise ValidationError("Passwords don't match") return password2 def save(self, commit=True): user = super().save(commit=False) user.set_password(self.cleaned_data["password1"]) if commit: user.save() return user class UserChangeForm(forms.ModelForm): password = ReadOnlyPasswordHashField() class Meta: model = User fields = ('username', 'email', 'phone', 'is_active', 'is_staff') def clean_password(self): return self.initial["password"] class LoginForm(forms.Form): email = forms.CharField( widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Email'}), label='Email') password = forms.CharField( widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Password'}),label='Password') class RegisterForm(forms.ModelForm): password1 = forms.CharField(label='Password', widget=forms.PasswordInput( attrs={'class': 'form-control'})) password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput(attrs={'class': 'form-control'})) class Meta: model = User fields = ('username', 'email', 'phone') widgets = { # Optional 'username': forms.TextInput(attrs={'class': 'form-control'}), 'email': forms.EmailInput(attrs={'class': 'form-control'}), 'phone': forms.NumberInput( attrs={'class': 'form-control', 'type': 'tel', 'maxlength': '11', 'minlength': '11', 'onkeypress': 'return isNumber(event)', 'required': 'false'})} def clean_password2(self): password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") if password1 and password2 and password1 != password2: raise ValidationError("Passwords don't match") else: return password2 def save(self, commit=True): user = super().save(commit=False) user.set_password(self.cleaned_data["password1"]) if commit: user.save() return user class EditProfileForm(forms.ModelForm): class Meta: model = User fields = ('username', 'email', 'phone') class ChangePassword(forms.Form): password1 = forms.CharField(label=_('Password'), widget=forms.PasswordInput( attrs={'class': 'form-control', 'placeholder': 'Password'})) password2 = forms.CharField(label=_('Password confirmation'), widget=forms.PasswordInput( attrs={'class': 'form-control', 'placeholder': 'Re-Enter Password'})) def clean_password2(self): password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") if password1 and password2 and password1 != password2: raise ValidationError("Passwords don't match") else: return password2 ``` ```python # accounts/admin.py from django import forms from django.contrib import admin from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from django.contrib.auth.forms import ReadOnlyPasswordHashField from django.core.exceptions import ValidationError from django.contrib.auth.models import Group from accounts.models import User class UserCreationForm(forms.ModelForm): password1 = forms.CharField(label='Password', widget=forms.PasswordInput) password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput) class Meta: model = User fields = ('username', 'email', 'phone') def clean_password2(self): # Check that the two password entries match password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") if password1 and password2 and password1 != password2: raise ValidationError("Passwords don't match") return password2 def save(self, commit=True): user = super().save(commit=False) user.set_password(self.cleaned_data["password1"]) if commit: user.save() return user class UserChangeForm(forms.ModelForm): password = ReadOnlyPasswordHashField() class Meta: model = User fields = ('username', 'email', 'phone', 'password', 'is_active', 'is_staff') class UserAdmin(BaseUserAdmin): form = UserChangeForm add_form = UserCreationForm list_display = ('username', 'email', 'is_active', 'is_staff', 'is_superuser') list_editable = ('is_staff', 'is_active') list_filter = ('is_staff', 'groups') fieldsets = ( (None, {'fields': ( 'username', 'email', 'phone', 'password')}), ('Permissions', {'fields': ('is_staff',)}), ('Group Permissions', { 'fields': ('is_active', 'is_superuser', 'groups', 'user_permissions'), }) ) add_fieldsets = ( (None, { 'classes': ('wide',), 'fields': ('username', 'email', 'phone', 'password1', 'password2'), }), ('Group Permissions', { 'fields': ('is_active', 'is_superuser', 'groups', 'user_permissions'), }) ) search_fields = ('username', 'email', 'phone') ordering = ('username', 'email', 'phone') filter_horizontal = ('groups', 'user_permissions',) admin.site.register(User, UserAdmin) # unregister the Group model from admin. # admin.site.unregister(Group) ``` ```python # accounts/urls.py from django.urls import path from accounts import views app_name = 'accounts' urlpatterns = [ path('login/', views.userLogin, name='login'), path('register/', views.userRegister, name='register'), path('logout/', views.LogoutPage, name='logout') ] ``` ```python # accounts/views.py from django.shortcuts import render, redirect from django.contrib.auth import login, logout, authenticate from django.contrib.auth.decorators import login_required from django.utils.translation import gettext_lazy as _ from accounts.forms import LoginForm, RegisterForm from django.contrib import messages from accounts.models import User def userRegister(request): form = RegisterForm() if request.method == 'POST': form = RegisterForm(request.POST) if form.is_valid(): cd = form.cleaned_data if not User.objects.filter(email=cd['username']).exists(): if not User.objects.filter(email=cd['email']).exists(): user = User.objects.create_user( username=cd['username'], phone=cd['phone'], email=cd['email'], password=cd['password1']) user.save() login(request, user) messages.success(request, _("You successfully registered a user"), extra_tags="success") return redirect('app_name:home') else: messages.error(request, _("This Email is exists"), extra_tags="warning") else: messages.error(request, _("This Username is exists"), extra_tags="warning") else: import json er = json.loads(form.errors.as_json()) for e in er: messages.error(request, er[e][0]['message'], 'warning') return render(request, 'accounts/register.html', {'form': form}) def userLogin(request): if not request.user.is_active: if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): cd = form.cleaned_data if User.objects.filter(username=cd['username']).exists(): user = authenticate(request, username=cd['username'], password=cd['password']) if user is not None: login(request, user) messages.success(request, _("logged in successfully"), extra_tags="success") return redirect('app_name:home') else: messages.error(request, _("your username Or Password is wrong"), extra_tags="warning") else: messages.error(request, _("No account created with this username"), extra_tags="warning") return redirect('accounts:login') else: messages.error(request, _("Please enter your information correctly"), extra_tags="warning") else: form = LoginForm() return render(request, 'accounts/login.html', {'form': form}) else: return redirect('app_name:home') @login_required() def LogoutPage(request): logout(request) messages.success(request, _("You Logged Out successfully"), extra_tags="success") return redirect('app_name:home') ``` ## Send Email ```python # settings.py EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" # Send email function from django.core.email import send_mail send_mail( subject = "A new post has been created", message = "Go to the web site to see the detail", from_email = "test@test.com", recipient_list = ["test2@text.com"] ) ``` ## Signals ```python # models.py from django.db.models.signals import post_save, pre_save def post_user_created_signal(sender, instance, created, **kwargs): if created: UserProfile.objects.create(user=instance) # Launch the post_user_created_signal method if User model is save post_save.connect(post_user_created_signal, sender=User) ``` ## Seed ```python from app_name.models import Product, Category from django.shortcuts import HttpResponse from faker import Faker def seed(request): Product.objects.all().delete() Category.objects.all().delete() category = Category() category.name = "Sports" category.save() category = Category() category.name = "Home" category.save() fake = Faker() for _ in range(100): product = Product() product.name = fake.unique.word() product.short_description = fake.sentence() product.main_picture = fake.image_url() product.price = fake.random_digit() * 10 product.category = Category.objects.order_by('?').first() product.save() return HttpResponse('Seeded') ``` ## Environment Variables ### .env key/value file ```python $ pip install python-decouple ``` Create a file name '.env' in the root folder of your project ```python SECRET_KEY = 'your secret key' ALLOWED_HOST = 127.0.0.1 ``` In settings.py change security related settings to point to the .env file ```python from decouple import config SECRET_KEY = config('SECRET_KEY') ALLOWED_HOST = config('ALLOWED_HOST') ``` ## Asynchronous Tasks ### Django Celery Redis Celery is a distributed task queue that can collect, record, schedule, and perform tasks outside of your main program. #### Step 1: Install Celery Using pip ```python ~$ pip install celery # pip install celery[redis] ``` #### Step 2. Add celery.py File in Your Project Module ```python # your_project/celery.py import os from celery import Celery os.environ.setdefault('DJANGO_SETTINGS_MODULE','your_project_name.settings') app = Celery('your_project_name') app.config_from_object('django.conf:settings', namespace='CELERY') app.autodiscover_tasks() @app.task(bind=True) def debug_task(self): print(f'Request: {self.request!r}') ``` #### Step 3: Import the Celery App to Django To ensure that the Celery app is loaded when Django starts, add the following code into the `__init__.py` file that sits on the project module beside on settings.py file. ```python # your_project/__init__.py from .celery import app as celery_app __all__ = ('celery_app',) ``` #### Step 4: Download and Run Redis as a Celery ‘broker’ ```python ~$ redis-server ``` You can test that Redis is working properly by typing this into your terminal: ```python ~$ redis-cli ping ``` Redis should reply with PONG - try it! #### Step 5: Add Redis as a Dependency in the Django Project: ```python ~$ pip install redis ``` #### Step 6: Celery Stuff Configure to the Django Settings File Once Redis is up, add the following code to your settings.py file and use celery-result ```python ~$ pip install django-celery-results ``` This extension enables you to store Celery task results using the Django ORM.