I create a website where there is a normal user and admin. They both have different log in system.But the problem is when a user logged in as a user, he also logged in into admin page. Also when a admin logged in, he also logged in into user page.
def userlogin(request):
error = ""
if request.method == 'POST':
u = request.POST['emailid']
p = request.POST['pwd']
user = authenticate(username=u, password=p)
try:
if user:
login(request, user)
error = "no"
return redirect(profile)
else:
error = "yes"
except:
error = "yes"
return render(request, 'login.html', locals())
def login_admin(request):
error = ""
if request.method == 'POST':
u = request.POST['uname']
p = request.POST['pwd']
user = authenticate(username=u, password=p)
try:
if user.is_staff:
login(request, user)
error = "no"
else:
error ="yes"
except:
error = "yes"
return render(request,'login_admin.html', locals())
This model is used for normal user signup
class Signup(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
contact = models.CharField(max_length=10)
branch = models.CharField(max_length=30)
role = models.CharField(max_length=15)
username = models.CharField(max_length=15, unique=True)
image = models.ImageField(upload_to="images/img", default="")
upvotesuser = models.IntegerField(default=0)
I want to achive different authentication system for user and also for admin.
CodePudding user response:
Make some roles explicitly in SignUp model as follows as Django provides that too:
- admin
- staff
- simple user/regular user
Define the role of each user in the SignUp model. If a regular user is logged in it will definitely be filtered from the signUp model and that will return him/her as a regular/simple user.
CodePudding user response:
You don't need to create another model for signup You can easily check the current user permissions E.g
- is_admin
- is_staff
- is_superuser Then redirect depending on the permisions