/update/', PostsUpdateView.as_view(), name='post-update')
{% endwith %}
```
-## Create custom template tags and filters
+### Custom Template Tags and Filters
-For example, if your custom tags/filters are in a file called `basetags.py`, your app layout might look like this:
+For example, if your custom tags/filters are in a file called `basetags.py`, your app layout might look like this:
```
app_name/
@@ -591,19 +596,17 @@ app_name/
{% load basetags %}
```
-- the module must contain a module-level variable named register that is a template.Library instance, in which all the tags and filters are registered. So, near the top of your module, put the following:
+- the module must contain a module-level variable named register that is a template.Library instance, in which all the
+ tags and filters are registered. So, near the top of your module, put the following:
-#### Writing custom template filters
+### Writing custom template filters
-Custom filters are Python functions that take one or two arguments:
-The value of the variable (input) – not necessarily a string.
-The value of the argument – this can have a default value, or be left out altogether.
-For example, in the filter `{{ var|foo:"bar" }}`, the filter foo would be passed the variable var and the argument "bar".
+Custom filters are Python functions that take one or two arguments: The value of the variable (input) – not necessarily
+a string. The value of the argument – this can have a default value, or be left out altogether. For example, in the
+filter `{{ var|foo:"bar" }}`, the filter foo would be passed the variable var and the argument "bar".
-The Library.filter() method takes two arguments:
-The name of the filter – a string.
-The compilation function – a Python function (not the name of the function as a string).
-You can use register.filter() as a decorator instead:
+The Library.filter() method takes two arguments: The name of the filter – a string. The compilation function – a Python
+function (not the name of the function as a string). You can use register.filter() as a decorator instead:
```python
# basetags.py
@@ -636,9 +639,11 @@ def to_class_name(object):
Model Class Name: {{ objectmodel }}
```
-## Creating custom Context Processor
+## Context Processor {.cols-1}
-1. Anywhere, create a context_processors.py file
+### Creating custom Context Processor
+
+Anywhere, create a `context_processors.py` file
```
project_name
@@ -647,8 +652,10 @@ project_name
└───context_processors.pyy
```
-2. 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:
+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
@@ -674,7 +681,9 @@ def site_email(request):
## Model Managers and Querysets
-Model manager allow model database reads and writes
+### Model Manager
+
+Model manager allow model database reads and writes
```python
# One line create and save
@@ -755,7 +764,10 @@ tag1.articles_set.all()
## Form
-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.
+### 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
@@ -810,7 +822,7 @@ CRISPY_TEMPLATE_PACK = 'tailwind'
{{ form|crispy}}
```
-#### Form validation
+### Form Validation
```python
# forms.py
@@ -831,7 +843,11 @@ def clean(self):
raise ValidationError('Your name must not be Mike Taylor')
```
-## Flash messages
+## Flash messages {.cols-1}
+
+### Displaying messages
+
+
```python
messages.success(request, 'Login successful')
@@ -849,7 +865,9 @@ messages.error(request, 'Login error')
## User Model
-User Model (pre-created)
+### Pre-created
+
+User Model (pre-created)
```python
# Get a reference to Django pre-created User model
@@ -866,9 +884,9 @@ class User(AbstractUser):
# To make Django use that model go to settings.py and add: AUTH_USER_MODEL = 'app_name.User'
```
-## Authentication Configure
+## Authentication
-#### Authentication : LoginView
+### Authentication : LoginView
```python
# LoginView is already pre-created by Django
@@ -1416,20 +1434,20 @@ def seed(request):
## Environment Variables
-#### .env key/value file
+### .env key/value file
```python
$ pip install python-decouple
```
-##### Create a file name '.env' in the root folder of your project
+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
+In settings.py change security related settings to point to the .env file
```python
from decouple import config
@@ -1438,17 +1456,19 @@ SECRET_KEY = config('SECRET_KEY')
ALLOWED_HOST = config('ALLOWED_HOST')
```
-## Asynchronous Task with Django Celery Redis
+## Asynchronous Tasks
-Celery is a distributed task queue that can collect, record, schedule, and perform tasks outside of your main program.
+### Django Celery Redis
-##### Step 1: Install Celery Using pip
+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
+#### Step 2. Add celery.py File in Your Project Module
```python
# your_project/celery.py
@@ -1465,9 +1485,10 @@ def debug_task(self):
print(f'Request: {self.request!r}')
```
-##### Step 3: Import the Celery App to Django
+#### 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.
+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
@@ -1476,7 +1497,7 @@ from .celery import app as celery_app
__all__ = ('celery_app',)
```
-##### Step 4: Download and Run Redis as a Celery ‘broker’
+#### Step 4: Download and Run Redis as a Celery ‘broker’
```python
~$ redis-server
@@ -1488,24 +1509,25 @@ __all__ = ('celery_app',)
~$ redis-cli ping
```
-> Redis should reply with PONG - try it!
+Redis should reply with PONG - try it!
-##### Step 5: Add Redis as a Dependency in the Django Project:
+#### 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
+#### 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
+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.
-> It defines a single model (`django_celery_results.models.TaskResult`) used to store task results, and you can query this database table like any other Django model.
+This extension enables you to store Celery task results using the Django ORM.
It defines a single model
+(`django_celery_results.models.TaskResult`) used to store task results, and you can query this database table like any
+other Django model.
```python
INSTALLED_APPS = [
@@ -1533,15 +1555,16 @@ CACHES = {
}
```
-##### That’s it! You should now be able to use Celery with Django
+#### That’s it! You should now be able to use Celery with Django
-Test that the Celery worker is ready to receive tasks:
+Test that the Celery worker is ready to receive tasks:
```python
~$ celery -A your_project_name worker -l info
```
-> The most important task is: Always run a worker is needed to execute the celery task
if any error throws from Redis like this:
+The most important task is: Always run a worker is needed to execute the celery task if any error throws from Redis like
+this:
```python
AttributeError: 'str' object has no attribute 'items'
@@ -1549,9 +1572,9 @@ AttributeError: 'str' object has no attribute 'items'
- the solution is: you have to use Redis old version
-#### Add a New Task to the Celery Step by Step:
+### Add a New Task
-##### Step 1: Add tasks.py File to Your Django App.
+#### Step 1: Add tasks.py File to Your Django App.
```python
# app_name/tasks.py
@@ -1578,9 +1601,10 @@ def adding(x, y):
return result
```
-##### Step 2: Assign Task to the Celery.
+#### Step 2: Assign Task to the Celery.
-You need to assign a task to the celery. To assign this task you need to call this function with something different. celery gives us two methods `delay()` and `apply_async()` to call task.
+You need to assign a task to the celery. To assign this task you need to call this function with something different.
+celery gives us two methods `delay()` and `apply_async()` to call task.
```bash
# Normal function call in python
@@ -1590,14 +1614,15 @@ def adding(x, y):
~$ my_first_task.delay()
```
-> you can send argument to the function using the delay method.
-> To check celery on the action open a separate tab of the terminal then go to the project directory (activate environment if you are using one) and run this command again
+You can send argument to the function using the delay method. To check celery on the action open a separate tab
+of the terminal then go to the project directory (activate environment if you are using one) and run this command
+again
```bash
~$ celery -A your_project_name worker -l info
```
-##### Create a View in your App
+### Create a View in your App
```python
# app_name/views.py
@@ -1614,7 +1639,7 @@ def test(request):
return HttpResponse("Done")
```
-##### Then call the view from your app URL
+#### Then call the view from your app URL
```python
# app_name/urls.py
@@ -1626,7 +1651,7 @@ urlpatterns = [
]
```
-### Celery In Production Using Supervisor on Linux Server Step by Step:
+### Celery In Production Using Supervisor
#### Step 1: Install Supervisor on Ubuntu Server
@@ -1640,7 +1665,7 @@ urlpatterns = [
~$ sudo nano /etc/supervisor/conf.d/app_name.conf
```
-> app_name can be anything you like, it should be similar to your project name.
+`app_name` can be anything you like, it should be similar to your project name.
#### Step 3: Add some Configure in app_name.conf
@@ -1655,7 +1680,7 @@ stdout_logfile=/path/to/workflow/your_project_name/logs/celeryd.log
redirect_stderr=true
```
-> let's describe the configure file:
+Describe the configure file:
```bash
[program:your_app_name]
@@ -1685,7 +1710,8 @@ redirect_stderr=true
#### Step 4: Inform Configuration to the Server
-After adding a new program, we should run the following two commands, to inform the server to reread the configuration files and to apply any changes.
+After adding a new program, we should run the following two commands, to inform the server to reread the configuration
+files and to apply any changes.
```python
~$ sudo supervisorctl reread
@@ -1698,14 +1724,15 @@ redirect_stderr=true
~$ sudo supervisorctl
```
-You will be greeted with a list of the registered processes. You will see a process called `your_app_name` with a `RUNNING` status.
+You will be greeted with a list of the registered processes. You will see a process called `your_app_name` with a
+`RUNNING` status.
```bash
your_app_name RUNNING pid 6853, uptime 0:22:30
supervisor>
```
-Type `help` for a list of available commands.
+Type `help` for a list of available commands.
```bash
supervisor> help
@@ -1716,9 +1743,8 @@ avail fg pid remove shutdown status update
clear maintail quit reread signal stop version
```
-In a nutshell, we can `start`, `stop` and `restart` programs bypassing the program name as an argument to the respective command.
-We can also take a look at the program output with the `tail` command.
-Once you are finished, you can `quit`.
+In a nutshell, we can `start`, `stop` and `restart` programs bypassing the program name as an argument to the respective
+command. We can also take a look at the program output with the `tail` command. Once you are finished, you can `quit`.
```bash
~$ supervisor> quit