So I am trying to build a website using Django where there are two separate user bases, one is user and the other is agent. Now I imported User from from django.contrib.auth.models import User and have a separate model for Agent. What I want is for there to be different login urls for users and agents. An agent can be an agent without having to be registered as a user. Now I have tried this code, and what this does is after trying to log in using agent credentials, it logs in and returns the correct html page but doesn't even authenticate if those credentials are right or wrong. Like the agent_login is still logging me in even after providing it with the wrong credentials. Now I want to know what the problem is and how to fix it so that it works properly.
My models.py:
from django.db import models
from django.contrib.auth.models import User
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
phonenumber = models.IntegerField()
def __str__(self):
return f'{self.user.username} Profile'
class Agent(models.Model):
name = models.CharField(max_length=100)
password = models.CharField(max_length=200)
image = models.ImageField(upload_to = 'Images/')
bio = models.TextField()
instagram = models.URLField(max_length=100)
twitter = models.URLField(max_length=100)
facebook = models.URLField(max_length=100)
linkedin = models.URLField(max_length=100)
is_featured = models.BooleanField(default = False)
slug = models.SlugField(default='')
def set_password(self, raw_password):
self.password = make_password(raw_password)
self.save()
def __str__(self):
return f'{self.name} Agent Profile'
My forms.py:
class AgentSignInForm(AuthenticationForm):
username = forms.CharField(max_length=100)
password = forms.PasswordInput()
def clean(self):
cleaned_data = super().clean()
username = cleaned_data.get('username')
password = cleaned_data.get('password')
return cleaned_data
My views.py:
def agent_login(request):
if request.method == 'POST':
form = AgentSignInForm(request.POST)
username = request.POST['username']
password = request.POST['password']
agent = AgentBackend.authenticate(request)
return render(request, 'users/agent_dashboard.html', {'agent': agent})
else:
form = AgentSignInForm()
return render(request, 'users/agent_login.html', {'form': form,})
class AgentDashboard(DetailView):
model = Agent
template_name = 'users/agent_dashboard.html'
slug_field = 'slug'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
agent = self.get_object()
properties = Property.objects.filter(agent=agent)
context['properties'] = properties
return context
My settings.py:
AUTHENTICATION_BACKENDS = [
"django.contrib.auth.backends.ModelBackend",
"users.backends.AgentBackend",
]
My backends.py:
from .models import Agent
from contextlib import suppress
from django.contrib.auth.backends import BaseBackend
from django.contrib.auth.hashers import check_password
class AgentBackend(BaseBackend):
def authenticate(request, username=None, password=None, **kwargs):
# Skip authentication attempts without credentials
if username is None or password is None:
return
# Get agent by name, check the password hash and return it if everything is ok
with suppress(Agent.DoesNotExist):
agent = Agent.objects.get(name=username)
if check_password(password, agent.password):
return agent
return None
def get_user(self, slug):
# Fetch and return agent if it exists
with suppress(Agent.DoesNotExist):
return Agent.objects.get(slug=slug)
return None
My admin.py:
class AgentAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
if 'password' in form.changed_data:
obj.set_password(form.cleaned_data['password'])
super().save_model(request, obj, form, change)
admin.site.register(Agent, AgentAdmin)
1)
Frist make sure your normal view functions to look like below. (expect login view and register view).
2)
Then,
1) You only authenticate(check if pass and u_name is correct) them, You should log them in. Change below code to,
. Like below -
You need to log them in(save them as logged in users in DB).
3)
Update backends.py code as below.
.
You maybe already logged in current browser, so try using new incognito browser window.