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.
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.
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.
1) clone a GitHub repsoitory in PyCharm:
You copy the GitHub link of your repository which you want to clone into PyCharm.
When PyCharm is open, you navigate on the main menu to 'Git' and choose 'Clone...'
You copy your repository link into the 'URL' line and press 'Clone'
You 'Trust Project' and open the project 'In This Window'.
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.
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...'
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'.
A new folder named 'venv' is now in your project folder.
And if you open a terminal, you can see the (venv)
is activated.
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.
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)
This command will create the Django app in your root directory.
Your project tree should be like this:
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.
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:
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'.
This is it, the simple creation of a Django application.
Top comments (0)