Django different authentication for different URLs
I have two Django apps called frontend
and affiliates
like so:
django/
manage.py
frontend/
__init__.py
models.py
views.py
affiliate/
__init__.py
models.py
views.py
I want people to be able to have two accounts, they sign up for their frontend account at domain.com/signup/
and their affiliate account at domain.com/affilate/signup/
.
I've got no problem with sorting that out, but is it possible to have them logged into both accounts at the same time? If they visit and page on domain.com
they are authenticated with their frontend account, and if they visit any page on domain.com/affiliate/
they are authenticated on their affiliate account.
I have models for the accounts that look like this:
class Account(models.Model):
user = models.OneToOneField(User, db_index=True)
username = models.CharField(max_length=32, db_index=True, unique=True)
password = models.CharField(max_length=32)
email = models.CharField(max_length=128, db_index=True, unique=True)
For frontend.
class Affiliate(models.Model):
user = models.OneToOneField(User, db_index=True)
username = models.CharField(max_length=32, db_index=True, unique=True)
password = models.CharField(max_length=32)
email = models.CharField(max_length=128, db_index=True, unique=True)
For affiliates.
Is it possible to have two different sets of django users so the user = models.OneToOneField(User, db_index=True)
field relates to a different set of users for the frontend account than the affiliate accounts? Because I want people to be able to use the same usernames for both accounts. If they're both dipping into the same pool of django users I will get duplicate username errors. Thanks!
Instead of a OneToOne
between the User
model and the custom profile models, you could have a ForeignKey
to the User
instead. Then in the Account
views, you could have the check if hasattr(user, 'account')
and in the Affiliate
views, if hasattr(user, 'affiliate
).
Just make sure you don't allow more than one of each type of profile for each user object. You could do that either in the view, or the model. I'd say do it in both places.
Based on what you've told us, I'd say: create one model Account
for frontend users and use a ForeignKey
to associate that model to an Affiliate
model if needed.
That way, everyone's logging in with the same user accounts and you can lookup affiliate information for a user by accessing the Affiliate
model.