[Fixed]-Using custom permissions in view after log in django

1👍

Since all the models have one-to-one fields to the User model, you can use has_attr to check whether the user has a row for that model. You don’t have to create permissions.

if hasattr(request.user, 'customer'):
    return redirect('customer_home')

If you only have three customer types, then the if/elif/else is probably ok. As the number increases, you can change it to something like the following.

customer_types = [
    ('customer', 'customer_home'),
    ('clienttypea', 'ClientA_home'),
    ...
]

for field_name, redirect_url in customer_types:
    if hasattr(request.user, field_name):
        return redirect(redirect_url)
# fallback
return render(request, 'home.html')

Leave a comment