Django

Creating Custom Modules for the ARCsoft Library

by Lee Napthine, 9 December 2024

At ARCsoft we have begun building a repository of custom Helpers libraries that will be included in future projects. First on the docket was looking at several giant UI test code blocks in Django. These often span dozens of modules and thousands of lines of code. Quite common throughout was the use of repetitive and non-descriptive JsonResponse calls. Let’s walk through how we compartmentalized these calls into a more cohesive set of custom JsonResponse classes and how we added them to our repository of custom methods that we will use here at ARCsoft. Along the way we will document the setup of the Helpers repository and the essential inclusions to a project for the package to be able to be used, imported, and published.

Django Cheatsheet

by Nick Wurzer, 26 October 2023

Below will be a list of specific Django features with a short description of how they are used.

Ingestion Made Easy!

by Priya Srinivasan, 11 August 2023

ZooDB already possessed an ingestion script, capable of efficiently processing hundreds of rows of zooarchaeological bone data. This script was traditionally executed from the command line by the developer. However, to enhance the user experience and streamline the ingestion process, we created a user-facing feature to allow the researchers to upload and process their data via the web application. In this article, I describe the crucial parts of this feature and how it was developed.

Displaying Dynamic Data with a Django View Function

by Priya Srinivasan, 31 May 2023

Django is a powerful Python web framework and it provides a straightforward way to display dynamic data in web applications. It has a template system for displaying content on the web. Templates can be combined with view functions to display data dynamically. This article will describe how to use a Django view function to retrieve and display dynamic data.

Step 1: Define a View Function

Initially, we need to create a view function as follows, that can handle the request and return the response containing the dynamic data. In Django applications, view functions are conventionally defined in the views.py file.

Writing unit tests for a Django web application

by Priya Srinivasan, 30 April 2023

As websites expand, manual testing becomes more challenging. Interactions between components can lead to errors in other areas, and more changes are needed to ensure everything continues to function correctly. To address these issues, automated tests can be used to run every time changes are made, ensuring reliable and consistent testing. This blogpost demonstrates how to automate unit testing of your website using Django’s test framework.

Strapper real-time deployment log

by Archie To, 18 April 2023

This real-time deployment log feature was implemented by following a tutorial on Django Channels.

All of the code below can be found in the Strapper GitLab repository

Setup

Install daphne and channels:

$ pip install -U daphne channels["daphne"]

In app_starter/app_starter/settings.py:

INSTALLED_APPS = [
    "daphne",
    ...,
]

ASGI_APPLICATION = "app_starter.asgi.application"

Configure ASGI app

Since wsgi doesn’t support long-lived connections, we will have to serve our app as an asgi app. In app_starter/app_starter/asgi.py:


"""
ASGI config for app_starter project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/
"""

import os

from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.security.websocket import AllowedHostsOriginValidator
from django.core.asgi import get_asgi_application

from normal_users.routing import websocket_urlpatterns

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app_starter.settings')
# Initialize Django ASGI application early to ensure the AppRegistry
# is populated before importing code that may import ORM models.
django_asgi_app = get_asgi_application()

application = ProtocolTypeRouter({
    "http": django_asgi_app,
    "websocket": AllowedHostsOriginValidator(
        AuthMiddlewareStack(URLRouter(websocket_urlpatterns))
    ),
})

Here we are using ProtocolTypeRouter as the root application, which defines what asgi application to serve depending on the protocol type:

Django development tips

by Archie To, 31 March 2023

This article contains tips that I wish I knew when starting out with Django. Following these tips will help your development process with Django a lot faster and make your code way cleaner. This article assumes you already have basic Django knowledge. (Django’s full documentation can be found here.)

Note: All the code examples below assume there is no bug within each code, all dependencies for each code have been set up and each code runs correctly. The purpose of this tutorial is not to teach you Django code, but to show you the available options that Django offers to developers.

Researcher Contact Database

by Archie To, 28 February 2023

Introduction - Existing Problem

RCS team member works with researchers and faculty members on multiple projects everyday. Each of these projects involve different people (inside and outside of the university), are at different stages and require different actions. It has become extremely difficult for RCS team members to keep track of these information as the number of projects that they work on rises. Therefore, there needs to be a solution.

Django App Starter Template

by Archie To, 20 January 2023

Django App Starter Template: https://gitlab.com/uvic-arcsoft/strap/app-starter-django/-/tree/archie

Introduction - Existing Problem

Django is a very popular web framework known by many Python developers. RCS ARC Software Team decided to use this framework as the main technology to develop multiple web applications, which will eventually serve the need of researchers, falcuty members and employees in the University of Victoria. All of these applications have some identical functionalities such as authenticating users by HTTP request headers, displaying separated pages for normal users and admin users, and providing an about page for web developers. These similar functionalities force engineers to rewrite the same code everytime they start a new project. This is obviously a time-consuming and boring process.

Converting Flask App Starter Into Django

by Archie To, 28 October 2022

My goal in this project is to convert a Flask skeleton code into Django.

The blog will summarize my takes on Flask and Django.

Authentication transformation

1. Authentication system in the Flask code:

Flask code authenticates users by checking the HTTP request header “X-Forwarded-User”. There are four levels of authentication and authorization in the Flask code: