Hi, I'm Harlin and welcome to my blog. I write about Python, Alfresco and other cheesy comestibles.

Python - How to Set Up a Simple HelloWorld Django App

The purpose of this post is to provide a set of instructions to create a simple helloworld app in Django. This app can then be used for testing purposes in other posts I've written.

Set up virtual environment:

# pyenv global 3.6.3
# pyenv virtualenv deploy_django
# pyenv local deploy_django

Set up Django helloworld project:

# pip install django
# django-admin.py startproject helloworld
# cd helloworld
# ./manage.py startapp web
# ./manage.py runserver

Check to make sure you can access it at http://localhost:8000/. You should see the familiar Django welcome page. Now go ahead and close the Django dev server (on Linux a control-C should work).

Open helloworld/settings.py and add the 'web' app:

INSTALLED_APPS = [
    ...
    'django.contrib.staticfiles',
    'web',
]

Save settings.py.

Open helloworld/urls.py and change the urls:

from django.conf.urls import url
from django.contrib import admin
from web.views import index


urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(
        regex='^$',
        view=index,
        name='web_index',
    )
]

Save urls.py. Open web/views.py and add:

from django.shortcuts import render


def index(request):
    return render(
        request,
        'index.html',
        {
            'message': 'Hello World!',
        }
    )

Save views.py. Create a new directory called web/templates.

# mkdir web/templates

Add a file in web/templates called index.html and add the following:

<!DOCTYPE html>
<html>
<head>
    <title>{{ message }}</title>
</head>
<body>
    <p>{{ message }}</p>
</body>
</html>

Let's start the Django dev server and navigate to http://localhost:8000/. You should see "Hello World!". At this point, we have a working Django helloworld project.

Any Comments, Always Welcome!