vault backup: 2024-10-14 13:30:54

This commit is contained in:
Keannu Bernasol 2024-10-14 13:30:54 +08:00
parent 137148a722
commit ab4bb45313
11 changed files with 243 additions and 19 deletions

View file

@ -20,7 +20,7 @@ You will need to install the latest version of Python (version 3.11.2 as of 2/19
[<span style="margin: 0px; padding: 0px; border: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 16px; vertical-align: baseline; outline: 0px;">https://www.python.org/downloads/</span>](https://www.python.org/downloads/ "https://www.python.org/downloads/")
<span style="margin: 0px; padding: 0px; border: 0px; font-weight: inherit; font-style: inherit; font-family: inherit; font-size: 16px; vertical-align: baseline; outline: 0px;">During the installation, make sure to check **Add Python 3.x to PATH**</span>
During the installation, make sure to check **Add Python 3.x to PATH**
![[_resources/1_Setup/373ea207d1abd6f0a734506d8ab4acbc_MD5.jpg]]

View file

@ -30,19 +30,21 @@ We previously installed Git, now's the time to use it. In your terminal, run the
![[_resources/2_Initialize_Your_First_Project/765336c9701ec47364bcd428b580e1ce_MD5.jpg]]
If successful, this will create a hidden .git folder in your project
If successful, this will create a hidden `.git` folder in your project
![[_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]]
Download the gitignore file [here](https://fileserver.06222001.xyz/files/Bookstack/gitignore) below and drop it into your project's folder
![[_resources/2_Initialize_Your_First_Project/d5a36b65d64255b05bc81a1709ca1158_MD5.jpg]]
Make sure to rename this to **.gitignore** (take note of the dot before the filename!)
Don't forget to rename it to `.gitignore`. Take note of the dot before the filename!
You can also choose to search for your own .gitignore template online for Django, whichever suits best for you.
You can also choose to search for your own `.gitignore` template online for Django or Python which will work fine too.
### Select your Interpreter
@ -52,7 +54,7 @@ Press **CTRL + Shift + P** to open the VSCode menu and type in **Select Interpre
![[_resources/2_Initialize_Your_First_Project/d4da2b316a92c4444c01272187355abd_MD5.jpg]]
Select the virtual Python environment you created earlier (eg. the one created from **pipenv shell)**
Select the virtual Python environment you created earlier (eg. the one created from `pipenv shell`)
![[_resources/2_Initialize_Your_First_Project/9d0f73aa1a1929a62a482e23bf2e94b5_MD5.jpg]]
@ -76,13 +78,13 @@ In your code editor's terminal or command prompt, run the command
![[_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**.
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]]
Change your directory in the terminal to the folder just created eg. **cd PROJECT\_NAME**
Change your directory in the terminal to the folder just created eg. `cd PROJECT\_NAME`
![[_resources/2_Initialize_Your_First_Project/ab38fdac55d8d6331471d609707dfdb9_MD5.jpg]]

View file

@ -15,16 +15,16 @@ The folder immediately proceeding your first one contains your Django project, c
This contains the following
- `manage.py` - the entry point to your Django app (e.g. the `python manage.py runserver` command and such)
- `db.sqlite3` - the default database file for Django, which uses [SQLite](https://www.sqlite.org/) (you can switch to PostgreSQL or MySQL later on)
- another folder which contains your project's main config or app
- `manage.py` - The entry point to your Django app (e.g. the `python manage.py runserver` command and such)
- `db.sqlite3` - The default database file for Django, which uses [SQLite](https://www.sqlite.org/) (you can switch to PostgreSQL or MySQL later on)
- Another folder which contains your project's main config or app
#### Django Apps
Django divides your projects into groups called apps. When you start a new project, you start off with your first app.
In this case, my first app is named **djangobackend**
In this case, my first app is named `djangobackend`
![[_resources/3_Project_Structure/1f40b8e177d08862762cffe20f01e38f_MD5.jpg]]
@ -34,11 +34,11 @@ That seems a bit confusing doesn't it?
To make things clearer, you can opt to rename the main app of your Django project. These are the changes you must make
- Rename the main app folder to **config**
- Rename the main app folder to `config`
![[_resources/3_Project_Structure/bd12032f298e0a3e9d7c12999b231b77_MD5.jpg]]
- Open `asgi.py` and change line 14 to point to **config.settings** instead of **PROJECT\_NAME.settings**
- Open `asgi.py` and change line 14 to point to **config.settings** instead of **PROJECT_NAME.settings**
![[_resources/3_Project_Structure/9a3064d2cb4d17b632358d10dfbdcc22_MD5.jpg]]
@ -46,15 +46,15 @@ To make things clearer, you can opt to rename the main app of your Django projec
![[_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**
- 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]]
- Do the same for **WSGI\_APPLICATION** in `settings.py`, line 70
- Do the same for **WSGI_APPLICATION** in `settings.py`, line 70
![[_resources/3_Project_Structure/42ab80ce78c1158e8af47ddf9631c027_MD5.jpg]]
- In `manage.py`, change line 19 to point to **config.settings** instead of **PROJECT\_NAME.settings**
- In `manage.py`, change line 19 to point to **config.settings** instead of **PROJECT_NAME.settings**
[![[_resources/3_Project_Structure/29904325362d967d3e9fe79941ae3589_MD5.jpg]]
@ -72,11 +72,11 @@ Your current setup only includes Django. To proceed with Django REST Framework,
You've already installed Django REST Framework in the previous steps (e.g. `pipenv install django djangorestframework`).
To register it as an app in our project. Open your **settings.py** file and look for the **INSTALLED\_APPS** section
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]]
Add **rest\_framework** to **INSTALLED\_APPS**
Add **rest_framework** to **INSTALLED_APPS**
![[_resources/3_Project_Structure/c327ae10edcfd245d0ad4cfe787bf9ae_MD5.jpg]]

View file

@ -0,0 +1,57 @@
# REST Framework Setup
### API App
When writing backend APIs, you make changes to your app that may not be compatible with older versions.
Examples of these include the following;
- You changed what should be sent to your login API in order to authenticate with a newer version of your backend
- When requesting books from your database, a newer version of your API may return a different format (XML instead of JSON)
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]]
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]]
Create the following files inside the `api` folder
- `__init__.py`
- `urls.py`
![[_resources/4_REST_Framework_Setup/8357583c43ac252db3811fcc35f21564_MD5.jpg]]
Leave **\_\_init\_\_.py** blank. Instead, open **urls.py** and add the following code
```python
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
```
We will then need to point the main config's url file **(url.py)** to the one we just created. Open the **urls.py** file in the **config** folder, replace it with the following
```python
from django.urls import path, include
urlpatterns = [
path('api/v1/', include('api.urls')),
]
```
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)
[![[_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)**
The next section will handle users and authentication

View file

@ -0,0 +1,162 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
static/
media/
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

Binary file not shown.

After

Width:  |  Height:  |  Size: 252 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 449 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

View file

@ -4,3 +4,6 @@
- [Perlite Web Instance](https://notes.06222001.xyz)
- [GitHub Repository](https://github.com/lemeow125/Notes)
- [Gitea Repository Mirror](https://git.06222001.xyz/keannu125/Notes)
### Notes
- [[Django REST Framework Guide/1_Setup|Django REST Framework Setup]]