Django is a backend development language, it is a language for perfectionists. This is called so because it gives you many components out of blue. Whenever you want to develop an application, Django is the go-to language.
To start using Django, you need to be well conversant with Python language first. Begin by knowing all Python-related stuff such as variables, how to do looping in python, tuples, and dictionaries because this is what you encounter every time while working with Django.
In this tutorial, I will take you through setting the Python Django project. The easiest way to do what I usually use is using Pycharm text editor.
Steps to follow:
Step 1: Open Pycharm text editor.
On the upper left corner click on file->New project->Name of the project->create
file->New project->Name of the project->create
Here Pycharm automatically creates a project for you.
Alternatively, you can create a virtual environment on your terminal using this process: How to install Python Django on Ubuntu 20.04
Step 2: Creating a Project
Creating a project is the easiest part, first install Django using the following command on the build-in terminal on Pycharm.
$pip install django~=4.0
Collecting django
Using cached Django-4.0.4-py3-none-any.whl (8.0 MB)
Collecting asgiref<4,>=3.4.1
Using cached asgiref-3.5.1-py3-none-any.whl (22 kB)
Collecting sqlparse>=0.2.2
Using cached sqlparse-0.4.2-py3-none-any.whl (42 kB)
Installing collected packages: sqlparse, asgiref, django
Successfully installed asgiref-3.5.1 django-4.0.4 sqlparse-0.4.2
You can check the version of Django using this command.
$ python -m django --version
4.0.4
Next is to start your project.
On your command line use the following to start a new project.
django-admin startproject myproject .
Make use of the period at the end, it is necessary because it’s instructing that the project be created in the current directory.
If you do an ls inside the project directory, you should see the following:
$ls
manage.py myproject
Inside my project, this should be what you can find inside
asgi.py __init__.py settings.py urls.py wsgi.py
Step 3: Start development server
When you are satisfied with your setup, start the development server while inside the root of the Django project. The root of the Django project is the directory where manage.py is located. in our case its inside myproject folder.
Use the following command to run the development server.
$ python manage.py runserver
This will start the development server and you will the following from your terminal.
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
May 03, 2022 - 18:39:21
Django version 4.0.4, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Don’t worry about the 18 unapplied migrations, we will fix those in a moment. Look the development server will run at port 8000 on our localhost. Copy that URL and paste it into your browser. This is what you will get.
Step 4: Creating an App
Whenever you are creating a project, there are modules that contain the element you want to implement. Let’s say you want to create an e-commerce website, you will want to divide your project into tiny modules called apps. For example, you will create a products app, cart app, customer app, etc. This will make your project more convenient and elegant.
So to create an app use the following command inside the root of your Django project.
python manage.py startapp product
Product is the name of our app in this example.
Inside the created project folder, this is what you will see.
admin.py apps.py __init__.py migrations models.py tests.py views.py
This will be the directory structure
Step 5: Creating your first view
As you can see from the screenshot above, we have views.py. Click on it, this is where you will write your logic. Inside your view insert the following code.
from django.shortcuts import render
from django.http import HttpResponse
def home_view(request):
return HttpResponse('Hello world')
I want you to create another file called URLs inside your app, then you will need to insert the following in that urls.py
product/urls.py
from django.urls import path
from .views import home_view
urlpatterns = [
path('', home_view, name='home')
]
Then on the myproject/urls.py insert the following:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('product.urls')),
path('admin/', admin.site.urls),
]
On the settings.py you need to register your app. Go to settings.py->installed_apps->AddYour app name in our case its product.
Go to settings.py->installed_apps->'product.apps.ProductConfig'
Refresh your browser and you will see Hello world printed on your screen.
In the next lesson, we will see how to create a project using templates and how to do our first database migration.