Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

hello world
django

How to write Hello World in Django

Educative Answers Team

Django is an MTV web framework designed to make web applications in Python. The set of rules for its installation are:

  1. Install Python from python.org
  2. Install Django from Djangoproject.com

Now, in the directory where you have installed Django, you will get a Python file named django-admin.

Open a terminal and move to the directory containing the django-admin file using the cd command.

In our case, the django-admin file resides on the Desktop.

svg viewer

Creating a project and deploying

We can create a new project using the startproject command, followed by the name of the project (HelloWorld). This command will create a new folder with the given name of the project in the same directory. Now, once again, move into the new directory using the cd command and deploy it on the local server.

In your project folder, you will have a Python file with the name manage and another folder with the name project.

svg viewer

After deploying it at the local server, you will get a bunch of lines on the command line interface with the server URL in it (like in the illustration above). Copy that URL to your browser and you will get an introductory message. Once you are done, press Ctrl+c on the command line to exit from the server.

Editing project to print Hello World

Now, move inside the second HelloWorld folder and create a view file using the touch command.

C:\users\educative\Helloworld\HelloWorld> touch view.py

Remember: view.py is case-sensitive

Now, we will edit two files:

  • view.py
  • url.py

View File

In the this file we will write the Http response and Print function.

# editing view.py file
import django.http from HttpResponse

def print(request):
      return HttpResponse("Hello World!")

Url file

Once we have created our view, we will add URL configurations for our view .There will be a lot of code, so you will need to add a few lines for your print function.

# Editing url.py file

# add this command
from . import views

# add this code inside the urlpatterns
url(r'^,view.print), 

Once you are done editing those two files, run this command again:

C:\users\educative\HelloWorld> python manage.py runserver

And, yes! It prints “Hello World” when you run the URL.​

RELATED TAGS

hello world
django
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring