Hello World app with Django

Rion Louji
4 min readAug 20, 2020

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 creat 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.

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 StatReloader Performing 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:50 Django version 3.1, using settings 'helloworld.settings' Starting development server at Quit the server with CONTROL-C.http://127.0.0.1:8000/

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.

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.

Applying contenttypes.0001_initial...Applying auth.0001_initial...Applying admin.0001_initial...Applying admin.0002_logentry_remove_auto_add...Applying admin.0003_logentry_add_action_flag_choices...Applying contenttypes.0002_remove_content_type_name...Applying auth.0002_alter_permission_name_max_length...Applying auth.0003_alter_user_email_max_length...Applying auth.0004_alter_user_username_opts...Applying auth.0005_alter_user_last_login_null...Applying auth.0006_require_contenttypes_0002...Applying auth.0007_alter_validators_add_error_messages...Applying auth.0008_alter_user_username_max_length...Applying auth.0009_alter_user_last_name_max_length...Applying auth.0010_alter_group_name_max_length...Applying auth.0011_update_proxy_permissions...Applying auth.0012_alter_user_first_name_max_length...Applying sessions.0001_initial...Operations to perform: Apply all migrations: admin, auth, contenttypes, sessions Running migrations:  OK  OK  OK  OK  OK  OK  OK  OK  OK  OK  OK  OK  OK  OK  OK  OK  OK  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.

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 as index, that returns a HttpResponse.

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

Create a file named urls.py inside the myapp folder 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 url from .views import index urlpatterns = [ url('',index), ]

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

"""helloworld URL Configuration The `urlpatterns` list routes URLs to views. For more information please see: Examples:https://docs.djangoproject.com/en/1.11/topics/http/urls/ Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) """ from django.conf.urls import url, include from django.contrib import admin urlpatterns = 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…..

--

--