Skip to content

Django

The Python-based Django framework is used by:

instagram-new dropbox spotify--v1 youtube-play pinterest--v1 bitbucket

Upcoming: - configuring URLs - database model creation - dynamic data in templates - admin panel

Setup new Django project

installation:
pip install django

start a new Django project
django-admin startproject workoutplans .

install the testing framework (interface between pytest and django, with additional features)
pip install pytest-django

start a new Django app
python manage.py startapp workoutapp

cd - python manage.py startapp workoutapp

add html template:
mkdir templates && cd templates

echo welcome page workout generator > index.html

Navigate to project/myproject/settings.py:

add
import os

update TEMPLATES[“DIRS”] with:
os.path.join(BASE_DIR, "workoutapp/templates")

under INSTALLED_APPS, add an entry for the name of the new django app that was just created.

Views

A view is a function that receives a request and returns a response.

example, workoutapp/views.py:

from django.shortcuts import render, HttpResponse


def home(request):
    return HttpResponse("Welcome to the workoutapp")

then in workoutapp/urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path("", views.home, name="home")
]

then in workoutplans/urls.py:

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path("workoutapp/", include("workoutapp.urls")),
]

Development server

Run app
python manage.py runserver

run Django app with the development server
python manage.py runserver (runs at local port 8000)

if you wish to change port:
python manage.py runserver 8080

then visit
http://127.0.0.1:8000/workoutapp/welcome_view/

Files

the special configuration files asgi.py and wsgi.py allow django to communicate with the web server.

the settings.py file:
to install django apps, plugins, middleware, or modify database engines.

the urls.py file: configure different url routes, that can then be directed / routed to different Django applications.

the manage.py file acts as a command line tool, and can be used for running the django development server, do database migrations, create users and more.

the models.py file: here the database models are placed.

the admin.py file: register database models so they can be viewed on the admin panel.

the tests.py file: adding automated tewst cases.

the views.py file: create different views or routes, that can be accessed from the website.

templates

example, create templates/base.html, populate using emmet, then add blocks, e.g.

<title>{% block title %}workoutplans{% endblock %}</title>

which uses the jinja templating engine, which allows displaying dynamic data.

re-using template in templates/home.html:

{% extends "base.html" %} {% block title %} Home {% endblock %}
{% block content %}
<p>welcome to the workoutplans home page</p>
{% endblock %}

CSS

  • bootstrap
  • tailwind

Run Django app with Docker

docker build --tag python-django .
docker run --publish 8000:8000 python-django