In this article, we will learn how to develop and run a python-Django app in less than 5 minutes.
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
$ source helloworld_VE/bin/activate
$ pip install django
$ django-admin startproject myproject
$ cd myproject
manage.pyand a directory with the same name as of your project.
$ python manage.py startapp helloworld
helloworlddirectory. 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 def index(request): 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