How to start your first project with Python Django in 10 minutes
Install PyCharm
Python3
Check if Python3 is already installed
python3 — version
Install Virtualenv
- Install virtualenv
pip3 install virtualenv
Let’s start by creating our very first virtual environment (for that — we need to create project path called “venv”) and installing Django. Command : virtualenv <virtualenvprojectpath>
virtualenv venv -p python3
Activate the newly created virtualenv
source venv/bin/activate
If you are done working in the virtual environment for the moment, you can deactivate it
deactivate
Install Django
pip install django
To start a new Django project, run the command below. The command-line utility django-admin is automatically installed with Django. After we run the command above, it will generate the base folder structure for a Django project.
django-admin startproject pyprojectss
Go into project folder (pyprojectss)
Django comes with a simple web server installed. It’s very convenient during the development, so we don’t have to install anything else to run the project locally. We can test it by executing the command:
python manage.py runserver
Notes :
- django-admin is Django’s command-line utility for administrative tasks. In addition, manage.py is automatically created in each Django project. It does the same thing as django-admin but also sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.
- Generally, when working on a single Django project, it’s easier to use manage.py than django-admin. If you need to switch between multiple Django settings files, use django-admin with DJANGO_SETTINGS_MODULE or the — settings command line option.
Django Apps
To create your app, make sure you’re in the same directory as manage.py and type this command
django-admin startapp boards
This will give us the following directory structure:
Create our first view in views.py
Let’s configure our project to use it (include the app in our project to get the project to know about your application) Open the settings.py and try to find the INSTALLED_APPS variable
To call the view, we need to map it to a URL — and for this we need a URLconf. To create a URLconf in the polls directory, create a file called urls.py
The next step is to point the root URLconf at the polls.urls module. In pyprojectss/urls.py, add an import for django.urls.include and insert an include() in the urlpatterns list, so you have:
Start the web server. You need to be in the directory that contains the manage.py file (the pyprojectss directory). In the console run
python manage.py runserver