Django User-model - Softtalks

Featured Post

Should you use React Native to build your startup’s mobile app?

Should you use React Native to build your startup’s mobile app? Flexible application improvement is a jumbled system. It requires ...

Friday, April 24, 2020

Django User-model

         Django User-model

Today's Topics in the Django User model I am going to talk about In the Django user model, in Brief, I will start by talking about Django.

Django is a framework

Django is a framework written in python, it's a backend framework which makes the life of python developer a-lot easier by hiding all the abstraction the developer would have to deal with if it wasn't for Django, Django is used by a lot of famous companies and start-up's like Quora(my favorite one in the list), Youtube, Instagram, Pinterest, etc.. and the list goes on, Django kind of prohibit re-inventing the wheel which means it doesn't encourage much of doing things from Scratch.

MVT (Model View Template)

Django is an MVT (Model View Template) which is similar to the MVC(Model View Controller) in java where the Model is the Database of the Django App and View is the Function which joins the backend part(Database) with the Front-end part(Template).

ORM(Object Relational Mapping)

Django also supports ORM(Object Relational Mapping) which means that you don't have to write raw SQL when dealing with queries for example if you're looking for a record with name = "Steven" in a Table called Student instead of writing raw SQL a statement like select * from Student where name = "Steven" you can write in Django view Name = Student.Objects.get(name = "Steven") which is equivalent to the select statement in raw SQL you can read more about making queries in Django from the Fun to Read Section.

Django Authentication Model

Now to our main topic which is User-class where User is a class in Django Authentication Model where user represents the user navigating the website or the admin who is dealing with the Admin page Django has Authentication page where Admin can review users registered on the Website and can check the username and the password but the password is hashed so you're not going to see user password and the Tables in the DataBase and the Records inserted in the Tables.

Note:- The admin page can be customized to change it's UI, the Database, and the Table is not added to the Admin page by Default it has to be registered in the Admin class which is created by default.

The user class has the following attributes:-


and the following Methods:-
is_Authenticated() to check if the user has an account or not
login() to log the user in
logoff() to log the user out
save() to save the user in the user database
getusername() return Username of the user
getfirstname() return the FirstName of the user
getlastname() return the LastName of the user

example in Code:-
def Blog(request):
if(not request.user.is_authenticated):
return HttpResponse("Blog Can't be accessed without logging in, you can log in here")
return render(request,"Algorthims/Blog.html",context)
Demo Model Files
from django.db import models

# Create your models here.
class executive(models.Model):
    img = models.ImageField(upload_to='pics', default=" ")
    facebook=models.CharField(max_length=150, default=" ")
    twitter=models.CharField(max_length=150 , default=" ")
    instagram = models.CharField(max_length=150, default=" ")
def __str__(self):
    return self.img+self.profile_name+self.designation+self.facebook+self.twitter+self.instagram

the following code checks if a user has an account or not if the user doesn't have an account then it will return the following error "Blog Can't be accessed without logging in, you can log in here" else it will render the Blog containing another post if you're interested to check the whole project on Github you can find it in the Fun to Read

No comments:

Post a Comment