In this article I will teach you how to make and run a python-django app in less than 5 minutes.
Prerequisite: Python (preferably python 3) is already installed.
Install virtual environment. You may proceed without virtual environment too, but in long run it 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 virtual environment.
$ pip install django
Create your project.
$ django-admin startproject myproject
Go inside newly created project directory.
$ cd myproject
You will see a file manage.py
and a directory with same name as of your project.
create an app here. Every project consist of one or more apps. These are plug-able modules which can be reused in other project if written properly.
$ python manage.py startapp helloworld
Go inside helloworld
directory. create a new file urls.py
.
Add 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 below code in it.
from django.shortcuts import render from django.http import HttpResponse def index(request): return HttpResponse("Hello World. First Django Project. ThePythonDjango.Com")
Include the app i.e. helloworld urls in main project urls file. Open myproject/urls.py file and make add below line in urlpatterns.
url(r'^helloworld/',include('helloworld.urls')),
Also import include module.from django.conf.urls import include
Now finally add helloworld
in installed apps in myproject/settings.py file
.
Now run command. $ python manage.py runserver
.
This will run the python http server on localhost and 8000 port. If you want to run it on different port, use port number in command.
$ python manage.py runserver 8888
. To run it on different IP address, pass the IP address as well. 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 basic tutorial to set up django app in less than 5 minute. You can refer below github code and video for same.
Code on Github:
Github URL : https://github.com/anuragrana/hellow-world-django.git
Video: