Hello World app with Django

Sjlouji
5 min readAug 20, 2020

What is Django?

Django is a python based open-source web-framework which follows the model-view-controller architectural pattern. It is used for the Standardized structure, Neat Design, Security, and Rapid development. Django uses “Batteries included” philosophy. Django includes a lot of functionalities. Some of the functionality that Django provides for the developers are,

  1. ORM
  2. Database Migration
  3. User Authentication
  4. Inbuild Super Admin Panel
  5. Forms
  6. Testing

Hello World Django Project:

  1. Create a Virtual Environment

Virtual Environment is a tool that helps to keep the dependencies of the project isolated from the other project dependencies so that each project can have isolated space and would not disturb other projects.

Initially let’s create a new folder “hello-world-django”.

$ mkdir hello-world-django$ cd hello-world-django/

To create a virtual environment, we’ll use pipenv. Pipenv can the installed with the below command.

$ pip install pipenv

Once Pipenv has been installed, start installing your dependency. Here we are installing Django since we are creating a Django project.

$ pipenv install django

After Django installation, activate the virtual environment.

$ pipenv shell

If you are on a Mac you should see parentheses now at the beginning of your command line prompt in the form (hello-world-django). If you are on Windows you will not see a visual prompt at this time.

2. Create a Django Project

With the command below, create a Django project called helloworld.

(hello-world-django) bash-3.2$ django-admin startproject helloworld

This will create a simple Django project in our hello-world-django folder.

The Project Structure of the Django project created must look like this.
The project Structure of the Django project created must look like this.

3. Run the Django Project

(hello-world-django) bash-3.2$ cd helloworld/(hello-world-django) bash-3.2$ python3 manage.py runserver

The command will start the Django server.

Watching for file changes with StatReloaderPerforming system checks...System check identified no issues (0 silenced).You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.Run 'python manage.py migrate' to apply them.August 20, 2020 - 08:02:50Django version 3.1, using settings 'helloworld.settings'Starting development server at http://127.0.0.1:8000/Quit the server with CONTROL-C.

Now open your preferred browser and navigate to this address http://120.0.0.1:8000. If everything was fine, then you should see the Django welcome page.

Django Welcome Page
Django Welcome Page

4. Run Migration

Django Migration is a method of applying changes that we have made to a model, into the database schema.

(hello-world-django) bash-3.2$ python3 manage.py migrate

Django by default has some migrations. The above command will run all those default migrations.

Operations to perform:Apply all migrations: admin, auth, contenttypes, sessionsRunning migrations:Applying contenttypes.0001_initial... OKApplying auth.0001_initial... OKApplying admin.0001_initial... OKApplying admin.0002_logentry_remove_auto_add... OKApplying admin.0003_logentry_add_action_flag_choices... OKApplying contenttypes.0002_remove_content_type_name... OKApplying auth.0002_alter_permission_name_max_length... OKApplying auth.0003_alter_user_email_max_length... OKApplying auth.0004_alter_user_username_opts... OKApplying auth.0005_alter_user_last_login_null... OKApplying auth.0006_require_contenttypes_0002... OKApplying auth.0007_alter_validators_add_error_messages... OKApplying auth.0008_alter_user_username_max_length... OKApplying auth.0009_alter_user_last_name_max_length... OKApplying auth.0010_alter_group_name_max_length... OKApplying auth.0011_update_proxy_permissions... OKApplying auth.0012_alter_user_first_name_max_length... OKApplying sessions.0001_initial... OK

5. Create a Django app:

Till now, we have just configured everything needed to develop a web project. Now its time to create an app where we make all our logic and stuff.

(hello-world-django) bash-3.2$ python3 manage.py startapp myapp

The command creates a simple app called myapp with Migrations, Model, Admin, Test, and Views.

Django app myapp
Django app — myapp

Now add the created app to the settings of our project. Open settings.py. The file should look as shown.

INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',]

To add our app to the existing project, add our app name to the installed apps as shown below.

INSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','myapp', //Add your app name here]

6. Creating a View

Now we registered our app with the hello-word-project. Now let's create our first view. Open myapp/views.py. Create a simple function named asindex, that returns a HttpResponse.

from django.shortcuts import renderfrom django.http import HttpResponse# Create your views here.def index(request):return HttpResponse('Hello World')

Create a file named urls.py inside the myappfolder as shown below. We are creating this file to handle routes.

In order to see our view in the browser, we are mapping the view to a URL. Open myapp/urls.py which will be empty. Write the bellow code which is mapping the URL to our view.

from django.conf.urls import urlfrom .views import indexurlpatterns = [url('',index),]

Now connect the myapp/urls.py with the default URL in helloworld/urls.py. The file should look like this.

"""helloworld URL ConfigurationThe `urlpatterns` list routes URLs to views. For more information please see:https://docs.djangoproject.com/en/1.11/topics/http/urls/Examples:Function views1. Add an import:  from my_app import views2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')Class-based views1. Add an import:  from other_app.views import Home2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')Including another URLconf1. Import the include() function: from django.conf.urls import url, include2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))"""from django.conf.urls import url, includefrom django.contrib import adminurlpatterns =url(r'^admin/', admin.site.urls),]

Create a new URL which includes our apps urls.py. The updated code should look like this.

urlpatterns =url(r'^admin/', admin.site.urls),url('helloworld', include('myapp.urls'))]

Now everything is set. Start the server.

(hello-world-django) bash-3.2$ python3 manage.py runserver

Now navigate to http://127.0.0.1:8000/helloworld in your browser. You should see the result below.

Try with some random URL like http://127.0.0.1:8000/random. Django by default handles these path errors with a 404 page saying that the route is not available and also suggests us with the available routes.

Happy coding…..

--

--

Sjlouji

Software Engineer at @Pando. Developer | Writer. From ABC to the world of code.