Django hello world

Create hello world in django?

In this article we’ll create the hello world app.

If you do not have Django installed, see the previous tutorials.

You may like: Python and Django Full Stack Web Developer Bootcamp

Make a new project

Execute these commands:

django-admin startproject first_django_project
cd first_django_project/
python manage.py startapp hello

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

We create the hello world code.
If you open the folder /first_django_project/hello you’ll see this:

django app

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:

We’re gonna create our view in here.

Note: A view returns what the visitor sees: a webpage.

 
We’ll use HTML and a class to create the view.

If you are not familiar with these, I recommend this course:
Python and Django Full Stack Web Developer Bootcamp

Add these lines of code:

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.

If we visit a webpage, the web browser sends a HTTP request.
To link views with the HTTP request, we create a new route.

 
In the app directory, create the file hello/urls.py with this content:

In the project directory, change /first_django_project/urls.py:

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:

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.

Remove those lines.

All set!

Run the command:

python manage.py runserver

Then open the URL to see the result:

django hello world

If you see the above result, you completed this lesson!

Previous Post
Next Post

Leave a Reply