| Django is a Python framework that makes it easier to create web sites using Python. Django takes care of the difficult stuff so that you can concentrate on building your web applications. Django emphasizes reusability of components, also referred to as DRY (Don't Repeat Yourself), and comes with ready-to-use features like login system, database connection and CRUD operations (Create Read Update Delete). Django follows the MVT design pattern (Model View Template). - Model - The data you want to present, usually data from a database.
- View - A request handler that returns the relevant template and content - based on the request from the user.
- Template - A text file (like an HTML file) containing the layout of the web page, with logic on how to display the data.
URLs - Django also provides a way to navigate around the different pages in a website.
- When a user requests a URL, Django decides which view it will send it to.
- This is done in a file called
urls.py.
Common WorkFlow in Django - here's what you should keep in mind When you have installed Django and created your first Django web application, and the browser requests the URL, this is basically what happens: - Django receives the URL, checks the
urls.py file, and calls the view that matches the URL. - The view, located in
views.py, checks for relevant models. - The models are imported from the
models.py file. - The view then sends the data to a specified template in the
template folder. - The template contains HTML and Django tags, and with the data it returns finished HTML content back to the browser.
Django can do a lot more than this, but this is basically what you will learn in this tutorial, and are the basic steps in a simple web application made with Django. Django Requires Python To check if you system has Python install, run this command in the command prompt: python --version
If Python is installed, you will get a result with the version number, like this Python 3.12.2
PIP To install Django, you must use a package manager like PIP, which is included in Python from version 3.4. To check if your system has PIP installed, run this command in the command prompt: pip --version
If PIP is installed, you will get a result with the version number. For me, on a windows machine, the result looks like this: pip 20.2.3 from c:\python39\lib\site-packages\pip (python 3.9)
If you do not have PIP installed, you can download and install it from this page: https://pypi.org/project/pip/ Virtual Environment It is suggested to have a dedicated virtual environment for each Django project, and one way to manage a virtual environment is venv, which is included in Python. The name of the virtual environment is your choice, in this tutorial we will call it myworld. Type the following in the command prompt, remembers to navigate to where you want to create your project: Windows: py -m venv myworld
Unix/MacOS: python -m venv myworld
This will set up a virtual environment, and create a folder named "myworld" with subfolders and files, like this: % ls -Fal
total 16
drwxr-xr-x 7 chunkang staff 224 Jul 2 14:39 ./
drwxr-x---+ 66 chunkang staff 2112 Jul 2 14:29 ../
drwxr-xr-x 14 chunkang staff 448 Jul 2 12:51 bin/
drwxr-xr-x 3 chunkang staff 96 Jul 2 12:40 include/
drwxr-xr-x 3 chunkang staff 96 Jul 2 12:40 lib/
-rw-r--r-- 1 chunkang staff 289 Jul 2 12:40 pyvenv.cfg
Then you have to activate the environment, by typing this command: Windows: myworld\Scripts\activate.bat
Unix/MacOS: source myworld/bin/activate
Once the environment is activated, you will see this result in the command prompt: Windows: (myworld) C:\Users\Your Name>
Unix/MacOS: (myworld) ... $
Install Django Now, that we have created a virtual environment, we are ready to install Django. Note: Remembers to install Django while you are in the virtual environment! Django is installed using pip, with this command: Windows: (myworld) C:\Users\Your Name>py -m pip install Django
Unix/MacOS: (myworld) ... $ python -m pip install Django
Which will give a result that looks like this (at least on my Windows machine): Collecting Django
Downloading Django-4.0.3-py3-none-any.whl (8.0 MB)
|████████████████████████████████| 8.0 MB 2.2 MB/s
Collecting sqlparse>=0.2.2
Using cached sqlparse-0.4.2-py3-none-any.whl (42 kB)
Collecting asgiref<4,>=3.4.1
Downloading asgiref-3.5.0-py3-none-any.whl (22 kB)
Collecting tzdata; sys_platform == "win32"
Downloading tzdata-2021.5-py2.py3-none-any.whl (339 kB)
|████████████████████████████████| 339 kB 6.4 MB/s
Installing collected packages: sqlparse, asgiref, tzdata, Django
Successfully installed Django-4.0.3 asgiref-3.5.0 sqlparse-0.4.2 tzdata-2021.5
WARNING: You are using pip version 20.2.3; however, version 22.3 is available.
You should consider upgrading via the 'C:\Users\Your Name\myworld\Scripts\python.exe -m pip install --upgrade pip' command.
That's it! Now you have installed Django in your new project, running in a virtual environment! Running your project on Windows, Mac, or Unix You can run this project on either one. There are some small differences, like when writing commands in the command prompt, Windows uses py as the first word in the command line, while Unix and MacOS use python: Windows: py --version
Unix/MacOS: python --version
In the rest of this tutorial, we will be using the Windows command. If you just want to use python just like Windows, you can make it by: ln -s $(which python3) $(dirname $(which python3))/py
Above is based on your python3 Check Django Version You can check if Django is installed by asking for its version number like this: (myworld) C:\Users\Your Name>django-admin --version
If Django is installed, you will get a result with the version number: Python 3.12.2
Creating a Django Project Once you have come up with a suitable name for your Django project, like mine: pub, navigate to where in the file system you want to store the code (in the virtual environment), I will navigate to the myworld folder, and run this command in the command prompt: django-admin startproject pub
Django creates a pub folder on my computer, with this content: pub
├── manage.py
└── pub
├── __init__.py
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
These are all files and folders with a specific meaning, you will learn about some of them later in this tutorial, but for now, it is more important to know that this is the location of your project, and that you can start building applications in it. Run the Django Project Now that you have a Django project, you can run it, and see what it looks like in a browser. Navigate to the /pub folder and execute this command in the command prompt: py manage.py runserver
Which will produce this result: Watching for file changes with StatReloader
Performing system checks...
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.
July 02, 2024 - 21:43:31
Django version 5.0.6, using settings 'pub.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Open a new browser window and type 127.0.0.1:8000 in the address bar. The result: 
What is an App? An app is a web application that has a specific meaning in your project, like a home page, a contact form, or a members database. Let's just create a simple Django app that displays "Hello World!". Create App I will name my app members. Start by navigating to the selected location where you want to store the app, in my case the pub folder, and run the command below. If the server is still running, and you are not able to write commands, press [CTRL] [BREAK], or [CTRL] [C] to stop the server and you should be back in the virtual environment. py manage.py startapp members
Django creates a folder named members in my project, with this content: pub
├── db.sqlite3
├── members
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── manage.py
└── pub
├── __init__.py
├── __pycache__
│ ├── __init__.cpython-312.pyc
│ ├── settings.cpython-312.pyc
│ ├── urls.cpython-312.pyc
│ └── wsgi.cpython-312.pyc
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
First, take a look at the file called views.py. This is where we gather the information we need to send back a proper response. Django Views views.py Django views are Python functions that takes http requests and returns http response, like HTML documents. A web page that uses Django is full of views with different tasks and missions. Views are usually put in a file called views.py located on your app's folder. There is a views.py in your members folder that looks like this: pub/members/views.py:
from django.shortcuts import render
# Create your views here.
Find it and open it, and replace the content with this: pub/members/views.py:
from django.shortcuts import render
from django.http import HttpResponse
def members(request):
return HttpResponse("Hello world!")
Note: The name of the view does not have to be the same as the application. I call it members because I think it fits well in this context. Django URLs urls.py Create a file named urls.py in the same folder as the views.py file, and type this code in it: pub/members/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('members/', views.members, name='members'),
]
The urls.py file you just created is specific for the members application. We have to do some routing in the root directory pub as well. This may seem complicated, but for now, just follow the instructions below. There is a file called urls.py on the pub folder, open that file and add the include module in the import statement, and also add a path() function in the urlpatterns[] list, with arguments that will route users that comes in via 127.0.0.1:8000/. Then your file will look like this: pub/pub/urls.py:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('', include('members.urls')),
path('admin/', admin.site.urls),
]
To make sure, please double check where your urls.py file is located like below: 
Note that every single app should have its own urls.py. If the server is not running, navigate to the /pub folder and execute this command in the command prompt:
py manage.py runserver
In the browser window, type 127.0.0.1:8000/members/ in the address bar. 
Django Templates Create a templates folder inside the members folder, and create a HTML file named myfirst.html. The file structure should be like this: pub
manage.py
pub/
members/
templates/
myfirst.html
Open the HTML file and insert the following:
pub/members/templates/myfirst.html: <!DOCTYPE html>
<html>
<body>
<h1>Hello World!</h1>
<p>Welcome to my first Django project!</p>
</body>
</html>
Modify the View Open the views.py file and replace the members view with this: pub/members/views.py: from django.http import HttpResponse
from django.template import loader
def members(request):
template = loader.get_template('myfirst.html')
return HttpResponse(template.render())
Change Settings To be able to work with more complicated stuff than "Hello World!", We have to tell Django that a new app is created. This is done in the settings.py file in the my_tennis_club folder. Look up the INSTALLED_APPS[] list and add the members app like this: pub/pub/settings.py: INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'members'
]
Then run this command: py manage.py migrate
Which will produce this output: Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying admin.0003_logentry_add_action_flag_choices... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying auth.0010_alter_group_name_max_length... OK
Applying auth.0011_update_proxy_permissions... OK
Applying auth.0012_alter_user_first_name_max_length... OK
Applying sessions.0001_initial... OK
(myworld) C:\Users\Your Name\myworld\pub>
Start the server by navigating to the /pub folder and execute this command: py manage.py runserver In the browser window, type 127.0.0.1:8000/members/ in the address bar. The result should look like this: 
Django Models Now we will see how Django allows us to work with data, without having to change or upload files in the prosess. In Django, data is created in objects, called Models, and is actually tables in a database. Create Table (Model) To create a model, navigate to the models.py file in the /members/ folder. Open it, and add a members table by creating a members class, and describe the table fields in it:pub/members/models.py: from django.db import models
class CKMember(models.Model):
firstname = models.CharField(max_length=255)
lastname = models.CharField(max_length=255)
- The first field, firstname, is a Text field, and will contain the first name of the members.
- The second field, lastname, is also a Text field, with the members's last name.
- Both firstname and lastname is set up to have a maximum of 255 characters.
SQLite Database - When we created the Django project, we got an empty SQLite database.
- It was created in the my_tennis_club root folder, and has the filename db.sqlite3.
- By default, all Models created in the Django project will be created as tables in this database.
Migrate Now when we have described a Model in the models.py file, we must run a command to actually create the table in the database. Navigate to the /pub/ folder and run this command: py manage.py makemigrations members
Which will result in this output: Migrations for 'members':
members\migrations\0001_initial.py
- Create model members
(myworld) C:\Users\Your Name\myworld\pub>
Django creates a file describing the changes and stores the file in the /migrations/ folder: pub/members/migrations/0001_initial.py:
# Generated by Django 5.0.6 on 2024-07-16 22:14
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='CKMember',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('firstname', models.CharField(max_length=255)),
('lastname', models.CharField(max_length=255)),
],
),
]
Note that Django inserts an id field for your tables, which is an auto increment number (first record gets the value 1, the second record 2 etc.), this is the default behavior of Django, you can override it by describing your own id field. The table is not created yet, you will have to run one more command, then Django will create and execute an SQL statement, based on the content of the new file in the /migrations/ folder. Run the migrate command: py manage.py migrate
Which will result in this output: Operations to perform:
Apply all migrations: admin, auth, contenttypes, members, sessions
Running migrations:
Applying members.0001_initial... OK
(myworld) C:\Users\Your Name\myworld\pub>
Now you have a members table in you database! View SQL As a side-note: you can view the SQL statement that were executed from the migration above. All you have to do is to run this command, with the migration number: py manage.py sqlmigrate members 0001
Which will result in this output: BEGIN;
--
-- Create model members
--
CREATE TABLE "members_members" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "firstname" varchar(255) NOT NULL, "lastname" varchar(255) NOT NULL); COMMIT;
Add Records The Members table created in the above is empty. We will use the Python interpreter (Python shell) to add some members to it. To open a Python shell, type this command: py manage.py shell
Now we are in the shell, the result should be something like this: Python 3.12.2 (main, Feb 6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
At the bottom, after the three >>> write the following: >>> from members.models import CKMember
Hit [enter] and write this to look at the empty Member table: >>> CKMember.objects.all()
This should give you an empty QuerySet object, like this: <QuerySet []>
A QuerySet is a collection of data from a database. Add a record to the table, by executing these two lines: >>> member = CKMember( firstname='Chun', lastname='Kang')
>>> member.save()
Execute this command to see if the Member table got a member: >>> Member.objects.all().values()
Hopefully, the result will look like this: <QuerySet [{'id': 1, 'firstname': 'Chun', 'lastname': 'Kang'}]>
Add Multiple Records You can add multiple records by making a list of Member objects, and execute .save() on each entry: >>> member1 = CKMember( firstname='Johnny', lastname='Kang')
>>> member2 = CKMember( firstname='Jane', lastname='Kang')
>>> member3 = CKMember( firstname='Sunmmi', lastname='Moon')
>>> member4 = CKMember( firstname='David', lastname='Lyme')
>>> member5 = CKMember( firstname='Edward', lastname='Furlong')
>>> members_list = [member1,member2, member3, member4, member5]
>>> for x in members_list:
... x.save()
...
Now there are 6 members in the Member table: >>> CKMember.objects.all().values()
<QuerySet [{'id': 1, 'firstname': 'Chun', 'lastname': 'Kang'},
{'id': 2, 'firstname': 'Johnny', 'lastname': 'Kang'},
{'id': 3, 'firstname': 'Jane', 'lastname': 'Kang'},
{'id': 4, 'firstname': 'Sunmmi', 'lastname': 'Moon'},
{'id': 5, 'firstname': 'David', 'lastname': 'Lyme'},
{'id': 6, 'firstname': 'Edward', 'lastname': 'Furlong'}]>
>>>
Update Records To update records that are already in the database, we first have to get the record we want to update: >>> from members.models import CKMember
>>> x = CKMember.objects.all()[4]
x will now represent the member at index 4, which is "Stale Refsnes", but to make sure, let us see if that is correct:
>>> x.firstname
This should give you this result: 'David'
Now we can change the values of this record: >>> x.firstname = "Dave"
>>> x.save()
Execute this command to see if the Member table got updated: >>> CKMember.objects.all().values()
Hopefully, the result will look like this: <QuerySet [{'id': 1, 'firstname': 'Chun', 'lastname': 'Kang'},
{'id': 2, 'firstname': 'Johnny', 'lastname': 'Kang'},
{'id': 3, 'firstname': 'Jane', 'lastname': 'Kang'},
{'id': 4, 'firstname': 'Sunmmi', 'lastname': 'Moon'},
{'id': 5, 'firstname': 'Dave', 'lastname': 'Lyme'},
{'id': 6, 'firstname': 'Edward', 'lastname': 'Furlong'}]>
Delete Records To delete a record in a table, start by getting the record you want to delete: >>> from members.models import CKMember
>>> x = CKMember.objects.all()[5]
x will now represent the member at index 5, which is "Jane Doe", but to make sure, let us see if that is correct:
>>> x.firstname
This should give you this result: 'Edward'
Now we can delete the record: >>> x.delete()
The result will be: (1, {'members.CKMember': 1})
Which tells us how many items were deleted, and from which Model. If we look at the Member Model, we can see that 'Jane Doe' is removed from the Model: >>> CKMember.objects.all().values()
<QuerySet [{'id': 1, 'firstname': 'Chun', 'lastname': 'Kang'},
{'id': 2, 'firstname': 'Johnny', 'lastname': 'Kang'},
{'id': 3, 'firstname': 'Jane', 'lastname': 'Kang'},
{'id': 4, 'firstname': 'Sunmmi', 'lastname': 'Moon'},
{'id': 5, 'firstname': 'Dave', 'lastname': 'Lyme'}]>
>>>
Add Fields in the Model To add a field to a table after it is created, open the models.py file, and make your changes: pub/members/models.py:
from django.db import models
class CKMember(models.Model):
firstname = models.CharField(max_length=255)
lastname = models.CharField(max_length=255)
phone = models.IntegerField()
joined_date = models.DateField()
As you can see, we want to add phone and joined_date to our Member Model. This is a change in the Model's structure, and therefor we have to make a migration to tell Django that it has to update the database: py manage.py makemigrations members
Which, in my case, will result in a prompt, because I try to add fields that are not allowed to be null, to a table that already contains records. As you can see, Django asks if I want to provide the fields with a specific value, or if I want to stop the migration and fix it in the model: py manage.py makemigrations members
You are trying to add a non-nullable field 'joined_date' to members without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
2) Quit, and let me add a default in models.py
Select an option:
I will select option 2, and open the models.py file again and allow NULL values for the two new fields: pub/members/models.py:
from django.db import models
class CKMember(models.Model):
firstname = models.CharField(max_length=255)
lastname = models.CharField(max_length=255)
phone = models.IntegerField(null=True)
joined_date = models.DateField(null=True)
And make the migration once again: py manage.py makemigrations members
Which will result in this: Migrations for 'members':
members\migrations\0002_ckmember_joined_date_ckmember_phone.py
- Add field joined_date to ckmember
- Add field phone to ckmember
Run the migrate command: py manage.py migrate
Which will result in this output: Operations to perform:
Apply all migrations: admin, auth, contenttypes, members, sessions
Running migrations:
Applying members.0003_ckmember_joined_date_ckmember_phone... OK
Insert Data We can insert data to the two new fields with the same approach as we did in above First we enter the Python Shell: py manage.py shell
Now we are in the shell, the result should be something like this: Python 3.12.2 (main, Feb 6 2024, 20:19:44) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
At the bottom, after the three >>> write the following (and hit [enter] for each line): >>> from members.models import CKMember
>>> x = CKMember.objects.all()[0]
>>> x.phone = 10070216
>>> x.joined_date = '2004-10-16'
>>> x.save()
>>>
This will insert a phone number and a date in the Member Model, at least for the first record, the four remaining records will for now be left empty. We will deal with them later in the tutorial. Execute this command to see if the Member table got updated: >>> CKMember.objects.all().values()
The result should look like this: <QuerySet [{'id': 1, 'firstname': 'Chun', 'lastname': 'Kang', 'phone': 10070216, 'joined_date': datetime.date(2004, 10, 16)},
{'id': 2, 'firstname': 'Johnny', 'lastname': 'Kang', 'phone': None, 'joined_date': None},
{'id': 3, 'firstname': 'Jane', 'lastname': 'Kang', 'phone': None, 'joined_date': None},
{'id': 4, 'firstname': 'Sunmmi', 'lastname': 'Moon', 'phone': None, 'joined_date': None},
{'id': 5, 'firstname': 'Dave', 'lastname': 'Lyme', 'phone': None, 'joined_date': None}]>
>>>
Reference URL: https://www.w3schools.com/django/django_update_model.php |