Codementor Events

Django User Model

Published Jun 10, 2020
Django User Model

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

Django is framework written in python it's a backend framework which make 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 favourite one in the list), Youtube, Instagram, Pinterest, etc.. and the list goes on, Django kind of prohibits re-inventing the wheel which means it doesn't encourage much of doing things from Scratch.

Django follows an MVT (Model View Template) Design Paradigm 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) and the Template is the front-end part(raw html or css or any other front-end framework(react, angular).

Django also support 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 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.

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 have Authentication page where Admin can review users registered on the Website and can check username and the password but the password is hashed so you're not going to see user password and the Tables in the Data Base and the Records inserted in the Tables.

Note :- The admin page can be customised 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 :-

Username
FirstName
LastName
Password

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)

the following code check if a user have 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 other post if you're interested to check the whole project on Github you can find it in the Fun to Read

Fun to Read :-
https://docs.djangoproject.com/en/3.0/topics/db/queries/
https://github.com/12mohaned/Algo_Digital

Discover and read more posts from mohaned
get started
post commentsBe the first to share your opinion
Show more replies