Hi guys π I prepare for myself a small guide that I would like to share with you and I hope will be helful
In this guide, you will find the basic info how to start a django project.
So let's start β
Table of Contents
- Requirements
- Virtual Environment
- Django
- First project
- PostgreSQL
- .env
- Run the server
- .gitignore
- Conclusion,Django app and more
βRequirements
I highly recommend to read the django documentation Doc.
Keep in mind that I'm on Windows and I will be using powershell inside Visual Studio Code to execute all the commands.
π Virtual Environment
We need to write our code in python, so we need to create a python virtual Environment.
Virtualenv is used to manage Python packages for different projects and allows you to avoid installing Python packages globally which could break system tools or other projects.
In our project folder, let's install the environment with the following command:
python -m venv env
env\Scripts\activate
β¬ Django installation
Then to work with Django we need to install it.
pip install django
π Create our first project
Now that we have the python environment and django installed, we can create our first project.
django-admin startproject mysite
python manage.py check
At this point our folder tree looks like this:
env/
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
π Install PostgreSQL (optional)
In our project we need a database, by default Django use Sqlite, but you can use a different database. In this case I will be using Postgresql and to do so we need to install Psycopg.
Psycopg is the most popular PostgreSQL database adapter for the Python programming language.
Install psycopg2
pip install django psycopg2
Now that we have the database it is important to organize our setting. We can do it with the help of Decouple so that we can change parameters without having to redeploy our app.Decouple
pip install python-decouple
After we install decouple, we haft to change our database information inside mysite/mysite settings.py
from decouple import config
SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG',default=True, cast=bool)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': config('DB_NAME'),
'USER': config('DB_USER'),
'PASSWORD': config('DB_PASSWORD'),
'HOST': config('DB_HOST'),
'PORT': '80',
}
}
Once we done with the settings, we need to connect to our database, but we dont want to keep our information public, that why we need a safe place to store our info with the help of .env
.env
.env it is just a file that keep secret and safe information such as credentials , access keys and tokens to services used by programs.
So let's create an .env file at the same level of manage.pyt in mysite (mysite/.env).
In my case I will store all information about the database connection.
No need to import this info inside settings, decouple will do for us.
example variable inside mysite/.env
SECRET_KEY=
DEBUG=True
DB_NAME=
DB_USER=
DB_PASSWORD=
DB_HOST=
DB_PORT=
To fully create and save our database we haft to run the following commands:
python manage.py makemigrations
python manage.py migrate
π‘ Run the server
Once we have all in place, to verify if the Django project works correctly, we need to type the following command:
python manage.py runserver
if everything is correct we should get a page that looks more or less like this one:
.gitignore file
Before continue, I would like to suggest to create a .gitignore file.
If someone is using git in development, it is crucial to not push some file or information on deployment.
In our project folder, at the same level of manage.py create the file .gitignore (mysite/.gitignore) and then save all the follow information inside the file.
*.log
*.pot
*.pyc
__pycache__
db.sqlite3
media
*.bak
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
.idea/**/aws.xml
.idea/**/contentModel.xml
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
.idea/**/gradle.xml
.idea/**/libraries
*.iws
out/
atlassian-ide-plugin.xml
*.py[cod]
*$py.class
.Python build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
*.manifest
*.spec
pip-log.txt
pip-delete-this-directory.txt
htmlcov/
.tox/
.coverage
.coverage.*
.cache
.pytest_cache/
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.ipynb_checkpoints
.python-version
celerybeat-schedule.*
*.sage.py
*.env
.env
.venv
/env
env/
venv/
ENV/
env.bak/
venv.bak/
/site
.mypy_cache/
*.tmlanguage.cache
*.tmPreferences.cache
*.stTheme.cache
*.sublime-workspace
*.sublime-project
sftp-config.json
Control.last-run
Control.ca-list
Control.ca-bundle
Control.system-ca-bundle
GitHub.sublime-settings
.vscode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history
Conclusion,Django app and more
For the end of this guide, I would highlight that the magic of django it is happening after the creation of the django app
python manage.py startapp app
this commands will create a new folder app/
env/
mysite/
manage.py
.gitignore
.env
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
app/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
Here will be completly a new world with a lot of new things to explore and learn , concept such as model , forms , class base view and much more.
There are many online courses along with Django documentation,
that go deep into the concept.
Sometimes things get repeated so I prepare for myself this small tutorial and I thought would be helpful also for others.
I hope you find it useful.π₯³.
Top comments (0)