Hello world in django: Django lets you build Web Apps with Python. In this article we’ll create the hello world app. This app will show the message “Hello World”.
Django is an open-source Python Web Framework. You can develop secure websites and web applications with Django. Are you ready to do Django programming with Python?
Related course:
Django Web Developer Course
Make a new project
You can develop and deploy your Django Python app directly online. This saves you a lot of time.
Create an account and select the option Add a new web app You can setup a Python Django app with only a few clicks.
Click next and select “Django” from the list of Python modules.
Then select the latest Python version (>= 3.7) from the list of versions and click next.
Create a name for your project and click next.
Click next. It’s setting up the Web App using Python and Django. You can then immediately access it online, using the url shown.
Open a console in the pythonanywhere web interface and edit the file views.py. You can edit it with any text editor like nano, vim etc.
Add the content below to the file views.py
from django.http import HttpResponse |
The function helloView() will be called, each time someone opens the webpage.
The function returns a HttpResponse, this is text that is shown in your web browser (Chrome, Firefox, Safari).
Then open the file urls.py and modify it to the code below:
from django.contrib import admin |
This file maps all of the paths to the Python functions. In this case the webpage path ‘’ (domain name) maps to the helloView function.
Finally restart the server by clicking the green button on your project page.
Open the url in your web browser, and you’ll see Hello world in the web page.
That’s it!
Develop offline
I highly recommend developing your app online, using the tutorial shown above. Because this saves you a lot of time and is the easiest way to get started. You can skip the section below.
If you only want to develop offline only (not recommended), you can execute these commands in a terminal:
django-admin startproject first_django_project |
Wait.. what did we just do?
- We created a project named first_django_project
- We created an app named hello
Django projects can have multiple apps. Every app is like a website component, it has unique functionality.
Hello world code
You generated some Django code. You can modify this code to turn it into the hello world program. If you open the folder /first_django_project/hello you’ll see this:
A lot of code! Which file do we need to change?
There’s one file named views.py. If you open the file you’ll see this:
from django.shortcuts import render |
You create your view in here. A view returns what the visitor sees: a webpage. Any webpage is defined in the HTML language.
Create a view like this:
from django.shortcuts import render |
We create a class HomePageView with the method dispatch.
This methods return the webpage code (in HTML).
URL router
Python needs to return the view, if we visit the webpage.
In the app directory, create the file hello/urls.py with this content:
from django.conf.urls import url
from hello.views import HomePageView
urlpatterns = [
url(r'^$', HomePageView.as_view(), name='home'),
]
In the project directory, change /first_django_project/urls.py:
from django.conf.urls import include, url
urlpatterns = [
url(r'', include('hello.urls')),
]
Change app settings
Then we edit first_django_project/settings.py. Apps can be added or removed in this file.
You’ll see a list definition:
INSTALLED_APPS = [ .. ]
Add our hello app to this list.
We’ll remove the line SessionAuthenticationMiddleware.
Finally we’ll disable the database. This is defined somewhere on line 72.
DATABASES = {
...
}
Remove those lines.
All set!
Run the command:
python manage.py runserver
Then open the URL to see the result:
If you see the above result, you completed this lesson!