Hello world in Django

Murali mahadeva B S
4 min readAug 1, 2020

--

Django

If you are new to Django. Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

How is Django special

Ridiculously fast

Django was designed to help developers take applications from concept to completion as quickly as possible.

Reassuringly secure

Django takes security seriously and helps developers avoid many common security mistakes.

Exceedingly scalable

Some of the busiest sites on the Web leverage Django’s ability to quickly and flexibly scale.

Lets get Django….

Django is a very easy to learn and comfortable framework. You will enjoy every bit of it. If you find it difficult initially, stick through it I’m sure you will love it.

I’m using Ubuntu Operating system, demonstrations will be on Ubuntu itself. No worries, process doesn’t change much if you are using some other OS. A little side note, get yourself comfortable working with Terminal(Linux and Mac) Command line(Windows).

Requirements:

  • You should have Python installed on your system. If not click here to install on Linux and Mac, and click here to install on Windows.
  • Using virtual environment is not compulsory to get started but it is a very good practice.

Create a directory for your project

On Linux and Mac

mkdir Django && cd Django

On Windows

mkdir Django && chdir Django

Create a virtual environment

I’m using Virtualenv to create virtual environment.

virtualenv venv

Activate the created virtual environment.

On Linux and Mac

source venv/bin/activate

On Windows

venv/scripts/activate

Install latest version of Django

pip install Django

Check version of Django to confirm proper installation

django-admin --version

Create a new Django project

django-admin startproject mynew_django_project .

Side note: Don’t use “-” while naming your Django project. It’ll create an array. You are free to use underscore “_”. Dot “.” at the end installs all files in the current directory. You can leave the dot if you want to install all files within a new directory with the name of your project name. Do experiment. You can delete the directory and start over again.

A new file called manage.py will be created. This Python file will be used to do all Django operations. Now run the Django server by the below command.

python manage.py runserver

It will start the server at the port number 8000. Open any browser, go to the URL “localhost:8000” by typing it in the URL bar.

Django startup page

That’s it your server is up and running. Now lets print “hello world” in the Django page.

I’m using Visual Studio Code as the IDE. Its an amazing IDE you should try it. You can use any IDE of your choice. Open the current project directory in the IDE.

Create a Django app with the command. “myapp” is the app name. Name whatever you want.

python manage.py startapp myapp

Now a folder will be created with the name “myapp” with required files in it.

Till now we worked on setting up Django. Now work begins within Django. Follow closely…

Add your app name within settings.py file under INSTALLED_APPS list.

django_project/settings.py

Add your app’s urls into main project’s urls.py file. You will find that within a folder by your project’s name.

django_project/urls.py

Import include along with path in the import statement. Add a new path line within urlpatterns.

Now open view.py file in myapp.

myapp/views.py

import HttpResponse and create a function to display hello world text.

Create a file called urls.py within app folder. Call the above view function in urls.py file in the app folder.

myapp/urls.py

Now run the rerun the server. You will be able to see the hello world in Django page.I have maximised the size of the text to make it visible but yours should appear small. A side note: ctrl + plus will increase the size where as ctrl + minus will decrease the size.

Final output

Django is not as complex as it appears. Work your way in, you will definitely fall in love with it.

Until then, be curious… be creative…

If you find this post useful, don’t forget to leave a clap

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Responses (1)

Write a response