Django installation with PostgreSql, small guide for beginners 🎸
Tags: beginners, django, webdev, tutorial
Published: 2023-04-12 17:50:39



{% user giuseppewdev %}

---


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 ✋


<h2 id="table">Table of Contents</h2>


1. <a href="#requirements">Requirements</a> 
2. <a href="#enviroment">Virtual Environment</a>
3. <a href="#django">Django</a> 
4. <a href="#project">First project</a>  
5. <a href="#db">PostgreSQL</a>
6. <a href="#env">.env</a>
7. <a href="#server">Run the server</a>
8. <a href="#git">.gitignore</a>
9. <a href="#con">Conclusion,Django app and more</a>

---


<h3 id="requirements">❗Requirements</h3>

- [Python](https://www.python.org/downloads/)
- Visual Studio Code or any other editors [VSCode](https://code.visualstudio.com/download) 
- Package installer for python [Pip](Python https://pypi.org/project/pip/)

I highly recommend to read the django documentation **[Doc](https://docs.djangoproject.com/en/4.2/)**.



Keep in mind that I'm on Windows and I will be using powershell inside Visual Studio Code to execute all the commands.

<a href="#table">Back to the table of Contents</a>

---

<h3 id="enviroment" >📖 Virtual Environment</h3>

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: 

```shell
python -m venv env
env\Scripts\activate
```


<a href="#table">Back to the table of Contents</a>

---

<h3 id="django">⬇ Django installation</h3>

Then to work with Django we need to install it. 

```shell
pip install django

```



<a href="#table">Back to the table of Contents</a>

---


<h3 id="project">📂 Create our first project</h3>  

Now that we have the python environment and django installed, we can create our first project.

```shell
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
```




<a href="#table">Back to the table of Contents</a>

---


<h3 id="db">🗄 Install PostgreSQL (optional)</h3>

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](https://pypi.org/project/psycopg2/)

```shell
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](https://pypi.org/project/python-decouple/#:~:text=Python%20Decouple%3A%20Strict%20separation%20of%20settings%20from%20code&text=store%20parameters%20in%20ini%20or,to%20rule%20all%20your%20instances)

```shell
pip install python-decouple 
```

After we install decouple, we haft to change our database information inside mysite/mysite settings.py

```python

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 <a href="#env">.env</a>




<a href="#table">Back to the table of Contents</a>

---

<h3 id="env">.env</h3>
 
.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

```python

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:


```shell
python manage.py makemigrations
python manage.py migrate

```







<a href="#table">Back to the table of Contents</a>

---

<h3 id="server">📡 Run the server</h3>

Once we have all in place, to verify if the Django project works correctly, we need to type the following command:

```shell
python manage.py runserver
```

if everything is correct we should get a page that looks more or less like this one:

![django](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/necyashmye0qsouxgwyo.png)




<a href="#table">Back to the table of Contents</a>

---


<h3 id="git">.gitignore file</h3>

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

```


<a href="#table">Back to the table of Contents</a>

---


<h3 id="con">Conclusion,Django app and more</h3>

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.🥳.