DEV Community

Cover image for Django Hello World!
Babak Nikanjam
Babak Nikanjam

Posted on • Updated on • Originally published at bnik.org

Django Hello World!

Django is a secure, tested and battery included Python web frame work comes with a powerful ORM and migration, a different approach of developing web apps (solving the web request-response problem) called Model-View-Template in contrast with MVC, and often considered very opinionated.

One may think it's clunky and unnecessarily complicated for simple tasks. In the last US DjangoCon in September 2019, Carlton Gibson a respected friendly core Django veteran, beautifully shows that at core all web frame works are trying to solve the same problem and it is possible that Django too can be scraped down to a skinny single Python script for a Hello World! But Django is evolved to be what it is today to solve real world problems for rapid development and pragmatic design.

# app.py
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
from django.http import HttpResponse
from django.urls import path

settings.configure(
    ROOT_URLCONF = __name__,
)

def hello_world(request):
    return HttpResponse('Hello World!')

urlpatterns = [
    path('', hello_world)
]

webapp = WSGIHandler()

Now, lets install Gunicorn HTTP WSGI web server in a new environment to catch and hand over any request coming its way to our web app!

$ Pipenv install gunicorn
$ Pipenv shell

Let's run and introduce the web server to our minimalist web app.

$ gunicorn app:webapp

In the above command, the left of the colon is the python script file name our code lives in and the right of the colon is the name of the
function calling our web app.

Now navigate to http://127.0.0.1:8000/ for a nice greeting!

Carlton Gibson talk at DjangoCon 2019 Django as a Micro-Framework

Top comments (1)

Collapse
 
jeffshomali profile image
Jeff Shomali

Great Article