You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/technologies/django/getting-started.mdx
+68-10Lines changed: 68 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,9 +10,15 @@ import SubHeading from "@site/src/components/SubHeading";
10
10
11
11
## Introduction to Django
12
12
13
-
**Django** is a leading web framework actively supported and versioned by programming experts using a `batteries-included` concept. Django can help us to code from simple, one-page websites, APIs, micro-services, or complex eCommerce platforms. It enables developers to build robust applications rapidly while helping developers to avoid security mistakes since Django takes security seriously. Django is flexible and scalable and comes with a lot of tools prebuilt into it for example, its Object relation mapper (ORM), admin dashboard, user authentication and lots more.
13
+
**Django** is a leading web framework actively supported and versioned by programming experts using a `batteries-included` concept.
14
+
Django can help us to code from simple, one-page websites, APIs, micro-services, or complex eCommerce platforms.
15
+
It enables developers to build robust applications rapidly while helping developers to avoid security mistakes since Django takes security seriously.
16
+
Django is flexible and scalable and comes with a lot of tools prebuilt into it for example, its Object relation mapper (ORM), admin dashboard, user authentication and lots more.
14
17
15
-
Django adopts the MTV (Model Template View) architectural pattern. **Model** determines the data structure and is a handler between the database and the view. **Template** keeps everything the browser renders, django uses a plain text template system. **View** communicates with the database using the model and sends the data to the template to be displayed. The framework acts as a controller sending requests to the right view based on the URL configuration. Django's MVT pattern emphasizes the separation of concerns and modularity, making it easier to develop and maintain web applications with a clear separation between business logic, data access, and presentation.
18
+
Django adopts the MTV (Model Template View) architectural pattern. **Model** determines the data structure and is a handler between the database and the view.
19
+
**Template** keeps everything the browser renders, django uses a plain text template system. **View** communicates with the database using the model and sends the data to the template to be displayed. The framework acts as a controller sending requests to the right view based on the URL configuration. Django's MVT pattern emphasizes the separation of concerns and modularity, making it easier to develop and maintain web applications with a clear separation between business logic, data access, and presentation.
20
+
21
+
<br />
16
22
17
23
### Advantages of Using Django for web development
18
24
* Django enables rapid development by eliminating a lot of the hassle of web development
@@ -21,10 +27,16 @@ Django adopts the MTV (Model Template View) architectural pattern. **Model** det
21
27
* Since Django is built using Python, this gives the developer a wide range of third-party modules to select from to solve various challenges that may arise during development
22
28
* Django has a very vibrant community and it is regularly updated
23
29
30
+
<br />
31
+
24
32
## Installing Django: A Step-by-Step Guide
33
+
25
34
Before development can start with Django, it has to be installed using Python package manager (`pip`). Before Django is installed and development begins, there is a need to create a virtual environment, this way the development environment is isolated from the rest of our machine. This can help prevent dependency issues and also help in reproducibility.
`virtualenv` creates a virtual environment called venv, and the next line activates the virtual environment. `(venv)` indicates that the virtual environment is active. Now we can install Django without concerns of conflicts and dependency issues.
49
61
62
+
<br />
63
+
50
64
### Installing Django
65
+
51
66
Since our virtual environment is active we can install Django and start development. In your terminal run the following commands
A Django project is a collection of settings and configurations that define the structure and behavior of a web application. It is a high-level representation of a web application that includes multiple components such as models, views, templates, forms, and static files.
58
76
59
-
### Creating a Django project
77
+
<br />
78
+
79
+
### Creating the Project
80
+
60
81
A Django project is created using the `django-admin` command-line tool. `django-admin startproject <project-name>` is the command used to create a new Django project.
61
82
62
83
* Run the command below to create a Django project called core
@@ -76,7 +97,10 @@ After running the command above, your directory structure will look like this. T
76
97
└── manage.py
77
98
```
78
99
100
+
<br />
101
+
79
102
### Applying initial Database configuration
103
+
80
104
Django comes with some database configurations which can be found in `core/settings.py`, the default database configuration for Django is SQLite3. Django is built to integrate with a lot of popular databases like MySQL, Postgresql, Oracle and so on. Check Django documentation [here](https://docs.djangoproject.com/en/4.2/ref/databases/) for more information on how to get started using other database engines. The command below applies the initial migration for all the installed applications for the Django project.
81
105
```bash
82
106
(venv) django-tutorial$ python manage.py migrate
@@ -89,7 +113,10 @@ Django comes with some database configurations which can be found in `core/setti
89
113
This starts up a Django server, open `http://127.0.0.1:8000/` in your browser, you should see the page below. You can stop the server from the terminal by using `Ctrl + c`.
In Django, an application is a self-contained module of code that provides specific functionality to a project. It is a collection of models, views, templates, and other related files that work together to implement a particular feature or set of features. An application is typically stored in its directory within the project's directory structure. Each application contains a set of files that define its functionality, including:
94
121
95
122
* Models: A model is a Python class that defines the structure of a database table and the relationships between tables. Models are used to define the data structure of the application.
@@ -102,7 +129,10 @@ In Django, an application is a self-contained module of code that provides speci
102
129
103
130
* Static files: Static files are files such as CSS, JavaScript, and images that are used to define the presentation layer of the application.
104
131
132
+
<br />
133
+
105
134
### Creating a Django application
135
+
106
136
Using the `django-admin` command from the terminal, we will be creating a Django application inside the project directory. `django-admin startapp <app-name>` is the command used for creating new applications.
107
137
108
138
* Run the command below on your terminal to create a Django application name example
@@ -131,7 +161,10 @@ With this, a new application called `example` has been created. The new folder s
131
161
```
132
162
Although we have created an application, the Django project does not recognize our application, we will need to register the application with the project to allow the project to recognize the application.
133
163
164
+
<br />
165
+
134
166
### Connecting Django application to project
167
+
135
168
To connect `example` to the Django project `core`, open the file `core/settings.py` and add `example` to the `INSTALLED_APPS` list.
136
169
```py
137
170
# core/settings.py
@@ -149,7 +182,10 @@ INSTALLED_APPS = [
149
182
```
150
183
Now our Django project recognizes the `example` application.
151
184
185
+
<br />
186
+
152
187
### Adding `example` to project routes
188
+
153
189
Now that the Django project can recognize the `example` application, the next step is to give the application a route. By doing this we create a generic route that when visited, Django checks this application for resources to send back to the client.
154
190
155
191
* Inside `core/urls.py` add the following code snippet to register the root route to the `example` application.
@@ -165,7 +201,10 @@ urlpatterns = [
165
201
```
166
202
If your Django server was running when this change was made, you would be having an error. not to worry, it will be fixed in a few steps.
167
203
204
+
<br />
205
+
168
206
### Serving an HTML page from the example route
207
+
169
208
* Edit the `core/settings file by adding a route to template files for Django to use in identifying resources to be sent as the response
170
209
```py
171
210
# core/settings.py
@@ -251,7 +290,10 @@ The new folder structure will look like this
251
290
│ └── index.html
252
291
```
253
292
293
+
<br />
294
+
254
295
## How to install a design
296
+
255
297
Because Django applications are reusable, it is possible to get a predesigned application that fits into what you are developing and integrate it into your project. The Django repository is home to a lot of predesigned applications. Those applications can be installed using the Python package manager `pip`.
256
298
For this tutorial, we will be using the [`django-theme-material-kit`](https://github.com/app-generator/django-theme-material-kit).
257
299
@@ -311,10 +353,16 @@ Opening the URL `http://127.0.0.1:8000` from your browser you will see a new app
311
353
312
354
With predesigned themes, development can be made faster and you can focus on business-specific logic.
313
355
356
+
<br />
357
+
314
358
## Deploying a Django project
359
+
315
360
The application just built will be deployed on [Render](https://render.com) and also deployed using Docker.
316
361
362
+
<br />
363
+
317
364
### Initial setup
365
+
318
366
Before we can start deploying our project, we need to make some changes to our codebase.
319
367
320
368
* Installing `whitenoise` a package used for handling the hosting of static files. From your terminal run the command below
@@ -393,7 +441,10 @@ The changes made above combine the configuration for Docker and deploying on ren
* In the root directory of your application, create a file name `build.sh` and add the following line
398
449
```bash
399
450
#!/usr/bin/env bash
@@ -443,7 +494,10 @@ fi
443
494
444
495
* Save the web service and deploy the application on `Render`
445
496
497
+
<br />
498
+
446
499
### Running Django project on Docker
500
+
447
501
The following steps will help you create a docker image and start up a docker container for your Django project
448
502
* Create a file `.dockerignore` in the root directory with the following content
449
503
```
@@ -491,8 +545,12 @@ This creates the docker image for our application. Using the command `docker ima
491
545
```
492
546
With your web browser, open `127.0.0.1:8000` or `localhost:8000` to see your web application.
493
547
548
+
<br />
549
+
494
550
## Conclusion
495
-
In this Django tutorial, we have covered several concepts related to building web applications with Django, including using pre-designed templates, deploying projects to production, and containerizing applications with Docker.
551
+
552
+
In this Django tutorial, we have covered several concepts related to building web applications with Django, including using pre-designed templates, deploying projects to production,
553
+
and containerizing applications with Docker.
496
554
497
555
We began by discussing pre-designed templates, which can help you quickly set up and style your website without the need for a graphic designer.
498
556
@@ -502,9 +560,9 @@ Finally, we explored how to containerize a Django application using Docker, a po
502
560
503
561
By the end of this tutorial, you should have a good understanding of several important concepts related to building and deploying Django applications. Whether you are building a small web app or a large-scale enterprise application, the tools and techniques we've covered should help you build better and more maintainable software.
504
562
505
-
### Further reading
506
-
-[Django](https://www.djangoproject.com/) - official website
507
-
-[Django](https://docs.djangoproject.com/en/4.0/) - official documentation
0 commit comments