Django on Domino

andrew.colemanandrew.coleman Member, Moderator, Domino Posts: 3 mod
edited June 16 in How-To Guides

Django is a Python-based web framework that is powerful and flexible. Domino is known to support Flask and Dash for publishing Python apps, but we are happy to get a basic implementation of Django working on Domino as well.

A word of caution before continuing, the Django application that I have tested is incredibly basic, so more complex Django applications may be missing some additional configuration or they may not work at all on Domino.

Let's say I have a Django starter project, named "My Site", that was generated via:

django-admin startproject mysite

I can then upload that Django project to my Domino project named Django-Test via either the Domino CLI or the file upload tool in the UI. From there, we now have a Django "hello-world" project in our Domino project, that will allow us to get started on configuring the Django application to be able to run on Domino.

First thing we need to do is open the settings.py file in Django-Test/mysite/mysite/settings.py to make some changes to allow Domino to interact with our Django application. We need to add the following lines to the end of that file:

ALLOWED_HOSTS = ['*']
X_FRAME_OPTIONS = 'ALLOWALL'
XS_SHARING_ALLOWED_METHODS = ['POST','GET','OPTIONS', 'PUT', 'DELETE']

These options are meant to open up some configuration settings in Django in order to allow traffic to be routed from Domino into the Django app. These settings could be modified to be a bit more secure, but for the purpose of this example, we will keep them fairly liberal.

We are going to need a few tools to get this running on Domino, so we are going to install them in the Domino Environment by adding the required libraries to the Environment's Dockerfile:

RUN pip install Django postgres gunicorn

And we are going to use NGINX to route traffic to our Django app from Domino. So we need to install that into the Domino Environment as well, so while editing the Environment configuration, add the following lines to the Advanced -> Pre Setup Script:

apt update
apt install nginx -y

Now we can save the environment and allow that to build to be put in use for our project.

Back to our project - we need to add a configuration file for NGINX. So let's create a file named default in the root of our project:

  server {
	listen 8888 default_server;
	listen [::]:8888 default_server;
 
	client_max_body_size 1024M;
 
	root /var/www/html;
 
	index index.html index.htm index.nginx-debian.html;
 
	server_name _;
 
	location / {
            proxy_pass         http://127.0.0.1:55850/;
            proxy_set_header   Host $host;
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Host $server_name;
            proxy_read_timeout 86400;
  }
}

This file tells NGINX what to do when its running and receives traffic. A couple things of note in here, `listen 8888 default_server;` tells that NGINX will be listening on port 8888, which is the default app port that Domino will route traffic to. Secondly, `proxy_pass http://127.0.0.1:55850/;` is saying that when NGINX receives a request, forward it to this address, which is where our Django app is going to be running. If you are interested learning about some of the other configuration fields, then head over to the NGINX documentation.

Next, Domino published apps need to have an app.sh in the root of the project in order to tell Domino how the app should be published. Let's go ahead and create that file now, with the following contents:

#!/usr/bin/env bash
cd $DOMINO_WORKING_DIR
sudo cp $DOMINO_WORKING_DIR/default /etc/nginx/sites-enabled/
sudo service nginx start
 
cd /mnt/mysite
gunicorn -b 0.0.0.0:55850 mysite.wsgi:application

The shell script above is responsible for starting NGINX and copying the config we made to the directory that NGINX expects it to be in, and then moves to start our app using Gunicorn, which is an HTTP server for Python.

Now we should be in a position where we can publish the app from Domino and see our Django site running!

In conclusion, this is a quick way to get Django up and running with Domino. To do some more complex workloads with Django, you may need to do more than this, but hopefully this is a good starting point! For more information on publishing apps in Domino, check out our documentation - https://docs.dominodatalab.com/en/4.2/reference/publish/apps/index.html

Andrew

Field Engineer @ Domino

Sign In or Register to comment.