GithubHelp home page GithubHelp logo

risent / django-subdomains Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tkaemming/django-subdomains

1.0 1.0 0.0 84 KB

Subdomain tools for the Django framework, including subdomain-specific URL routing.

django-subdomains's Introduction

django-subdomains

Installation

  1. Add subdomains.middleware.SubdomainURLRoutingMiddleware to your MIDDLEWARE_CLASSES in your Django settings file.
  2. Set up your SUBDOMAIN_URLCONFS dictionary in your Django settings file. (See the example below.)
  3. Make sure that you've set up your SITE_ID in your Django settings file, and that the domain attribute corresponds to the proper domain name.
  4. ???
  5. Profit!

Example Configuration

# This is the URL that will be loaded for any subdomain that is not listed
# in SUBDOMAIN_URLCONFS. If you're going use wildcard subdomains, this will
# correspond to the wildcarded subdomain. 
# For example, 'accountname.mysite.com' will load the ROOT_URLCONF, since 
# it is not defined in SUBDOMAIN_URLCONFS.
ROOT_URLCONF = 'myproject.urls.account'

SUBDOMAIN_URLCONFS = {
    # The format for these is 'subdomain': 'urlconf'
    None: 'myproject.urls.frontend',
    'www': 'myproject.urls.frontend',
    'api': 'myproject.urls.api',
}

Usage Notes

The subdomain is also added to the request, so you can change view logic, depending on it's value, like so:

def user_profile(request):
    try:
        # Retrieve the user account associated with the current subdomain.
        user = User.objects.get(username=request.subdomain)
    except User.DoesNotExist:
        # No user matches the current subdomain, so return a generic 404.
        raise Http404

Adding Functionality

You can also subclass SubdomainURLRoutingMiddleware if you'd like, to associate requests with user accounts based on subdomain, etc.

Settings

USE_SUBDOMAIN_EXCEPTION

If USE_SUBDOMAIN_EXCEPTION is set to True, an subdomains.exceptions.IncorrectSiteException will be raised if the domain name does not match the django.contrib.sites.models.Site instance specified by your SITE_ID. This setting defaults to False, and will instead throw a warning that will not prevent your application from continuing if the Site is incorrect.

REMOVE_WWW_FROM_SUBDOMAIN

If REMOVE_WWW_FROM_SUBDOMAIN is set to True, Site models will have the "www." portion of stripped before processing. This allows for "www.example.com" to be used in the Site's domain attribute, without subdomains being resolved to "___.www.example.com".

django-subdomains's People

Contributors

jezdez avatar tkaemming avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.