Back to overview

Django Unboxed

| 2 min read

Intro

Setup

$ pip install django==4.2

Generate Project

$ django-admin startproject app .

Running the Django Dev Server

$ python manage.py runserver

Add additional Apps to the project

$ python manage.py startapp website

Working on new app

Creating the new View

from django.http import HttpResponse          # Here we import the HttpResponse method
from django.shortcuts import render

# Create your views here.

def index(request):                           # All default views have the name index although not required it is the standard naming convention
    return HttpResponse('gikken.com algos')   # We then return a string for the moment

Adding routing to the website app

from django.urls import path    # We get the path method 
from . import views             # We import from the local dir to make sure we dont import an external views package

urlpatterns = [                 # All routing url files must have the name urlpatterns
    path('', views.index)       # We then point the route to the view function, note it is uncalled here, also empty string is the top root
]

Add top level routing

from django.contrib import admin
from django.urls import path, include     # We import the include method

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('website.urls'))     # We point the top level route to our new app urls file, this will pick up the urlpatterns object
]

Add new Page

Updating views.py

from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.

def index(request):
    return HttpResponse('gikken.com algos')

def full_page(request):                        # We add this new function
    return HttpResponse('Full Page Layout')    # Just return some simple text for now