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.
Step to create and run the django app:
- 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.
Python1234567from django.conf.urls import urlfrom . import viewsurlpatterns = [url(r'^$', views.index, name='index'),]
- Open views file and save below code in it.
Python12345from django.shortcuts import renderfrom django.http import HttpResponsedef 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.
- 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:
Hosting django app on 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.
How to install virtual environment?
Django official tutorials.