How to make a simple website in Django: a python web framework.



In this article we presume that you have python and django installed in your system. First we will make a django project and name it website and under this project we will create an app and will name it hello.

 

1. Open your command terminal in any location in your system where you want to make this project and type below command to make  a project with name website.

    django-admin startproject website

    a folder with website name will be created and the directory layout will look similar to below image.

    

 

2. Inside this folder you will see a python file named "manage.py". This will help us to execute several commands, some of these we  will use in this project.

    Inside website folder again open the command terminal and enter below command to create an app with name hello

    python manage.py startapp hello

    now the directory will look like this

    

 

    make sure the above command is executed in the folder where manage.py file is located else you will get below error.

    python: can't open file 'manage.py': [Errno 2] No such file or directory

 

3. Inside your main website folder get into the website folder and open urls.py and delete all the content and type in below content and  save it.

from django.contrib import admin

from django.urls import path,include

urlpatterns = [

    path('admin/', admin.site.urls),

    path('',include("hello.urls"))

]

 

4. Now go to your hello folder inside the main website folder and create a urls.py file in this folder(make sure the file has .py extension)  and inside this urls.py file add below code

from django.urls import path

from . import views

urlpatterns = [

    path('',views.home)

]

 

5. now go to your views.py file inside your hello folder under main website folder and add below code

from django.http import HttpResponse

def home(request):

    return HttpResponse("Hello World")

 

6. Now the coding part is over, we need to run our local server. To do this, open the command prompt in your main website folder where manage.py file is located and type in below command.

python manage.py runserver

this command will run our server and we will get below output in our command prompt.

In this output we can see that our server is running at http://127.0.0.1:8000/ . Keep this prompt open, if you close this prompt it will close our server too.

Now copy http://127.0.0.1:8000/ and paste it in your browser url. You will see below result.

 

 

 

With this our objective for this article is accomplished. You may not understand it completely and keeping this in mind we have published below article explaining all of the above steps in detail.

How to make a simple website in django (Part 2 : Explanation)

Please visit the above link if you had any difficulty in understanding any of the above steps. Good bye for now.


Published : June 20, 2020