Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 367939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:46:34+00:00 2026-05-12T13:46:34+00:00

I am building a Django site framework which will power several independent sites, all

  • 0

I am building a Django site framework which will power several independent sites, all using the same apps but with their own templates. I plan to accomplish this by using multiple settings-files and setting a unique SITE_ID for them, like suggested in the Django docs for the django.contrib.sites framework

However, I don’t want a user from site A to be able to login on site B. After inspecting the user table created by syncdb, I can see no column which might restrict a user to a specific site. I have also tried to create a user, ‘bob’, on one site and then using the shell command to list all users on the other side and sure enough, bob shows up there.

How can I ensure all users are restricted to their respective sites?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T13:46:34+00:00Added an answer on May 12, 2026 at 1:46 pm

    The most compatible way to do this would be to create a user Profile model that includes a foreign key to the Site model, then write a custom auth backend that checks the current site against the value of that FK. Some sample code:

    Define your profile model, let’s say in app/models.py:

    from django.db import models
    from django.contrib.sites.models import Site
    from django.contrib.auth.models import User
    
    class UserProfile(models.Model):
        user = models.OneToOneField(User)
        site = models.ForeignKey(Site)
    

    Write your custom auth backend, inheriting from the default one, let’s say in app/auth_backend.py:

    from django.contrib.auth.backends import ModelBackend
    from django.contrib.sites.models import Site
    
    class SiteBackend(ModelBackend):
        def authenticate(self, **credentials):
            user_or_none = super(SiteBackend, self).authenticate(**credentials)
            if user_or_none and user_or_none.userprofile.site != Site.objects.get_current():
                user_or_none = None
            return user_or_none
    
        def get_user(self, user_id):
            try:
                return User.objects.get(
                    pk=user_id, userprofile__site=Site.objects.get_current())
            except User.DoesNotExist:
                return None
    

    This auth backend assumes all users have a profile; you’d need to make sure that your user creation/registration process always creates one.

    The overridden authenticate method ensures that a user can only login on the correct site. The get_user method is called on every request to fetch the user from the database based on the stored authentication information in the user’s session; our override ensures that a user can’t login on site A and then use that same session cookie to gain unauthorized access to site B. (Thanks to Jan Wrobel for pointing out the need to handle the latter case.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a Django application that will be using UTC for all time
I am building an article site using django. I've added many to many relation
I'm building a real estate site in Django and have a Home model, which
I'm building a site which will provide product information in two languages: English and
I'm building a site for a client using django. It's been hosted on shared
I'm building a small site framework for a set of sites that are likely
I am using django registration to handle um... registration on a site I'm building.
I'm building a Django app that will need SSL on all user-facing pages. On
I'm building a Django site and trying to use the request.is_ajax() function... But it's
I'm building a site using mongodb and django-nonrel. I've read in various places that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.