In this article, we will learn how to develop and run a python-Django app in less than 5 minutes.
Step to create and run the Django app:
Install the virtual environment. You may proceed without a virtual environment too, but in the long run, the virtual environment is going to be very helpful.
$ pip install virtualenv
Create a virtual environment.
$ virtualenv -p /usr/bin/python3 helloworld_VE
Activate the virtual environment.
$ source helloworld_VE/bin/activate
Install the latest Django framework in the virtual environment.
$ pip install django
Create your project.
$ django-admin startproject myproject
Go inside the newly created project directory.
$ cd myproject
You will see a file
and a directory with the same name as of your project.
Create an app here. Every project consists of one or more apps. These are plug-able modules that can be reused in another project if written properly.
$ python manage.py startapp helloworld
directory. create a new file
Add the below lines to this file and save it.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
Open views file and save the below code in it.
from django.shortcuts import render
from django.http import HttpResponse
return HttpResponse("Hello World. First Django Project. PythonCircle.Com")
Include the helloworld app's URLs in main project URLs file.
myproject/urls.py file and make add below line in
from django.conf.urls import include
Now finally add
helloworld in installed apps in
Now start the Django development server by running the command.
$ python manage.py runserver
This will run the python HTTP server on the localhost and 8000 port. If you want to run it on a different port, use port number in the command. For example
$ python manage.py runserver 8888.
Use this command to make your project available for everyone on the network.
$ python manage.py runserver 0.0.0.0:8000
So this was a basic tutorial to set up a Django app in less than 5 minutes. You can refer to GitHub code and video.
Code on Github:
Github URL : https://github.com/anuragrana/hellow-world-django.git
Hosting the Django app on the server:
Read how you can host your Django app on pythonanywhere server for free
. A complete step by step guide with all screenshots and video.