Skip to main content

A way of allowing subdomains to be served by the same project, and associating objects with particular subdomains.

Project description

A simple way of allowing subdomains to be served by the same project, and associating objects with particular subdomains.

Installation

  1. By default, it will work if you use 127.0.0.1.nip.io (a domain that points to localhost) on port 80. To use a different base domain/port, set the BASE_HOST and/or BASE_PORT variables.

  2. Add instances to your INSTALLED_APPS and migrate to get the Instance database table.

  3. Add Instance objects, with the label being the subdomain you wish to use. Optionally, associate users with these instances.

  4. Add instances.middleware.MultiInstanceMiddleware to your middleware; it must come after AuthenticationMiddleware. Now if you go to <subdomain>.<BASE_HOST>, request.instance will be set to the matching Instance object. If there’s a subdomain given but no match, it will redirect to BASE_HOST.

Requests to a subdomain will use your ROOT_URLCONF file; requests to the BASE_HOST will use ROOT_URLCONF_HOST or instances.urls by default (which just has one page that lists all instances)..

Instance edit form

In your ROOT_URLCONF, use a line like the following to have a page for editing the title and description of an instance:

url(r’^instance/edit$’, InstanceUpdate.as_view(), name=’instance-edit’)

Associating models

To have a model’s objects be associated with an instance, mix in InstanceMixin, and if you have a custom manager make it a subclass of InstanceManager. This adds an instance field, and provides a for_instance manager method to return all the objects in the given instance.

Mix in InstanceViewMixin to any display class-based view to restrict the default queryset to the request’s instance. Add InstanceFormMixin to any create/update view to store the current instance upon save, and allow editing only by those users associated with the instance. Remember to exclude instance from any model form, as it won’t be seen.

Running tests

pip install .
python runtests.py

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-subdomain-instances-2.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

django_subdomain_instances-2.0-py2.py3-none-any.whl (19.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-subdomain-instances-2.0.tar.gz.

File metadata

File hashes

Hashes for django-subdomain-instances-2.0.tar.gz
Algorithm Hash digest
SHA256 8599504cd3b7c8682e7673d71629a1712446550150dd060df6fd785dcea88237
MD5 0612b66eb39420c4cc78c855aaefc9ae
BLAKE2b-256 fa7ef926c63c3266934f21a71569c44b1f0bc1d8047849779786545ffa0e81d7

See more details on using hashes here.

File details

Details for the file django_subdomain_instances-2.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_subdomain_instances-2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d72b0f3fea6c92a6ee5e2af24add64cd1173d429435e617a35291407c6b8c84d
MD5 7d906e255abe8ba660c49b8b0883815e
BLAKE2b-256 b37c98032ebe31cfb956ea89c5a6aa44cf9e844716fbe9a23b7fe611367d45f1

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