mirror of
https://github.com/lemeow125/Notes.git
synced 2024-11-17 04:09:26 +08:00
vault backup: 2024-10-14 13:51:48
This commit is contained in:
parent
d6def9352e
commit
fe17e76027
5 changed files with 46 additions and 49 deletions
|
@ -4,9 +4,9 @@
|
|||
|
||||
Django is a framework built with Python to help you implement web applications.
|
||||
|
||||
![[_resources/1_Setup/353e9c415078b3d4a18d7bd93294df1c_MD5.jpg]]
|
||||
![353e9c415078b3d4a18d7bd93294df1c_MD5](_resources/1_Setup/353e9c415078b3d4a18d7bd93294df1c_MD5.jpg)
|
||||
|
||||
![[_resources/1_Setup/8d193fcb83f676cd16e263f78f965bc5_MD5.jpg]]
|
||||
![8d193fcb83f676cd16e263f78f965bc5_MD5](_resources/1_Setup/8d193fcb83f676cd16e263f78f965bc5_MD5.jpg)
|
||||
|
||||
Django can be used as-is to develop full stack web apps which include your frontend and backend. This however isn't the focus of this guide.
|
||||
|
||||
|
@ -22,17 +22,17 @@ You will need to install the latest version of Python (version 3.11.2 as of 2/19
|
|||
|
||||
During the installation, make sure to check **Add Python 3.x to PATH**
|
||||
|
||||
![[_resources/1_Setup/373ea207d1abd6f0a734506d8ab4acbc_MD5.jpg]]
|
||||
![373ea207d1abd6f0a734506d8ab4acbc_MD5](_resources/1_Setup/373ea207d1abd6f0a734506d8ab4acbc_MD5.jpg)
|
||||
|
||||
To check if you have a working install, open a command line (CMD) and type in `python --version`
|
||||
|
||||
![[_resources/1_Setup/5d1081e556bd19260f14a235fef4e383_MD5.jpg]]
|
||||
![5d1081e556bd19260f14a235fef4e383_MD5](_resources/1_Setup/5d1081e556bd19260f14a235fef4e383_MD5.jpg)
|
||||
|
||||
If this does not work, you will have to check your PATH variables. If it does, you can safely proceed to the next step.
|
||||
|
||||
![[_resources/1_Setup/3ed75d2e3ff3aad581b4cd7c2af748a3_MD5.jpg]]
|
||||
![3ed75d2e3ff3aad581b4cd7c2af748a3_MD5](_resources/1_Setup/3ed75d2e3ff3aad581b4cd7c2af748a3_MD5.jpg)
|
||||
|
||||
![[_resources/1_Setup/aef7461456894804006fa820f6a2061e_MD5.jpg]]
|
||||
![aef7461456894804006fa820f6a2061e_MD5](_resources/1_Setup/aef7461456894804006fa820f6a2061e_MD5.jpg)
|
||||
|
||||
### Install [Pipenv](https://pipenv.pypa.io/en/latest/)
|
||||
|
||||
|
@ -58,7 +58,7 @@ Open up a command line (CMD) and type in `pip install --user pipenv` to install
|
|||
|
||||
This will use the package manager for Python (akin to npm for Javascript/Typescript projects) to install the Pipenv package
|
||||
|
||||
![[_resources/1_Setup/8e0864b9971e3e0ec0843db1494a9164_MD5.jpg]]
|
||||
![8e0864b9971e3e0ec0843db1494a9164_MD5](_resources/1_Setup/8e0864b9971e3e0ec0843db1494a9164_MD5.jpg)
|
||||
|
||||
### Install [Git](https://git-scm.com/downloads)
|
||||
|
||||
|
@ -66,10 +66,10 @@ When developing applications, you will need to use version control to track chan
|
|||
|
||||
Head over to the [downloads](https://git-scm.com/downloads/win) page and install the 64-bit Git for Windows Setup version (Standalone Installer)
|
||||
|
||||
![[_resources/1_Setup/c2f115b66c9b0f4e93ea54d1091f9696_MD5.jpg]]
|
||||
![c2f115b66c9b0f4e93ea54d1091f9696_MD5](_resources/1_Setup/c2f115b66c9b0f4e93ea54d1091f9696_MD5.jpg)
|
||||
This guide does not cover the proper usage of Git and so you may need to look up for other resources if you are not familiar with it yet.
|
||||
|
||||
![[_resources/1_Setup/6198958f0bad6ed532a840df3435ad23_MD5.jpg]]
|
||||
![6198958f0bad6ed532a840df3435ad23_MD5](_resources/1_Setup/6198958f0bad6ed532a840df3435ad23_MD5.jpg)
|
||||
You can proceed with the typical installation using defaults (no need to change anything), just click Next/Install.
|
||||
|
||||
With that out of the way, you can proceed to setting up your Django REST Framework project.
|
|
@ -6,41 +6,41 @@ With your tooling properly set up in the previous chapter, you can now start wit
|
|||
|
||||
Create a new folder for your project.
|
||||
|
||||
![[_resources/2_Initialize_Your_First_Project/4d6ba511be479a6749bc4baeb985aa54_MD5.jpg]]
|
||||
![4d6ba511be479a6749bc4baeb985aa54_MD5](_resources/2_Initialize_Your_First_Project/4d6ba511be479a6749bc4baeb985aa54_MD5.jpg)
|
||||
|
||||
And open the folder in your editor of choice, such as VSCode
|
||||
|
||||
![[_resources/2_Initialize_Your_First_Project/da331137b939a5faf4924ee8a21b3fca_MD5.jpg]]
|
||||
![da331137b939a5faf4924ee8a21b3fca_MD5](_resources/2_Initialize_Your_First_Project/da331137b939a5faf4924ee8a21b3fca_MD5.jpg)
|
||||
|
||||
Create a new Python virtual environment with Pipenv by running `pipenv shell`
|
||||
|
||||
![[_resources/2_Initialize_Your_First_Project/db56664f1d7fb30451131394845729c5_MD5.jpg]]
|
||||
![db56664f1d7fb30451131394845729c5_MD5](_resources/2_Initialize_Your_First_Project/db56664f1d7fb30451131394845729c5_MD5.jpg)
|
||||
|
||||
This will create a Pipfile in your project folder. This will track what packages you install in your virtual environment
|
||||
|
||||
![[_resources/2_Initialize_Your_First_Project/3567fe21bc31ff29f392e112cd1e9308_MD5.jpg]]
|
||||
![3567fe21bc31ff29f392e112cd1e9308_MD5](_resources/2_Initialize_Your_First_Project/3567fe21bc31ff29f392e112cd1e9308_MD5.jpg)
|
||||
I highly suggest you install the [Python VSCode extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) at this step to help with your developer experience
|
||||
|
||||
![[_resources/2_Initialize_Your_First_Project/4997f4d3a648a6ab9f5ac19c8211374f_MD5.jpg]]
|
||||
![4997f4d3a648a6ab9f5ac19c8211374f_MD5](_resources/2_Initialize_Your_First_Project/4997f4d3a648a6ab9f5ac19c8211374f_MD5.jpg)
|
||||
### Initialize Git
|
||||
|
||||
When working on software projects such as Django, you will need to be able to track small changes. We'll be using Git for that
|
||||
|
||||
We previously installed Git, now's the time to use it. In your terminal, run the command `git init`
|
||||
|
||||
![[_resources/2_Initialize_Your_First_Project/765336c9701ec47364bcd428b580e1ce_MD5.jpg]]
|
||||
![765336c9701ec47364bcd428b580e1ce_MD5](_resources/2_Initialize_Your_First_Project/765336c9701ec47364bcd428b580e1ce_MD5.jpg)
|
||||
|
||||
If successful, this will create a hidden `.git` folder in your project
|
||||
|
||||
![[_resources/2_Initialize_Your_First_Project/f1c979f796fda6b41f83a37744b469e2_MD5.jpg]]
|
||||
![f1c979f796fda6b41f83a37744b469e2_MD5](_resources/2_Initialize_Your_First_Project/f1c979f796fda6b41f83a37744b469e2_MD5.jpg)
|
||||
|
||||
There are some files in your projects that won't need to be tracked for changes (database files, and passwords!)
|
||||
\
|
||||
Download the `gitignore` below and rename it to `.gitignore`. You can then drop it into your root project folder
|
||||
![[_resources/2_Initialize_Your_First_Project/gitignore]]
|
||||
![gitignore](_resources/2_Initialize_Your_First_Project/gitignore)
|
||||
|
||||
|
||||
![[_resources/2_Initialize_Your_First_Project/d5a36b65d64255b05bc81a1709ca1158_MD5.jpg]]
|
||||
![d5a36b65d64255b05bc81a1709ca1158_MD5](_resources/2_Initialize_Your_First_Project/d5a36b65d64255b05bc81a1709ca1158_MD5.jpg)
|
||||
|
||||
Don't forget to rename it to `.gitignore`. Take note of the dot before the filename!
|
||||
|
||||
|
@ -52,11 +52,11 @@ Since you're using a virtual environment such as Pipenv, you will need to specif
|
|||
|
||||
Press **CTRL + Shift + P** to open the VSCode menu and type in **Select Interpreter**.
|
||||
|
||||
![[_resources/2_Initialize_Your_First_Project/d4da2b316a92c4444c01272187355abd_MD5.jpg]]
|
||||
![d4da2b316a92c4444c01272187355abd_MD5](_resources/2_Initialize_Your_First_Project/d4da2b316a92c4444c01272187355abd_MD5.jpg)
|
||||
|
||||
Select the virtual Python environment you created earlier (eg. the one created from `pipenv shell`)
|
||||
|
||||
![[_resources/2_Initialize_Your_First_Project/9d0f73aa1a1929a62a482e23bf2e94b5_MD5.jpg]]
|
||||
![9d0f73aa1a1929a62a482e23bf2e94b5_MD5](_resources/2_Initialize_Your_First_Project/9d0f73aa1a1929a62a482e23bf2e94b5_MD5.jpg)
|
||||
|
||||
### Install Dependencies and Packages
|
||||
|
||||
|
@ -66,32 +66,32 @@ On your current project's code editor, open a terminal or command line and type
|
|||
|
||||
`pipenv install django djangorestframework`
|
||||
|
||||
![[_resources/2_Initialize_Your_First_Project/0cfa5562ea461edb202130a6f49a7cea_MD5.jpg]]
|
||||
![0cfa5562ea461edb202130a6f49a7cea_MD5](_resources/2_Initialize_Your_First_Project/0cfa5562ea461edb202130a6f49a7cea_MD5.jpg)
|
||||
|
||||
This will update your Pipfile and create a Pipfile.lock to reflect the installed packages
|
||||
|
||||
![[_resources/2_Initialize_Your_First_Project/bb5d783ca5d693451fc98425856492b9_MD5.jpg]]
|
||||
![bb5d783ca5d693451fc98425856492b9_MD5](_resources/2_Initialize_Your_First_Project/bb5d783ca5d693451fc98425856492b9_MD5.jpg)
|
||||
### Initialize the Project
|
||||
|
||||
In your code editor's terminal or command prompt, run the command
|
||||
`django-admin startproject PROJECT\_NAME`
|
||||
|
||||
![[_resources/2_Initialize_Your_First_Project/9fa2115388cde426285a656a7e9965d5_MD5.jpg]]](https://bookstack.06222001.xyz/uploads/images/gallery/2024-09/i5bimage.png)
|
||||
![9fa2115388cde426285a656a7e9965d5_MD5](_resources/2_Initialize_Your_First_Project/9fa2115388cde426285a656a7e9965d5_MD5.jpg)](https://bookstack.06222001.xyz/uploads/images/gallery/2024-09/i5bimage.png)
|
||||
|
||||
Be sure to replace it with your own project name! I named mine `djangobackend`.
|
||||
|
||||
A new directory will be created, containing your Django project's files
|
||||
|
||||
[![[_resources/2_Initialize_Your_First_Project/a137cf212011825e092997818ff20658_MD5.jpg]]
|
||||
[![a137cf212011825e092997818ff20658_MD5](_resources/2_Initialize_Your_First_Project/a137cf212011825e092997818ff20658_MD5.jpg)
|
||||
|
||||
Change your directory in the terminal to the folder just created eg. `cd PROJECT\_NAME`
|
||||
|
||||
![[_resources/2_Initialize_Your_First_Project/ab38fdac55d8d6331471d609707dfdb9_MD5.jpg]]
|
||||
![ab38fdac55d8d6331471d609707dfdb9_MD5](_resources/2_Initialize_Your_First_Project/ab38fdac55d8d6331471d609707dfdb9_MD5.jpg)
|
||||
|
||||
Once that's done, run the command `python manage.py runserver 0.0.0.0:8000` to start your Django project
|
||||
|
||||
![[_resources/2_Initialize_Your_First_Project/b281e8b8bd83d316d45c1d0e810ce0a1_MD5.jpg]]
|
||||
![b281e8b8bd83d316d45c1d0e810ce0a1_MD5](_resources/2_Initialize_Your_First_Project/b281e8b8bd83d316d45c1d0e810ce0a1_MD5.jpg)
|
||||
|
||||
Your Django project will now be visible on the URL **[https://localhost:8000](https://localhost:8000)**
|
||||
|
||||
![[_resources/2_Initialize_Your_First_Project/377f5134b0525b830116a64d7699368f_MD5.jpg]]
|
||||
![377f5134b0525b830116a64d7699368f_MD5](_resources/2_Initialize_Your_First_Project/377f5134b0525b830116a64d7699368f_MD5.jpg)
|
|
@ -9,7 +9,7 @@ Your project's root folder (the outer most) contains the following
|
|||
|
||||
#### Project Folder
|
||||
|
||||
![[_resources/3_Project_Structure/3a9acd103fc5ff4a63a7520951dc99b8_MD5.jpg]]
|
||||
![3a9acd103fc5ff4a63a7520951dc99b8_MD5](_resources/3_Project_Structure/3a9acd103fc5ff4a63a7520951dc99b8_MD5.jpg)
|
||||
|
||||
The folder immediately proceeding your first one contains your Django project, created by the command `django-admin startproject PROJECT\_NAME` which you previously executed
|
||||
|
||||
|
@ -26,7 +26,7 @@ Django divides your projects into groups called apps. When you start a new proje
|
|||
|
||||
In this case, my first app is named `djangobackend`
|
||||
|
||||
![[_resources/3_Project_Structure/1f40b8e177d08862762cffe20f01e38f_MD5.jpg]]
|
||||
![1f40b8e177d08862762cffe20f01e38f_MD5](_resources/3_Project_Structure/1f40b8e177d08862762cffe20f01e38f_MD5.jpg)
|
||||
|
||||
That seems a bit confusing doesn't it?
|
||||
|
||||
|
@ -36,35 +36,35 @@ To make things clearer, you can opt to rename the main app of your Django projec
|
|||
|
||||
- Rename the main app folder to `config`
|
||||
|
||||
![[_resources/3_Project_Structure/bd12032f298e0a3e9d7c12999b231b77_MD5.jpg]]
|
||||
![bd12032f298e0a3e9d7c12999b231b77_MD5](_resources/3_Project_Structure/bd12032f298e0a3e9d7c12999b231b77_MD5.jpg)
|
||||
|
||||
- Open `asgi.py` and change line 14 to point to **config.settings** instead of **PROJECT_NAME.settings**
|
||||
|
||||
![[_resources/3_Project_Structure/9a3064d2cb4d17b632358d10dfbdcc22_MD5.jpg]]
|
||||
![9a3064d2cb4d17b632358d10dfbdcc22_MD5](_resources/3_Project_Structure/9a3064d2cb4d17b632358d10dfbdcc22_MD5.jpg)
|
||||
|
||||
- Do the same for the `wsgi.py` file, also in line 14
|
||||
|
||||
![[_resources/3_Project_Structure/6846e765a92401a0768a60fab29151f9_MD5.jpg]]
|
||||
![6846e765a92401a0768a60fab29151f9_MD5](_resources/3_Project_Structure/6846e765a92401a0768a60fab29151f9_MD5.jpg)
|
||||
|
||||
- Go to `settings.py` and set **ROOT_URLCONF** in line 52 to **config.urls** instead of **PROJECT_NAME.urls**
|
||||
|
||||
![[_resources/3_Project_Structure/8a0ab212600d24de9fb70656e810e603_MD5.jpg]]
|
||||
![8a0ab212600d24de9fb70656e810e603_MD5](_resources/3_Project_Structure/8a0ab212600d24de9fb70656e810e603_MD5.jpg)
|
||||
|
||||
- Do the same for **WSGI_APPLICATION** in `settings.py`, line 70
|
||||
|
||||
![[_resources/3_Project_Structure/42ab80ce78c1158e8af47ddf9631c027_MD5.jpg]]
|
||||
![42ab80ce78c1158e8af47ddf9631c027_MD5](_resources/3_Project_Structure/42ab80ce78c1158e8af47ddf9631c027_MD5.jpg)
|
||||
|
||||
- In `manage.py`, change line 19 to point to **config.settings** instead of **PROJECT_NAME.settings**
|
||||
|
||||
[![[_resources/3_Project_Structure/29904325362d967d3e9fe79941ae3589_MD5.jpg]]
|
||||
[![29904325362d967d3e9fe79941ae3589_MD5](_resources/3_Project_Structure/29904325362d967d3e9fe79941ae3589_MD5.jpg)
|
||||
|
||||
With that, you should be good to go. Double check your setup by starting the Django app once again
|
||||
|
||||
![[_resources/3_Project_Structure/d85a379bdec6197afff6e37bd79359a2_MD5.jpg]]
|
||||
![d85a379bdec6197afff6e37bd79359a2_MD5](_resources/3_Project_Structure/d85a379bdec6197afff6e37bd79359a2_MD5.jpg)
|
||||
|
||||
If you followed everything correctly, you should see the same page as before
|
||||
|
||||
![[_resources/3_Project_Structure/9e9b9a07ac2b8066670d18d04a97627c_MD5.jpg]]
|
||||
![9e9b9a07ac2b8066670d18d04a97627c_MD5](_resources/3_Project_Structure/9e9b9a07ac2b8066670d18d04a97627c_MD5.jpg)
|
||||
|
||||
### Setting up DRF
|
||||
|
||||
|
@ -74,10 +74,10 @@ You've already installed Django REST Framework in the previous steps (e.g. `pipe
|
|||
|
||||
To register it as an app in our project. Open your `settings.py` file and look for the **INSTALLED_APPS** section
|
||||
|
||||
![[_resources/3_Project_Structure/5bc2f2801c226ade60ad52496486ff78_MD5.jpg]]
|
||||
![5bc2f2801c226ade60ad52496486ff78_MD5](_resources/3_Project_Structure/5bc2f2801c226ade60ad52496486ff78_MD5.jpg)
|
||||
|
||||
Add **rest_framework** to **INSTALLED_APPS**
|
||||
|
||||
![[_resources/3_Project_Structure/c327ae10edcfd245d0ad4cfe787bf9ae_MD5.jpg]]
|
||||
![c327ae10edcfd245d0ad4cfe787bf9ae_MD5](_resources/3_Project_Structure/c327ae10edcfd245d0ad4cfe787bf9ae_MD5.jpg)
|
||||
|
||||
The next section will then tackle building a REST API with DRF
|
|
@ -11,20 +11,20 @@ Examples of these include the following;
|
|||
|
||||
You should take this into account when building a new project, otherwise users on older versions may encounter errors.
|
||||
|
||||
![[_resources/4_REST_Framework_Setup/3b993b4fb30752dbd22c157c4c852f82_MD5.jpg]]
|
||||
![3b993b4fb30752dbd22c157c4c852f82_MD5](_resources/4_REST_Framework_Setup/3b993b4fb30752dbd22c157c4c852f82_MD5.jpg)
|
||||
|
||||
This lets you allow users on older versions to still use your app (v1) while allowing newer users without any overlap (v2)
|
||||
|
||||
We will structure your project in the same manner as above. Create a folder named `api` in your Django project folder besides **config**
|
||||
|
||||
![[_resources/4_REST_Framework_Setup/e4b384b960e31b256a53775aa6f61ff2_MD5.jpg]]
|
||||
![e4b384b960e31b256a53775aa6f61ff2_MD5](_resources/4_REST_Framework_Setup/e4b384b960e31b256a53775aa6f61ff2_MD5.jpg)
|
||||
|
||||
Create the following files inside the `api` folder
|
||||
|
||||
- `__init__.py`
|
||||
- `urls.py`
|
||||
|
||||
![[_resources/4_REST_Framework_Setup/8357583c43ac252db3811fcc35f21564_MD5.jpg]]
|
||||
![8357583c43ac252db3811fcc35f21564_MD5](_resources/4_REST_Framework_Setup/8357583c43ac252db3811fcc35f21564_MD5.jpg)
|
||||
Leave **\_\_init\_\_.py** blank. Instead, open **urls.py** and add the following code
|
||||
|
||||
```python
|
||||
|
@ -48,9 +48,9 @@ urlpatterns = [
|
|||
|
||||
Your setup should then look like this
|
||||
|
||||
[![[_resources/4_REST_Framework_Setup/8a1e35b34d95be992bd550b32bfe03fa_MD5.jpg]]](https://bookstack.06222001.xyz/uploads/images/gallery/2024-09/2gZimage.png)
|
||||
[![8a1e35b34d95be992bd550b32bfe03fa_MD5](_resources/4_REST_Framework_Setup/8a1e35b34d95be992bd550b32bfe03fa_MD5.jpg)](https://bookstack.06222001.xyz/uploads/images/gallery/2024-09/2gZimage.png)
|
||||
|
||||
[![[_resources/4_REST_Framework_Setup/f9a1c18b83ca805afb4662e6ba43eca2_MD5.jpg]]](https://bookstack.06222001.xyz/uploads/images/gallery/2024-09/70bimage.png)
|
||||
[![f9a1c18b83ca805afb4662e6ba43eca2_MD5](_resources/4_REST_Framework_Setup/f9a1c18b83ca805afb4662e6ba43eca2_MD5.jpg)](https://bookstack.06222001.xyz/uploads/images/gallery/2024-09/70bimage.png)
|
||||
|
||||
There is no need to include the **api** app in your **INSTALLED\_APPS (config/settings.py)**
|
||||
|
||||
|
|
|
@ -1,7 +1,4 @@
|
|||
Notes may appear broken when viewed in GitHub or Gitea due to GitHub Flavored Markdown (GHFM) limitations. Be sure to view the notes at [notes.06222001.xyz](https://notes.06222001.xyz) instead.
|
||||
#### Resource List
|
||||
- [[Django REST Framework Guide/1_Setup|Django REST Framework Setup]]
|
||||
- [Django REST Framework Setup](Django%20REST%20Framework%20Guide/1_Setup.md)
|
||||
|
||||
#### These resources are also accessible via
|
||||
- [Perlite Web Instance](https://notes.06222001.xyz)
|
||||
- [GitHub Repository](https://github.com/lemeow125/Notes)
|
||||
- [Gitea Repository Mirror](https://git.06222001.xyz/keannu125/Notes)
|
||||
|
|
Loading…
Reference in a new issue