Coding Journal

My journey to become a Full Stack Developer

cover image

Django CRUD project tutorial

Nov. 27, 2020, 8:53 p.m.

In this series of blog articles, I want to describe how to create a simple CRUD Django project similar to My diary app: https://diary-by-makneta.herokuapp.com/. I'm going to describe the process of initializing a Django project, creating a user profile, public and private diary entries. I will also write about deploying such an app to Heroku.

 

## Part1: How to start a Django project.

 

*Content:*

1. [Step1: Instal or check Python version](#step-1-install-or-check-python-version)

2. [Step 2: Make sure you have pip installed](#step-2-make-sure-you-have-pip-installed)

3. [Step 3: Create a virtual environment](#step-3-create-a-virtual-environment)

4. [Step 4: Let's install Django](#step-4-lets-install-django)

5. [Step 5: Start a Django project](#step-5-start-a-django-project)

6. [Step 6: Run the project now](#step-6-run-the-project-now)

7. [Step 7: Add small changes in website/settings.py](#step-7-add-small-changes-in-websitesettingspy)


 

In this part of the tutorial, we will be using a terminal a lot. Make sure that you know where to find the command line/terminal in your computer. If you are on Linux pressing ctrl + alt + t should open the terminal for you. Other than that, please check before starting the project.

 

If you are using a code editor like VS Code or Pycharm, you can also find the terminal there.

 

Django is a server-side web framework written in Python, so if we want to write our first project in this framework, we need to have Python installed first.

 

## Step 1: Install or check **Python** version

 

In this project, we are using Python 3.8 version. You can check it by typing in your command line:

```python --version```

 

Some Operations Systems have Python installed already, but if you are unsure or you need to upgrade to a newer version, please read this great Real Python article. It covers Python installation on different OS. https://realpython.com/installing-python/

 

## Step 2: Make sure you have **pip** installed

 

We also need **pip**. It is the standard package manager for Python. It means that we need it to install any Python library. We can easily check the version of pip in our command line typing:

```pip --version```

If you don't have pip, please read this part of pip documentation: https://pip.pypa.io/en/stable/installing/

 

My pip version at the moment of writing this article is 20.2.4, and it's already up to date. If you would like to upgrade your **pip**, please type:

```pip install -U pip```

 

Now when we have Python and pip ready, we can start our Django project.

 

A good practice is to start every Python project within a virtual environment. The virtual environment is like a box in which we can put our project with all libraries we need. This way we can have a few projects with a different version of Django each.

 

There are several different virtual environments available but we are using **pipenv**.

Pipenv is not only a virtualenv but also a package tool. In a project with pipenv we will be using pipenv instead of pip to install new libraries.

 

## Step 3: Create a virtual environment

 

First, we need to install **pipenv**.

```pip3 install --user pipenv```

 

Now let's create a folder for our project. I called my folder crud-tut.

```mkdir crud-tut ```

and go into this folder

```cd crud-tut```

 

We've already installed pipenv and now we can use it to create a virtual environment for our project. As I wrote it before, I'm using Python 3.8 for this project so now, I'm telling the pipenv what Python version it should be using

 

```pipenv --python 3.8```

 

This will create a virtualenv for this project as you can see in the terminal

 

![pipenv start](readme-images/pipenv-start.png)

 

Now we can see that there is a new file in our folder - Pipfile. It was generated automatically. It will contain the record of all libraries used in the project.

 

Now we can activate the virtual environment:

```pipenv shell```

 

![pipenv-shell](readme-images/pipenv-shell.png)

 

There should be the name of the folder in brackets at the beginning of the line in our terminal (crud-tut). If we want to stop using the virtual environment it is enough to type ```exit``` in the command line.

 

> We need to remember to activate the virtual environment every time we start working on our project. If we don't activate it, we can be surprised that some dependencies don't work.

 

Every time if we install a new library/package using ```pipenv install <package name>```, a new dependency will occur in the Pipfile. And after the first ```pipenv install <package name>``` a new file should be generated: Pipfile.lock. The Pipfile.lock contains the exact versions of packages as well as hashes to support more secure verification. This file should not be changed manually. If missing, it should be generated with the ```pipenv lock``` command.

 

Both Pipfile and Pipfile.lock should be added to git. But we will take care about it in our later parts of that tutorial.

 

## Step 4: Let's install Django

 

If we want to install the current version of Django it is enough to write:

```pipenv install django```

 

and we will have the latest Django 3 version (in November 2020)

 

We can also install the specific version like this:

```pipenv install "django=1.11"```

(it will install the latest of Django 1.11 version)

or

```pipenv install "django<3.0"```

(it will install the latest version lower than 3.0)

 

If we want to check what version of Django we have just installed, we should type:

```django-admin --version```

in our terminal.

 

Now we are ready to start our project.

 

## Step 5: Start a Django project

 

First, we start a project, and then we can create apps. In Django, the **project** is the entire application and all its parts. Each part of the project that can be separated from the project should be created as a separate **app**. In our case we will create one project called **website** and two apps called: **users** and **diary**.

 

We've already seen that by installing Django we also installed a new command: ```django-admin```. Now we will use this command to create our project. Let's type in the command line:

 

```django-admin startproject website .```

 

After this command, our folders' structure should look like this (you shouldn't have README.md file at the moment because you haven't been initializing Git yet)

 

![folder-tree](readme-images/tree.png)

 

**website** is the name of our project and don't forget about adding the dot in the end. We do it to avoid nested folders. Now, we have two more elements in our crud-tut folder. One of them is manage.py file. From now on, every time we want to run any command connected with our project we need to make sure we are in the folder that contains manage.py file. This is our tool to execute Django-specific tasks. We will be using it while creating new apps, running the local server, or sending models to the database, and so on.

 

But we are more interested in our website folder. It contains a few files. But the 2 most important for us are urls.py and settings.py.

 

In each file, we already have some code that initializes our project. **website/urls.py** file is the place where we store the URL Configuration for our website. Each page on the internet needs its own address - its url. At the moment we have an url of admin site of our project in website/urls.py and after creating new apps, we will be adding the "address" of the app to our main urls.py file.

 

Settings.py has a lot of useful stuff stored for us. While our project is growing we will be adding new settings or changing the current a bit.


 

## Step 6: Run the project now

 

So far we haven't written a single line of code but we can run the local server to see our project.

Let's write in terminal

 

```python manage.py runserver```

 

and press enter. Our local server should be actived and we should see some warnings in our terminal. It says that we didn't apply migrations and our project can work unproperly. From that warning, we can read that there are some built-in apps: admin, auth(for authentication), contenttype, and sessions.

But at the moment we don't want to apply any migrations. We won't be doing it until we create our Custom User model.

 

![runserver](readme-images/runserver.png)

 

But we can also see that our development (local) server is running at http://127.0.0.1:8000/ . We can hold Ctrl key while pressing the link with our mouse or we can type 127.0.0.1:8000 in the browser tab to see our app running.

 

![first-django](readme-images/first-django.png)

 

If you look carefully at your crud-tut folder you will see a new file: db.sqlite3. It is a small database that is prepared for us and can be used mostly locally.


 

## Step 7: Add small changes in website/settings.py

 

At the moment we could do 2 things: If we want we can change the language of the project. For example, if I would like to create a project in my native language Polish, I can go to line 100 of **settings.py**: ```LANGUAGE_CODE = 'en-us'``` and change the language to **pl** ```LANGUAGE_CODE = 'pl'```. If I save it, the server will restart itself and I can see that Django's welcoming page is in Polish.

 

I can also change line 102 - time zone to ```TIME_ZONE = "Europe/Warsaw""```. We need to use timezone by continent and capital city.

If you need help with timezones, you can check this Wikipedia's List: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

 

### In this part we:

1. Checked Python's version

2. Checked and upgraded pip's version

3. Installed and activated pipenv -- a virutal environment and a package manager in one tool

4. Installed Django

5. Started our Django project

6. Changed language and timezone in our project

 

### Commands used in the terminal

: ```python --version```

: ```pip install -U pip```

: ```pip3 install --user pipenv```

: ```mkdir crud-tut ```

: ```cd crud-tut```

: ```pipenv --python 3.8```

: ```pipenv shell```

: ```pipenv install django```

: ```django-admin --version```

: ```django-admin startproject website .```

: ```python manage.py runserver```


 

### Next time we will create Users app and start building a User's dashboard.

Tagged in : tutorial

my photo

written by

Makneta

Avid learner, Python / Django and CSS Art ethusiast.

Similar posts

All posts