Powerful ? Thanos In Avengers Or Django In Python Framework ?

Saurabh Dattatray Thakre
3 min readMar 7, 2021

Believe it
- Uzumaki Naruto

Why are you using Django ? Why not other python framework ?
Because Django was named after Django Reinhardt who was a Belgian-born Romani-French jazz guitarist and composer. And I play guitar too XD.
Just kidding, Django is an open-source web framework. Django is ridiculous fast. It encourages rapid development with a clean and pragmatic design, best thing is it has better Debugging Tools

Now lets go ahead and CREATE our first ever Django web application

  • Install Python on your OWN
$ python --version
Python 3.9.0
  • It is always good to create a Virtual Environment
    It will isolates your new development environment and keeps things safe in case you’re working on other projects as well which might have different dependencies
$ mkdir mywebapp
$ cd mywebapp

Now create and activate your virtual environment and make sure you see (djangoenv) in the terminal.

$ python -m venv djangoenv
$ source djangoenv/bin/activate
(djangoenv) ~$
  • Install Django
(djangoenv) ~$ pip install Django
(djangoenv) ~$ pip show django # Spend a minute on the details
  • Create your Django Project
(djangoenv) ~$ django-admin startproject HiddenLeaf

The above command creates a beautiful directory structure with the files you need to get going. Let me do tree on the directory

├── HiddenLeaf
│ ├── asgi.py
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-39.pyc
│ │ ├── settings.cpython-39.pyc
│ │ ├── urls.cpython-39.pyc
│ │ └── wsgi.cpython-39.pyc
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── manage.py
  • Now, with following command you would see you’ve Running Django app
(djangoenv) ~$ python manage.py runserverWatching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).Run 'python manage.py migrate' to apply them.
March 07, 2021 - 06:27:07
Django version 3.1.7, using settings 'HiddenLeaf.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

You will find the app at http://127.0.0.1:8000/.

It was the demo app, but what if you would like to create your own web application. Don’t think, just read forward ! We are going to create our own app.

  • Create a new app in the project, for me the project name is HiddenLeaf
(djangoenv) ~$ python manage.py startapp Hokage

Again you will see the same beautiful directory structure has been created for your new app. All we have to do is just write whatever code you desire in your app.

  • First step, create `templates/Hokage/` two driectories in Hokage directory to put your .html code
HiddenLeaf
├── Hokage
│ ├── templates
│ │ └── Hokage
│ │ └── hello.html
  • All the views should be stored in views.py. Lets modify the HiddenLeaf/Hokage/views.py with following
from django.shortcuts import render  
from django.http import HttpResponse
# Create your views here.
def hello(request):
return render(request, 'Hokage/hello.html')
  • urls.py is the main controller that maps URLs path to your website. Lets create a urls.py in our app Hokage/urls.py with following code
from django.urls import path
from . import views
urlpatterns = [
path('', views.hello, name='Home-Page'),
]

We need to map Hokage/urls.py with the Project HiddenLeaf/urls.py. To do so, lets modify the HiddenLeaf/urls.py with following

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('Hokage.urls'))

]

Now the final step, we need to tell our HiddenLeaf Project about our Hokage app. To do so, we have to pick class name from Hokage/apps.py file for me its HokageConfig.
Put it to HiddenLeaf/settings.py file under INSTALLED_APPS like follows

INSTALLED_APPS = [
'Hokage.apps.HokageConfig',
'django.contrib.admin',

All new app which you’re gonna create, you need to add those in settings.py under INSTALLED_APPS

That’s it folks, run following command to get your Django app up and running.

(djangoenv) ~$ python manage.py runserverWatching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).Run 'python manage.py migrate' to apply them.
March 07, 2021 - 06:27:07
Django version 3.1.7, using settings 'HiddenLeaf.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

I know I know, I’ve already put the code on GitHub, Django_App_HiddenLeaf

Again, I left some code unexplained so I know you will come back to this blog again ;). Comment on all your queries here and I’ll be your google.

Tata, talk to you later, Happy day o/

--

--

Saurabh Dattatray Thakre

Who Am I ? Couldn't fill up only 160 words to explain that :shrug ! Come on, go and read my blogs to know more o/