<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Giuseppe</title>
    <description>The latest articles on DEV Community by Giuseppe (@giuseppewdev).</description>
    <link>https://dev.to/giuseppewdev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1055508%2Ffa877162-75ea-4627-b67b-26718b5b860a.png</url>
      <title>DEV Community: Giuseppe</title>
      <link>https://dev.to/giuseppewdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/giuseppewdev"/>
    <language>en</language>
    <item>
      <title>Django installation with PostgreSql, small guide for beginners 🎸</title>
      <dc:creator>Giuseppe</dc:creator>
      <pubDate>Wed, 12 Apr 2023 17:50:39 +0000</pubDate>
      <link>https://dev.to/giuseppewdev/django-installation-with-postgresql-small-guide-for-beginners-5fn4</link>
      <guid>https://dev.to/giuseppewdev/django-installation-with-postgresql-small-guide-for-beginners-5fn4</guid>
      <description>&lt;div class="ltag__user ltag__user__id__1055508"&gt;
    &lt;a href="/giuseppewdev" class="ltag__user__link profile-image-link"&gt;
      &lt;div class="ltag__user__pic"&gt;
        &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1055508%2Ffa877162-75ea-4627-b67b-26718b5b860a.png" alt="giuseppewdev image"&gt;
      &lt;/div&gt;
    &lt;/a&gt;
  &lt;div class="ltag__user__content"&gt;
    &lt;h2&gt;
&lt;a class="ltag__user__link" href="/giuseppewdev"&gt;Giuseppe&lt;/a&gt;Follow
&lt;/h2&gt;
    &lt;div class="ltag__user__summary"&gt;
      &lt;a class="ltag__user__link" href="/giuseppewdev"&gt;A software/web developer&lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;





&lt;p&gt;Hi guys 😊 I prepare for myself a small guide that I would like to share with you and I hope will be helful&lt;/p&gt;

&lt;p&gt;In this guide, you will find the basic info how to start a django project.&lt;/p&gt;

&lt;p&gt;So let's start ✋&lt;/p&gt;

&lt;h2 id="table"&gt;Table of Contents&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
Requirements &lt;/li&gt;
&lt;li&gt;Virtual Environment&lt;/li&gt;
&lt;li&gt;
Django &lt;/li&gt;
&lt;li&gt;
First project
&lt;/li&gt;
&lt;li&gt;PostgreSQL&lt;/li&gt;
&lt;li&gt;.env&lt;/li&gt;
&lt;li&gt;Run the server&lt;/li&gt;
&lt;li&gt;.gitignore&lt;/li&gt;
&lt;li&gt;Conclusion,Django app and more&lt;/li&gt;
&lt;/ol&gt;




&lt;h3 id="requirements"&gt;❗Requirements&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.python.org/downloads/" rel="noopener noreferrer"&gt;Python&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Visual Studio Code or any other editors &lt;a href="https://code.visualstudio.com/download" rel="noopener noreferrer"&gt;VSCode&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;Package installer for python &lt;a&gt;Pip&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I highly recommend to read the django documentation &lt;strong&gt;&lt;a href="https://docs.djangoproject.com/en/4.2/" rel="noopener noreferrer"&gt;Doc&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

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

&lt;p&gt;Back to the table of Contents&lt;/p&gt;




&lt;h3 id="enviroment"&gt;📖 Virtual Environment&lt;/h3&gt;

&lt;p&gt;We need to write our code in python, so we need to create a python virtual Environment.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;In our project folder, let's install the environment with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python &lt;span class="nt"&gt;-m&lt;/span&gt; venv &lt;span class="nb"&gt;env
env&lt;/span&gt;&lt;span class="se"&gt;\S&lt;/span&gt;cripts&lt;span class="se"&gt;\a&lt;/span&gt;ctivate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to the table of Contents&lt;/p&gt;




&lt;h3 id="django"&gt;⬇ Django installation&lt;/h3&gt;

&lt;p&gt;Then to work with Django we need to install it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;django

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to the table of Contents&lt;/p&gt;




&lt;h3 id="project"&gt;📂 Create our first project&lt;/h3&gt;  

&lt;p&gt;Now that we have the python environment and django installed, we can create our first project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;django-admin startproject mysite
python manage.py check
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point our folder tree looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;env/
mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to the table of Contents&lt;/p&gt;




&lt;h3 id="db"&gt;🗄 Install PostgreSQL (optional)&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Psycopg is the most popular PostgreSQL database adapter for the Python programming language. &lt;/p&gt;

&lt;p&gt;Install &lt;a href="https://pypi.org/project/psycopg2/" rel="noopener noreferrer"&gt;psycopg2&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;django psycopg2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;a href="https://pypi.org/project/python-decouple/#:~:text=Python%20Decouple%3A%20Strict%20separation%20of%20settings%20from%20code&amp;amp;text=store%20parameters%20in%20ini%20or,to%20rule%20all%20your%20instances" rel="noopener noreferrer"&gt;Decouple&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;python-decouple 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After we install decouple, we haft to change our database information inside mysite/mysite settings.py&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;decouple&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt; 

&lt;span class="n"&gt;SECRET_KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;SECRET_KEY&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="n"&gt;DEBUG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;DEBUG&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cast&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="n"&gt;DATABASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;default&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ENGINE&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.db.backends.postgresql&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;NAME&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;DB_NAME&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; 
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;USER&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;DB_USER&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;PASSWORD&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;DB_PASSWORD&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;HOST&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;DB_HOST&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;PORT&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;80&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;  

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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&lt;/p&gt;

&lt;p&gt;Back to the table of Contents&lt;/p&gt;




&lt;h3 id="env"&gt;.env&lt;/h3&gt;
 

&lt;p&gt;.env it is just a file that keep secret and safe information such as credentials , access keys and tokens to services used by programs.&lt;/p&gt;

&lt;p&gt;So let's create an .env file at the same level of manage.pyt in mysite (mysite/.env).&lt;/p&gt;

&lt;p&gt;In my case I will store all information about the database  connection. &lt;/p&gt;

&lt;p&gt;No need to import this info inside settings, decouple will do for us.&lt;/p&gt;

&lt;p&gt;example variable inside mysite/.env&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="n"&gt;SECRET_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;
&lt;span class="n"&gt;DEBUG&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="n"&gt;DB_NAME&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; 
&lt;span class="n"&gt;DB_USER&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;
&lt;span class="n"&gt;DB_PASSWORD&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;
&lt;span class="n"&gt;DB_HOST&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;
&lt;span class="n"&gt;DB_PORT&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To fully create and save our database we haft to run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py makemigrations
python manage.py migrate

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to the table of Contents&lt;/p&gt;




&lt;h3 id="server"&gt;📡 Run the server&lt;/h3&gt;

&lt;p&gt;Once we have all in place, to verify if the Django project works correctly, we need to type the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py runserver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;if everything is correct we should get a page that looks more or less like this one:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnecyashmye0qsouxgwyo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnecyashmye0qsouxgwyo.png" alt="django"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Back to the table of Contents&lt;/p&gt;




&lt;h3 id="git"&gt;.gitignore file&lt;/h3&gt;

&lt;p&gt;Before continue, I would like to suggest to create a .gitignore file.&lt;/p&gt;

&lt;p&gt;If someone is using git in development, it is crucial to not push some file or information on deployment.&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*.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

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back to the table of Contents&lt;/p&gt;




&lt;h3 id="con"&gt;Conclusion,Django app and more&lt;/h3&gt;

&lt;p&gt;For the end of this guide, I would highlight that the magic of django it is happening after the creation of the django app&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python manage.py startapp app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this commands will create a new folder app/&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;br&gt;
There are many online courses along with Django documentation,&lt;br&gt;
that go deep into the concept.&lt;/p&gt;

&lt;p&gt;Sometimes things get repeated so I prepare for myself this small tutorial and I thought would be helpful also for others. &lt;br&gt;
I hope you find it useful.🥳.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>django</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
