Create a simple Django app from scratch with PyCharm IDE and use a GitHub repository
Tags: django
Published: 2024-04-24 10:50:14

## **Introduction:**
Sometimes I need to remind myself, how to create a simple Django app from scratch, because that happens not every day. In this article I will focus on how to create the Django application with PyCharm IDE. 

<hr>

## **Prerequisites:**
For these steps you will need the PyCharm IDE on your machine (an other IDE is fine, too) and Python 3 should be already on your machine. A GitHub account and a GitHub repository to clone into the IDE PyCharm. 

<hr>

## **Setting up PyCharm:**
There are several ways to setting up PyCharm for a Django application. I will explain the process to clone a GitHub repsoitory in PyCharm, setting up an virtual environment and installing dependencies.

<hr>

### **1) clone a GitHub repsoitory in PyCharm:**
You copy the GitHub link of your repository which you want to clone into PyCharm.

![GitHub repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kvwp09ccj0p7if4tk2r3.png)


When PyCharm is open, you navigate on the main menu to 'Git' and choose 'Clone...'

![PyCharm Git](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wq9bmoiyerxytosbssx4.png)


You copy your repository link into the 'URL' line and press 'Clone'

![PyCharm Clone](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/byehscjyej84lpo3hntn.png)


You 'Trust Project' and open the project 'In This Window'.

![Trust Project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/td1xdymigpya2sn7u5qc.png)


<hr>

### **2) create a virtual environment**
After the project was cloned and loaded, you will see a yellowish message on the top of your PyCharm IDE.

![Python Interpreter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hv201lwxx71hak4o68gi.png)


Click on the blue link: 'Configure Python Interpreter' to set a virtual environment for this project. You choose 'Add New Interpreter' and 'Add Local Interpreter...'

![Python Interpreter add](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iwan5q07mmosu1ivy94u.png)


On the next window you can choose between different virtual environments ('Virtual Environment', 'Conda Environment', 'System Interpreter', 'Pipenv Environment' and 'Poetry Environment'). I have chosen the 'Virtual Environment' and the name of the virtual environment is: 'venv'.

![Python Interpreter save](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oy026ll71gxq3t5tj6qc.png)


A new folder named 'venv' is now in your project folder.

![Project Tree venv](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cbdzvr12nnqvfj4n0ukf.png)


And if you open a terminal, you can see the `(venv)` is activated.

![Terminal venv](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lwdhzhmzr5781hm9e5qy.png)


<hr>

### **3) install Django**
Now your virtual environment should be up and running. Now you can use `pip install django` in your terminal. Save the installed dependency in a file called 'requirements.txt'. You can do that with a simple command: `pip freeze > requirements.txt`. With the command: `pip freeze` your terminal displays all installed packages, dependencies.
After the command: `pip freeze > requirements.txt` you will find a new file named 'requirements.txt' in your root directory.

![Pip Freeze](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ebzm0o3t6oz622pm4mcl.png)


<hr>

### **4) Create the Django application:**
You go back to the terminal and type: `django-admin startproject project_name .`. 

_'project_name': 'project_name' can be set as you want, you choose the name of the folder and of the project_.
_The '.' after the 'project_name': The dot skips the top-level project folder and creates your management app and the manage.py file right inside your current working directory._
(Source: [RealPython Set up a Django app](https://realpython.com/django-setup/#set-up-a-django-project))


This command will create the Django app in your root directory.

Your project tree should be like this:

```python
project_name/
│
├── project_name/
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
│
└── venv
└── main.py
└── manage.py
└── requirements.txt
```


The next step is to create the application(s). You need to be in the folder where the 'manage.py' file is located. 

![Project Tree Manage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmuv4mdqsa5wp06y4oul.png)

You can check with `ls` as command in terminal which folders and files are currently in your directory.
To create an application you use: `python manage.py startapp my_app`.


After this command, you will see an additional folder named: 'my_app' in your root directory and your project tree looks like:

```python
project_name/
│
├── my_app/
│   ├── migrations/
│   │   └── __init__.py
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
│
├── project_name/
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
│
└── venv
└── main.py
└── manage.py
└── requirements.txt
```

To finalize the process you will add the app name 'my_app' inside the 'settings.py' of the django project folder. You choose 'project_name' and after you open the 'settings.py' file, you scroll to 'INSTALLED_APPS' and add 'my_app'.


![Settings Installed Apps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxfle86nph3ggyckm5lc.png)


<hr>

This is it, the simple creation of a Django application.