Starting with Python django

Django is a open source and free python based framework for web programming. I have used python3 and django 2.1.7 on debian 9.8 in this article to write a basic django based web app.

Install django


pip3 install django
Collecting django
  Downloading https://files.pythonhosted.org/packages/c7/87/fbd666c4f87591ae25b7bb374298e8629816e87193c4099d3608ef11fab9/Django-2.1.7-py3-none-any.whl (7.3MB)
    100% |████████████████████████████████| 7.3MB 12kB/s
Collecting pytz (from django)
  Downloading https://files.pythonhosted.org/packages/61/28/1d3920e4d1d50b19bc5d24398a7cd85cc7b9a75a490570d5a30c57622d34/pytz-2018.9-py2.py3-none-any.whl (510kB)
    100% |████████████████████████████████| 512kB 388kB/s
Installing collected packages: pytz, django
Successfully installed django-2.1.7 pytz-2018.9

Create a directory to hold are your development stuff “django-works” or whatever name you like.


manish@iotbox:~ $ mkdir django-works
manish@iotbox:~ $ cd django-works/

Create your first django project framework, let us call it firstone


manish@iotbox:~/django-works $ django-admin startproject firstone

Here is the file structure created by above command


manish@iotbox:~/django-works $ tree firstone/
firstone/
├── db.sqlite3
├── firstone
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-35.pyc
│   │   ├── sayhello.cpython-35.pyc
│   │   ├── settings.cpython-35.pyc
│   │   ├── urls.cpython-35.pyc
│   │   └── wsgi.cpython-35.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py

Now start your bare project to see if everything is good so far.


manish@iotbox:~/django-works/firstone $ python3 manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

March 02, 2019 - 04:30:04
Django version 2.1.7, using settings 'firstone.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

You will notice that it starts server on 127.0.0.1 port 8000. By default server starts on loopback IP and port 8000. This is not useful when you want to access it from network. So modify firstone/settings.py and add network IP of your server hosting python code. Replace IP address with your own server IP.

ALLOWED_HOSTS = ['192.168.0.25', 'localhost', '127.0.0.1']

Now start server again passing 0.0.0.0 to listen on all network interfaces and port 8000. You can change port as well.


manish@iotbox:~/django-works/firstone $ python3 manage.py runserver 0.0.0.0:8000
Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

March 02, 2019 - 04:34:35
Django version 2.1.7, using settings 'firstone.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

Browse http://192.168.0.25:8000/ (Replace IP with your server IP) If everything went well you will see a page with “The install worked successfully! Congratulations!” message.

Let us create a “sayhello” app in our project framework.


manish@iotbox:~/django-works/firstone $ python3 manage.py startapp sayhello

Here is the structure created by startapp command


manish@iotbox:~/django-works/firstone $ tree sayhello/
sayhello/
├── admin.py
├── apps.py
├── __init__.py
├── migrations
│   └── __init__.py
├── models.py
├── __pycache__
│   ├── __init__.cpython-35.pyc
│   ├── sayhello.cpython-35.pyc
│   └── urls.cpython-35.pyc
├── sayhello.py
├── tests.py
├── urls.py
└── views.py

Let us write a simple message in sayhello.py program and then publish this python file via urls.py.

save following in sayhello/sayhello.py


from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, its first django !!")

Create sayhello/urls.py with following content to publish above python script in app.


from django.urls import path
from . import sayhello

urlpatterns = [
    path('', sayhello.index, name='index'),
]

include sayhello/urls.py into parent urls.py as following


from django.contrib import admin
from django.urls import path
from django.conf.urls import include

urlpatterns = [
    path('hello/', include('sayhello.urls')),
    path('admin/', admin.site.urls),
]

Start server again


manish@iotbox:~/django-works/firstone $ python3 manage.py runserver 0.0.0.0:8000
Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

March 02, 2019 - 05:23:01
Django version 2.1.7, using settings 'firstone.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

And browse http://192.168.0.25:8000/hello/ and you will see “Hello, its first django !!”

Output on web browser

…..Done 🙂
Will do something more exciting with django next time.