Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Django Hello World App – Getting Started With Django

Getting Started With Django

This tutorial will get you started on the basics of Django. Let’s understand how we can create a hello world app on Django.

What is Django? Django is a python based web framework that is used for making web applications efficiently.

Note: Django is just a framework for web applications and not a Language.


1. Installing Django

We can install Django in our system by using PIP. We’ve outlined the steps below to install Django on different operating systems.

Django on Windows

First, we have to save get-pip.py on your computer.

Second, open cmd at the place where the file is saved and then write the command

python get-pip.py

Third, now if you want to upgrade the version you can write 

python -m pip install -U pip

Django on Mac OS

First, install pip using easy install

$ sudo easy_install pip

Second, we can now upgrade it using the command below

$ sudo pip install --upgrade pip

Django on Linux

We can install using the commands in the terminal shell

$ sudo apt-get update
$ sudo apt-get install python-pip
$ sudo pip install --upgrade pip

Once Django is installed we can move to the next section.


2. Starting Your First Django Project

Now in your computer, Navigate to the folder where you want to start your project.open command prompt/shell and type the following commands.

$ django-admin startproject <Name_of_project>

where <name_of_project> is the name you want to give to your project.

Django Startproject
Django Startproject

Now if you will look at the target location you will be able to see a new folder with the name of the project. Inside the folder, you will find another folder with the same name and manage.py file.

Note: manage.py python file manages the whole project and combines different parts of the project together for the Web application. 

A typical project will have a structure like this.

Django Project Structure
Django Project Structure

Now once the project is setup we can try running the server.


3. Running the Server on your localhost

Right now, the project is on your computer but in reality, the whole project for the web application will be uploaded to a web server.

For now, since the project is on your computer, the server will run on your localhost.

Now write the following command in the terminal shell/cmd to run the server.

$ python manage.py runserver
Runserver
Runserver

 The server will start running, and you can check the web app by going to that URL. (http://127.0.0.1:8000/ in my case)

Website Runserver
Website Runserver

Hence our web application is working perfectly and let’s move on to make an App.


3. Difference between the Django project and Django App.

Now before making our first Hello World App we need to know the difference between the Django project and the Django App

Django Project

A Django project is used for the entire Web application. That is there will be one project for handling one complete website. 

Inside the project, there can be many different Django apps handling a variety of processes.

Django App

Inside a project, an app is used for handling a particular section of the website. In a typical web application, one app is used entirely for User Authentication, one entirely for Payments, etc.

Django Project and App
Django Project and App (HelloWorldApp)

4. Running Your Hello World App

Inside the project folder, open-shell and write commands to set up a Hello World app.

$ django-admin startapp HelloWorldApp

Now you will see that your Hello world app is formed.

Django Startapp
Django Startapp

Django App has different python files inside it about which we will learn in the next article.

Note: The Django project won’t know about the app on its own and hence you have to mention it in the django_project/settings.py.

You can add your app in the INSTALLED_APPS for better application management. Under INSTALLED_APPS > Add "<name_of_app>", where <name_of_app> is the name we want to give to our app.

Installed Apps
Installed Apps

5. Modifying Django app files to show “Hello World”

In Django, it requires 3-4 files to show a particular web page. The sequence is given below

URL — Views — models(normally) — template

Hence, when a client goes to a particular URL, that means he sends a web request(for eg HTTP request) to the server, Views.py has a View corresponding to that particular URL request and then the data is sent back as a response(HTTP response) to the client along with the template(HTML file).

Now lets write a simple View in the views.py that shows Hello World in the browser.

SayHello
SayHello views.py

Here first we are importing HttpResponse from django.http library and then create a view called sayHello.

It will require a request from the user hence it takes request as input and then returns HttpResponse – Hello World.

from django.http import HttpResponse

def sayHello(request):
    return HttpResponse('Hello World')

Now we need to create urls.py in the app as well. So we can just copy past it from the project urls.py, then remove the admin path line and then add the following line.

from django.contrib import admin
from django.urls import path, include
from .views import sayHello

urlpatterns = [
    path('', sayHello, name='sayHello'),
]
UrlsHelloWorldApp
UrlsHelloWorldApp

Now we just need to add the endpoint sayHello/ in the urls.py/django_project. We are going to add the following code in to urlpatterns.

path('sayHello/', include('HelloWorldApp.urls')),
Urls Pydjango Project
Urls Pydjango Project

6. Running the Server

Just as shown in the above section 4, we are gonna run the server.

Runserver HelloWorld
Runserver HelloWorld

Now the server is on, hence we can check the browser by adding the endpoint sayHello/ to the URL.

Browser HelloWorld
Browser HelloWorld

We can see that it is working perfectly. Hence our first Django app is ready.

Conclusion

And that brings us to the end. I hope you’re ready to get started on your journey with Django. So stay tuned for more articles on Python Django and Flask in the coming few days!