Skip to content

Commit 21a88b4

Browse files
committed
Release v1.0.33 - How to DEBUG Django
1 parent e69671f commit 21a88b4

File tree

2 files changed

+71
-37
lines changed

2 files changed

+71
-37
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## [1.0.33] 2023-05-10
4+
### Changes
5+
6+
- Update [Django](https://docs.appseed.us/technologies/django/) Section
7+
- [How to DEBUG Django](https://docs.appseed.us/technologies/django/how-to-debug/)
8+
39
## [1.0.32] 2023-05-10
410
### Changes
511

docs/technologies/django/how-to-debug.mdx

Lines changed: 65 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,32 @@ description: Learn how to DENUG Django and activate Logging
55

66
import SubHeading from "@site/src/components/SubHeading";
77

8-
<SubHeading color="#25c2a0">Learn how to DENUG Django and activate Logging</SubHeading>
8+
<SubHeading color="#25c2a0">Learn how to DBUG Django and activate Logging</SubHeading>
99

10-
Logging and Debugging are essential aspects of any web application, as it allows developers to record and analyze the execution of their code, making it easier to identify and resolve issues. In a Django application, proper logging can not only save valuable time during development and debugging but also help you ensure the stability and security of your application in production.
10+
**Logging** and **Debugging** are essential aspects of any web application, as it allows developers to record and analyze the execution of their code, making it easier to identify and resolve issues.
11+
In a **Django** application, proper logging can not only save valuable time during development and debugging but also help you ensure the stability and security of your application in production.
1112

1213
In this article, we will discuss the importance of logging and debugging in Django applications. Our journey will be divided into three key sections:
1314

14-
* Configuring Django for Logging
15+
* Configuring Django for Logging
1516

16-
* Using `pdb` in a Django Application
17-
18-
* Configuring Django Debug Toolbar
17+
* ✅ Using `pdb` in a Django Application
1918

19+
* ✅ Configuring Django Debug Toolbar
2020

2121
So, let's dive in and explore the world of Django logging!
2222

23+
<br />
24+
2325
## Project Setup
2426

25-
For this article, you can find the code on GitHub at [https://github.com/app-generator/sample-django-debug](https://github.com/app-generator/sample-django-debug).
27+
For this article, you can find the code on GitHub at **[How to DEBUG Django Sample](https://github.com/app-generator/sample-django-debug)**.
2628

2729
To make things quicker, you can clone this specific commit so you can follow the tutorial.
2830

2931
```bash
30-
$ git clone https://github.com/app-generator/django-volt-dashboard.git
31-
$ cd django-volt-dashboard
32-
$ git checkout e2cb798e96f8b41a94c981c617b93f88ed9034ec
32+
$ git clone https://github.com/app-generator/sample-django-debug.git
33+
$ cd sample-django-debug
3334
```
3435

3536
Once the project is set up, you can install the dependencies and start the Django project.
@@ -43,9 +44,12 @@ $ python manage.py migrate
4344
$ python manage.py runserver
4445
```
4546

47+
<br />
48+
4649
## Configuring Django for Logging
4750

48-
Django natively integrates logging using Python's built-in logging module. This integration allows developers to easily track events, monitor application behavior, and diagnose issues. Django allows you principally to use the default logging system or to create your custom loggers.
51+
Django natively integrates logging using Python's built-in logging module. This integration allows developers to easily track events, monitor application behavior, and diagnose issues.
52+
Django allows you principally to use the default logging system or to create your custom loggers.
4953

5054
* **Default logging settings**: Django provides a default logging configuration that is applied if you don't explicitly configure logging in your `settings.py`. This default configuration logs messages with the `WARNING` level and above to the console. In `DEBUG` mode, Django also logs SQL queries.
5155

@@ -54,6 +58,8 @@ Django natively integrates logging using Python's built-in logging module. This
5458

5559
We will write our configuration for Logging in the next lines.
5660

61+
<br />
62+
5763
### Custom Logging Settings
5864

5965
As stated in the previous section, customizing the `Logging` in Django add the following configuration in `core/settings.py`.
@@ -99,46 +105,45 @@ LOGGING = {
99105

100106
Let's explain the code we have above :
101107

102-
1. General configuration
103-
108+
> 👉 `General configuration`
104109
105-
* `version`: `1`
110+
* **version**: `1`
106111

107112
* The version key is set to 1, indicating that this is a version 1 schema for logging configuration, which is the default and only version available for the logging configuration in Python's logging module.
108113

109-
* `disable_existing_loggers`: `False`
114+
* **disable_existing_loggers**: `False`
110115

111116
* This setting controls whether or not to disable all existing loggers when the configuration is applied. By setting it to `False`, the existing loggers are enabled.
112117

113118

114-
1. Formatters
119+
> 👉 **Formatters**
115120
116121

117-
* `default`
122+
* **default**
118123

119124
* A custom formatter with the format string `"%(asctime)s %(name)-12s %(levelname)-8s %(message)s"`. This format includes a timestamp, logger name, log level, and the log message.
120125

121-
* `django.server`
126+
* **django.server**
122127

123128
* This formatter is the same as the default formatter for the `django.server` logger.
124129

125130

126-
1. Handlers
131+
> 👉 **Handlers**
127132
128133

129-
* `apps_home`
134+
* **apps_home**
130135

131136
* A file handler that logs messages with a log level of `DEBUG` and above to a file named `logs/apps_home.log`.
132137

133-
* `console`
138+
* **console**
134139

135140
* A stream handler logs messages with a log level of `DEBUG` and above to the console (standard output). This handler uses the `default` formatter defined earlier.
136141

137142

138-
1. Loggers
143+
> 👉 **Loggers**
139144
140145

141-
* `apps.home`
146+
* **apps.home**
142147

143148
* A logger for the `apps.home` module (replace this with the actual name of your application or module). This logger has two handlers, `apps_home` and `console`, which means it will log messages to both the specified log file and the console. The log level is set to `DEBUG`, so it will log messages with a level of `DEBUG` and above. The `propagate` key is set to `False`, which means that the log messages handled by this logger will not be passed to higher-level loggers (e.g., the root logger or the `django` logger).
144149

@@ -191,7 +196,7 @@ This code is part of a Django view module that demonstrates how to use the custo
191196

192197
Visit the project at this link : `http://localhost:8000/test_logs` and you will see the current result
193198

194-
![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682865033264/a1c3deb4-fe65-44f7-9a13-8c24ddddfd63.png)
199+
![Debug Django - Error Page](https://cdn.hashnode.com/res/hashnode/image/upload/v1682865033264/a1c3deb4-fe65-44f7-9a13-8c24ddddfd63.png)
195200

196201
And you can check the `logs/apps_home.log` and you will see the content.
197202

@@ -203,9 +208,12 @@ And we have just logged our first error. Using the logging feature integrated wi
203208

204209
In the next section, we will explore how to use `pdb` for debugging in a Django application.
205210

206-
## Using pdb for Debugging in Django Application
211+
<br />
207212

208-
`pdb`, short for "Python Debugger", is a built-in Python module that provides a way to interactively debug your code. You can use `pdb` in a Django application to debug your views, models, or other code within the app. We will use `pdb` in the `test_logs` view by adding `pdb.set_trace()` after the line `users = User.objects.all()`.
213+
## Using **PDB** for Debugging
214+
215+
`pdb`, short for "Python Debugger", is a built-in Python module that provides a way to interactively debug your code.
216+
You can use `pdb` in a Django application to debug your views, models, or other code within the app. We will use `pdb` in the `test_logs` view by adding `pdb.set_trace()` after the line `users = User.objects.all()`.
209217

210218
```python
211219
...
@@ -216,9 +224,11 @@ def test_logs(request):
216224
...
217225
```
218226

219-
The line `pdb.set_trace()` is used to create a breakpoint in your Python code when using the `pdb` debugger. When the code execution reaches this line, it will pause and open an interactive debugging session in your terminal or console.
227+
The line `pdb.set_trace()` is used to create a breakpoint in your Python code when using the `pdb` debugger.
228+
When the code execution reaches this line, it will pause and open an interactive debugging session in your terminal or console.
220229

221-
At this point, you can interact with the debugger using various commands to inspect the variables, step through the code, execute arbitrary code, and more. This helps you identify and fix issues in your code more efficiently by providing an interactive debugging environment.
230+
At this point, you can interact with the debugger using various commands to inspect the variables, step through the code, execute arbitrary code, and more.
231+
This helps you identify and fix issues in your code more efficiently by providing an interactive debugging environment.
222232

223233
Here are some useful `pdb` commands you can use during the debugging session:
224234

@@ -239,13 +249,15 @@ Here are some useful `pdb` commands you can use during the debugging session:
239249
* `h` (help): Show a list of available commands.
240250

241251

242-
Visit this URL again [http://localhost:8000/test\_logs](http://localhost:8000/test_logs) and go on the terminal when you are running the Django server and let's type some commands. You can check the results of these commands typed in the image below.
252+
Visit this URL again [http://localhost:8000/test\_logs](http://localhost:8000/test_logs) and go on the terminal when you are running the Django server and let's type some commands.
253+
You can check the results of these commands typed in the image below.
243254

244255
![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682867617748/b896ea35-e0e3-4300-987e-b123af4ba1fa.png)
245256

246257
To quit the interactive debugging terminal, just type `q` and Django will continue the execution of the code.
247258

248-
Pdb is a great tool you can use in development to leverage your debugging requirements in a Django application. However, it's not recommended to use `pdb.set_trace()` in a live, production environment, as it may cause the application to hang when it reaches the breakpoint. Use `pdb` only during development and testing.
259+
Pdb is a great tool you can use in development to leverage your debugging requirements in a Django application.
260+
However, it's not recommended to use `pdb.set_trace()` in a live, production environment, as it may cause the application to hang when it reaches the breakpoint. Use `pdb` only during development and testing.
249261

250262
> If you're using Python 3.7 or later, you can use the built-in `breakpoint()` function instead of `pdb.set_trace()`. To do this, simply replace the `pdb.set_trace()` line with:
251263
>
@@ -255,13 +267,18 @@ Pdb is a great tool you can use in development to leverage your debugging requir
255267
>
256268
> By default, this will invoke `pdb`, but it can be customized to use other debuggers if needed.
257269
258-
We now understand how to use `pdb` to debug in our Django application. However, you might also want to have real-time data and a better description of errors when building a web application or an API with Django. In the next section, we will learn why and how to use `django-debug-toolbar`.
270+
We now understand how to use `pdb` to debug in our Django application.
271+
However, you might also want to have real-time data and a better description of errors when building a web application or an API with Django.
272+
In the next section, we will learn why and how to use `django-debug-toolbar`.
273+
274+
<br />
259275
260-
## Integrating django-debug-toolbar
276+
## Integrating `django-debug-toolbar`
261277
262278
As a developer, you might have already faced issues such as performance issues, SQL queries, template rendering issues, and more, all within the context of your application.
263279
264-
[**Django Debug Toolbar**](https://django-debug-toolbar.readthedocs.io/en/latest/) is a third-party package that provides a set of panels displaying various debug information about the current request/response in your Django application. It is an essential tool for Django developers and it can help you with the following issues:
280+
[**Django Debug Toolbar**](https://django-debug-toolbar.readthedocs.io/en/latest/) is a third-party package that provides a set of panels displaying various debug information about the current request/response in your Django application.
281+
It is an essential tool for Django developers and it can help you with the following issues:
265282
266283
1. **Performance analysis**: It allows you to analyze the performance of your views, identifying potential bottlenecks and slow database queries.
267284
@@ -302,7 +319,8 @@ INSTALLED_APPS = [
302319
...
303320
```
304321
305-
The next step is to add the middleware. In the `settings.py` file, add `'debug_toolbar.middleware.DebugToolbarMiddleware'` to the `MIDDLEWARE` list. Ensure it is placed after `'django.middleware.common.CommonMiddleware'`.
322+
The next step is to add the middleware. In the `settings.py` file, add `'debug_toolbar.middleware.DebugToolbarMiddleware'` to the `MIDDLEWARE` list.
323+
Ensure it is placed after `'django.middleware.common.CommonMiddleware'`.
306324
307325
```python
308326
...
@@ -315,7 +333,8 @@ MIDDLEWARE = [
315333
...
316334
```
317335
318-
We can then configure the internal IPs and add some configuration for the js files. The toolbar will only be shown for clients connecting from specific IP addresses. In your settings.py file, set the `INTERNAL_IPS` setting to include your IP address or use `'127.0.0.1'` and `localhost` for local development:
336+
We can then configure the internal IPs and add some configuration for the js files. The toolbar will only be shown for clients connecting from specific IP addresses.
337+
In your settings.py file, set the `INTERNAL_IPS` setting to include your IP address or use `'127.0.0.1'` and `localhost` for local development:
319338
320339
```python
321340
INTERNAL_IPS = [
@@ -369,8 +388,17 @@ And we are done.🚀 We have just integrated `django-debug-toolbar` in the Djang
369388
370389
Coupled with `pdb` and `logging`, you have a set of powerful tools you can use for debugging in your Django application.
371390
391+
<br />
392+
372393
## Conclusion
373394
374-
In this article, we have talked about `Django Debug Toolbar`, `pdb`, and `logging` which are essential tools for efficient Django application development. Utilizing these tools, you can identify performance bottlenecks, debug code issues, and optimize database queries with ease.
395+
In this article, we have talked about `Django Debug Toolbar`, `pdb`, and `logging` which are essential tools for efficient Django application development.
396+
Utilizing these tools, you can identify performance bottlenecks, debug code issues, and optimize database queries with ease.
397+
398+
<br />
399+
400+
## Resources
375401
376-
You can find the code in this article on [https://github.com/app-generator/sample-django-debug](https://github.com/app-generator/sample-django-debug).
402+
- 👉 [How to DEBUG Django](https://github.com/app-generator/sample-django-debug) - the sample updated for this tutorial
403+
- 👉 Free [Support](https://appseed.us/support/) via Email & Discord
404+
- 👉 [Custom Development Services](https://appseed.us/custom-development/) provided by experts

0 commit comments

Comments
 (0)