1steps Creating Basic Django Project
1steps Creating Basic Django Project
python --version
If Python is installed, you will get a result with the version number, like this
Python 3.9.2
If you find that you do not have Python installed on your computer, then you can download it for
free from the following website: https://www.python.org/
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.
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
The name of the virtual environment is your choice, in this tutorial we will call it myworld.
Page | 1
Type the following in the command prompt, remember to navigate to where you want to create
your project:
Windows:
py -m venv myworld
Unix/MacOS:
This will set up a virtual environment, and create a folder named "myworld" with subfolders and
files, like this:
myworld
Include
Lib
Scripts
pyvenv.cfg
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:
Unix/MacOS:
(myworld) ... $
Note: You must activate the virtual environment every time you open the command prompt to
work on your project.
Install Django
Now, that we have created a virtual environment, we are ready to install Django.
Note: Remember to install Django while you are in the virtual environment!
Page | 2
Django is installed using pip, with this command:
Windows:
Unix/MacOS:
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!
Windows:
py --version
Unix/MacOS:
python --version
Page | 3
Check Django Version
You can check if Django is installed by asking for its version number like this:
If Django is installed, you will get a result with the version number:
4.1.2
my_tennis_club
manage.py
my_tennis_club/
__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.
Navigate to the /my_tennis_club folder and execute this command in the command prompt:
Page | 4
py manage.py runserver
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.
October 27, 2022 - 13:03:14
Django version 4.1.2, using settings 'my_tennis_club.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Open a new browser window and type 127.0.0.1:8000 in the address bar.
The result:
Page | 5
Django Create App
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.
In this tutorial we will create an app that allows us to list and register members in a database.
But first, 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 my_tennis_club 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.
my_tennis_club
manage.py
my_tennis_club/
members/
migrations/
__init__.py
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
These are all files and folders with a specific meaning. You will learn about most of them later in
this tutorial.
This is where we gather the information we need to send back a proper response.
Page | 6
Django Views
Views
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.
my_tennis_club/members/views.py:
Find it and open it, and replace the content with this:
my_tennis_club/members/views.py:
def members(request):
Note: The name of the view does not have to be the same as the application.
Django URLs
Create a file named urls.py in the same folder as the views.py file, and type this code in it:
Page | 7
my_tennis_club/members/urls.py:
urlpatterns = [
The urls.py file you just created is specific for the members application. We have to do some
routing in the root directory my_tennis_club as well. This may seem complicated, but for now, just
follow the instructions below.
There is a file called urls.py on the my_tennis_club 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/.
my_tennis_club/my_tennis_club/urls.py:
urlpatterns = [
path('', include('members.urls')),
path('admin/', admin.site.urls),
If the server is not running, navigate to the /my_tennis_club folder and execute this command in the
command prompt:
py manage.py runserver
Page | 8
Page | 9