In our article "How to make a simple website in django" we have followed many steps and executed several commands to achieve our objective. This article is continuation of the same article and in this we will provide explanation of every single step which we have followed while creating our website. So, let’s start.
Step 1: in this step we have created a django project named website by following the below command.
django-admin startproject website.
we will divide this in 3 parts, first django-admin, second part startproject and third part website
Part 1 - djnago-admin is a command line utility of django using which we can accomplish many tasks. Basic syntax of this command is
django-admin <command> [options]
In our article we have used django-admin to create a django project.
Part 2 – startproject is the command under django-admin utility which helps us to create a django project directory structure. As we have seen when we executed this command a folder was created with many files and subfolders. These files help us to execute and accomplish many tasks in our website project.
Part 3 - website, this is not a command, this is simply the name of our project which must be given after startproject.
Step 2: manage.py file is a python file which helps us to execute several commands and manage our website for e.g. to migrate changes in the database structure, to run server, to collect static files, to create super user etc. This manage.py file gets automatically created for us when we have created our django project. In this step we have created a django app "hello" with the help of manage.py file.
python manage.py startapp hello
In the above command first word is python which simply informs the terminal that you are going to execute a python file.
second is the python file itself (manage.py) by using which we are going to execute a command. In this case the command is startapp which means we are creating an app, this will require an app name, we have named our app as "hello".
Step 3,4,5: I will explain how the request comes to django - request first goes to urls.py file of our project after getting processed from wsgi.py and settings.py. In urls.py it will search for the match of the url we have entered in our browser. In our case we have just typed in domain with empty string at the end "http://127.0.0.1:8000/"
so it will match with path('',include("hello.urls")) and the request will go to urls.py file of our hello app. In this apps urls.py it will again search for the match, since the url we entered in browser is empty after domain name, it will pick the empty path urlpatterns
and then this request goes to home function we have defined in views.py file of our app which looks like below
return HttpResponse("Hello World")
home function is accepting the request and returning an Http Response of "Hello World". The server is passing the response as Http and the same response will be displayed on our browser.
Step 6: In this part we have executed the server. We can test our output in our localhost which is http://127.0.0.1:8000/.
IP 127.0.0.1 in browser will redirect the request to our own computer and our django server is running at port number 8000 by default and hence we have provided port number as 8000 at the end of the url.
We have tried to explain all the necessary commands used to understand the website we have developed. The things will be more clear if you start trying yourself to build similar websites. This way you will be able to make your basics strong and can move on to the advanced stuffs. Good bye for now.