Tek:Cited

Misadventures in Coding

'Django-rundevserver' - a Django extension

Post Header Image
A Django addon library for development time, to run a custom server and port, also optionally forcing DEBUG mode.
Posted 26/04/2022 5:22 p.m. (Edited 30/06/2022 5:28 p.m.)

  

I have just created a new Django addon to fill a need I found and have released it, hoping that others will find it useful.

Since I code on a remote Linux server (a virtual machine on my network), the default Django 'runserver' command is no good to me - this defaults to binding to localhost, which doesn't work since I need it to bind to the external-facing interface instead. Yes, I can do as usual and run the following from the terminal:

python manage.py 0.0.0.0:8000

However, I'd like this to be more accessible and built-in; hence I wrote the simple Django addon 'django-rundevserver'.

A Django addon to provide a new management command that runs a development server with a custom host/port set in the applications settings.py file. You can also set the DEBUG variable here, allowing the default setting to be False for production.

Installation.

Install the addon using pip:

pip install django-rundevserver

You can also install from source if you have forked the Github repository (see the README for more details).

Usage.

Add this app to your Django settings.py INSTALLED_APPS like any other:

INSTALLED_APPS = [
    ...
    'rundevserver',
]

Now, just use rundevserver instead of runserver:

python manage.py rundevserver

By default, this will function exactly as the standard runserver except it automatically enables the DEBUG setting. In this way, you can leave DEBUG=False in your main settings.py file so it is correct for production but during development or debugging it will be set to True.

Configuration variables are set in the project settings.py and currently accepted values are as below. If all of these are unspecified, rundevserver will function exactly as runserver (except for the aforementioned DEBUG setting!):

# Chose which port to listen to, defaults to 8000 if unspecified
RDS_PORT = 8001

# Listen on all interfaces (ie 0.0.0.0) if true. Otherwise (or if unspecified),
# it will use the standard 127.0.0.1.
RDS_ALL_INTERFACES = True

# Force DEBUG mode if True OR unspecified. If you don't want DEBUG set in
# development for some reason, specify False here
RDS_DEBUG = True

I generally only set the RDS_ALL_INTERFACES variable to True and leave the others alone, but it's also possible to run simultaneous development and production servers on different ports, so you can change the RDS_PORT for this reason.

Development is not complete on this addon yet; I still have a few things I want to add. I also need to set up some documentation on readthedocs.org

Feel free to try this out and leave any comments below :)

Tags : custom library development django python
There are NO comments for this post so far.  Add a comment!