After login I want to access User details when ever I want (Particularly in navigation bar).
I did not use user model provided in django. I created my own model like this for authentication.
My database is stored in mysql on phpmyadmin(Xampp).
AdminUser Model
class adminUser(models.Model):
username=models.CharField(max_length=50)
firstname=models.CharField(max_length=50)
department=models.CharField(max_length=50)
name=models.CharField(max_length=50)
mail=models.CharField(max_length=50)
id=models.IntegerField(primary_key=True)
password=models.CharField(max_length=200)
class Meta:
db_table="admin_users"
admin_users.py
def login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
if username is not None and password is not None:
user=adminUser.objects.get(username=username)
hashed_password = user.password
is_check = bcrypt.checkpw(password.encode('utf8'),hashed_password.encode('utf8'))
print(is_check)
if is_check==True:
return redirect(reverse('faq_index'))
else:
return render(request,'AdminUsers/login.html')
return render(request,'AdminUsers/login.html')
During the login User Details can be only access by login function but I want to access user details in all pages for navigation bar and also want to find out whether user is authenticated or not. As I am not using User Model defined in django so I can not use user.is_authenticated(). So How do I do this?
2
Answers
First, inherit your user model from
AbstractBaseUser
. It gives you some features likeis_authenticated
,set_password
and etcThen define your custom user model as
AUTH_USER_MODEL
in your settings.pyAfter these, django treats to your custom user as its default user
models.py
settings.py
Docs in https://docs.djangoproject.com/en/3.2/topics/auth/customizing/
I’m gonna recommend you take a step back, and extend the built-in User model instead of creating your own.
As mentioned by @Amin, you can extend the
AbstractBaseUser
class to create a custom User model, and override theAUTH_USER_MODEL
insettings.py
.However, you can easily add a
Profile
model that has a one-to-one relationship with the built-in User model that does not require any overrides (which method is better escapes me at the moment, but I use the following method without issue):Then, in the same folder as contains your
models.py
, create a file calledsignals.py
, and add the following code:This will trigger the creation of a new
Profile
object whenever aUser
object is created.Then, add the following to the same folder’s
apps.py
(Assuming the app’s name isusers
… make it match the name of the app that contains yourProfile
model):Now, after you run your migrations, you will be able to use the built-in
User
model, and add additional fields to it as desired in theProfile
model.The only downside is that it will require a little extra work if you want to modify the
User
model and theProfile
model on the same page (ie. you won’t just be able to specify the model name in anUpdateView
).I don’t remember where I found this method. I believe it was adapted from this page on simpleisbetterthancomplex.com, but I’m not 100% sure.
At any rate, Django automatically creates a primary key
id
for you, so you probably don’t need to manually define that in your model.