Django: Beginners guide( Hello World!)

Italo Silva
4 min readOct 14, 2020

--

Begginers start here
Photo by Gia Oris on Unsplash

Learning a programming language is a big challenge, and at the beginning could be quite frustrating. Because of this, a lot of people quit before they could create anything in the programming world. To avoid that, it is necessary to balance this frustration with achievements.
There are several approaches to do that, and We’ll be using project-based learning. This means that the foundations of learning will be taught as it is required to apply in a project. The goal is to create a series of articles designed to explain, in a simple way, all that you need to know to start developing a website using Django and the Pycharm IDE( Integrated Development Environment).
Also, you should read every article at least twice. The first, just follow along with the development process, and in the second, search every term that you don’t know and try to figure out what is happening in the code and repeat the process by yourself. In this article, it will be explained how to create your first program: Hello world!

Before Start

Before starting it, you’ll need to install Python, Django, and Pycharm on your machine. Check the links below:

Hello World!

After creating a new project in Pycharm, it is time to install Django on the project. Open the Pycharm terminal and type this command to install it:

python -m pip install Django

To check if it was correctly installed:

python
import django
print(django.get_version())

This code will output the version of Django installed on your machine. Also, in the project folder(Scripts), there are now two Django files:

Django files added to script folder

Now that Django is installed, it is time to create your first project called “helloworld”. Type in the terminal:

django-admin startproject helloworld

Done! Now in your directory, you’ll see that a new folder is created. Access it and create your first application by typing:

cd helloworld
python manage.py startapp hello

Let’s say hello, now!

In the views file, import a HttpResponse and create a function that returns this HttpResponse with the “hello world” message:

from django.http import HttpResponseDef index(response):
return HttpResponse("Hello World!")

The Hello World is created, but right now, it doesn’t do anything. To properly work, it is necessary to tell Django what is this URL and that the app is installed. And this is something that you have to do every time that you create a new app. There are 3 steps to follow:

  • Uptade the settings file with the app name;
  • Create an URL file to your app;
  • Connect that app page with the project.
#setting.py: in INSTALLED_APPS add the name of the application:INSTALLED_APPS = [
'hello',
.
.
.
]
from django.urls import path, includeurlpatterns = [
path('hello/', include('hello.urls'))

The next step is to create a new python file in the hello folder with the name of urls and create a path for the app url:

  1. Click with the right button of the mouse on the hello folder;
  2. Select: New -> Python File;
  3. On the box created, type: urls and ENTER.

Open the new file and create a path to the project:

from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index')

After that, the app is about done. The only thing left to do is run the server. To do this, in the terminal type:

python manage.py runserver

To see the app executing, open your browser and go to:

http://localhost:8000/hello/

Photo by Giorgio Trovato on Unsplash

Congrats! You’ve created your first app with Django!

In the next article, I’ll explain what happened here, and a better way to do the “hello world”, and how to create your first web page!

--

--

No responses yet