Skip to main content

Automatically run the Django dev server on a particular hostname

Project description

Run Server on Hostname

My problem: I have multiple Django projects, but when I use the dev server (./manage.py runserver), they clobber each others' cookies since they run at 127.0.0.1 by default.

The manual solution: I set my hostfile (/etc/hosts) up so that each project has its own pseudo-hostname. Usually this is "projectname.localhost". But that means I have to remember which name I gave to each project and remember to pass it every time I ./manage runserver.

This package lets me define a desired dev server hostname in my Django settings, then runserver will respect it by default. If for any reason, I want to override that setting on a one-off basis, that still works.

Installation

  1. Install the django-runserveronhostname package using whatever package manager you prefer (I use uv).
  2. Add runserveronhostname to INSTALLED_APPS in your Django settings.
  3. Add a setting RUNSERVER_ON = 'myname.whatever:8000' (you must include a port number). This could also be used to bind to a particular IP address (RUNSERVER_ON = '0.0.0.0:8000') if you prefer.

Important note: this package doesn't do anything about making sure you can actually bind to the requested name or IP address. You will need to manually add an entry to your hostfile, DNS, etc. See the new hostfile command for some help with that.

Usage

When you run the dev server (./manage.py runserver, django-admin runserver, etc.), if you have RUNSERVER_ON defined, it'll use that. This works with any runserver implementation:

  • naive implementation in Django
  • staticfiles implementation in Django
  • daphne's runserver override

hostfile command

You can run ./manage.py hostfile to see whether the hostname you require is listed in your system host file. Right now this only works directly on Linux and macOS, but if you know where your system's hostfile lives, you can point to it with ./manage hostfile --file <path/to/hosts>.

There's another option you can pass, --write, which will spit back the hostfile it finds and add your project's RUNSERVER_ON hostname if it doesn't already appear. If you wanted to be really bold, you could do something like:

# I don't really recommend this, it's not battle-tested
% sudo ./manage.py hostfile --write >/etc/hosts 

Contributing

While I don't mind contributions, I don't really expect any, either. So I don't have great instructions here. It's a really small package, and you can probably stand up a little test app locally with this installed in editable mode.

Running tests

At time of writing, we have 100% test coverage. That's not necessarily a strict rule, but it sure feels good to have a second set of eyes watching each line.

% uv sync --extra test && uv run pytest

Changelog

See what's changed in each version in the changelog.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_runserveronhostname-0.3.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_runserveronhostname-0.3.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file django_runserveronhostname-0.3.0.tar.gz.

File metadata

  • Download URL: django_runserveronhostname-0.3.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_runserveronhostname-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c599096f0893b6836970a1cf18fdeccce1e70b74c35228d67a17ef8ab77f255a
MD5 ac54176b1e5c9e8ea14542226ce04e60
BLAKE2b-256 201b1f8468f534ff578d81785f14605b5b4cb12037aaad007f2384f2c3fe9163

See more details on using hashes here.

File details

Details for the file django_runserveronhostname-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: django_runserveronhostname-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for django_runserveronhostname-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 743c9ff553cd28015200167145238d66fd7e1d05f0b81176e278a4b8b2a82951
MD5 c06708790580a7a2c045ced1f840408c
BLAKE2b-256 7cf8ebae9ff36fc783926c7c85acf2ea557ddc0e76b9592bcae74c73ff1b8d08

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page