Skip to content

Instantly share code, notes, and snippets.

@openhubdev
Last active March 9, 2021 18:42
Show Gist options
  • Save openhubdev/6bae43aa06b1bfb166dffa0a97a07444 to your computer and use it in GitHub Desktop.
Save openhubdev/6bae43aa06b1bfb166dffa0a97a07444 to your computer and use it in GitHub Desktop.

🐝 django 101

make sure you have python >= 3.3 installed

0. Create a project folder (= the project root)

1. create a virtual environment

python3 -m venv venv

2. Activate the virtual environment

source venv/bin/activate

If you have multiple versions of Python, now if you type > python -V: it will show the version (python3 in this case) in this virtual environment.

3. install Django

> pip install django

you can add a specific version as well like pip install Django==3.1.7 try > pip freeze: will display what you have installed so far

4. create a django project

django-admin startproject myproject

This creates a "myproject" within a new "myproject" folder; you can change the name of the outer "myproject" folder however you would like; the inner "myproject" folder is your Django "project" that contains "settings.py"

Basic folder structure

β”œβ”€β”€ /venv
β”œβ”€β”€ manage.py  -> important file to "run" python
β”œβ”€β”€ /myproject  -> your Django project
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ asgi.py
β”‚   β”œβ”€β”€ settings.py -> important file to set all configs in your Django project
β”‚   β”œβ”€β”€ urls.py
β”‚   β”œβ”€β”€ wsgi.py

(project root)
β”œβ”€β”€ /venv
β”œβ”€β”€ /myproject  -> your Django project "conatainer" (you can rename as you wish)
β”‚   β”œβ”€β”€ manage.py  -> important file to "run" python
β”‚   β”œβ”€β”€ /myproject  -> your Django project
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€  asgi.py
β”‚   β”‚   β”œβ”€β”€  settings.py -> **important file to set all configs in your Django project
β”‚   β”‚   β”œβ”€β”€  urls.py -> **important file to config URL paths in your Django project
β”‚   β”‚   β”œβ”€β”€  wsgi.py
β”‚ ...
β”‚ # -- other django apps get created in this level
β”‚   β”œβ”€β”€ /other-app01  -> "other django app 01" folder ..
β”‚   β”œβ”€β”€ /other-app02  -> "other django app 02" folder ..
...

5. Run server, after cd into the django project where the "manage.py" resides

cd myproject
python manage.py runserver

✨ If success, you will be able to load http://127.0.0.1:8000/ or http://localhost:8000/!!!


to stop the server: CTRL+C

to EXIT venv

deactivate

Create a superuser for the django admin

python manage.py createsuperuser

Now you can login to the admin site @ http://127.0.0.1:8000/admin or http://localhost:8000/admin


TO RESUME AN EXISTING DJANGO PROJ

Just run these 2 commands:

source venv/bin/activate

in the project root to activate the virtual env

python manage.py runserver

cd into the django project container -> then run python manage.py runserver


OTHERS

Set your Postgre DB instead of SqLite:

# @your-django-proj/settings.py

DATABASES = {
    'default': {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "<your-postgresql-db-name>",
        "USER": "<your-postgresql-username>",
        "PASSWORD": "<your-postgresql-password>",
    }
}

πŸ‘ Set the 'requirements.txt'

pip freeze > requirements.txt

all installed Django pkgs will be listed into a new text file called "requirements.txt"

πŸ’ͺ To install an existing DJANGO PROJ: run the "requirements.txt"

pip install -r requirements.txt

option: -r, --requirement : Install from the given requirements file. This option can be used multiple times.

or pip install -r requirements.txt --no-index --find-links

HAPPY LEARNING! πŸ‘


DJANGO Guide https://docs.djangoproject.com/en/3.1/

Django at a glance https://docs.djangoproject.com/en/3.1/intro/overview/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment