Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django is it better to check user.is_authenticated in views.py or in template?

I have a homepage, which I want to display a login form when user is not logged in or display a list of items belong to that user if he/she already logged in.

So far I came up with 2 methods:

  1. Check whether user is authenticated in views.py and render corresponding view (in my views.py):

    if request.user.is_authenticated():
        return render(request, 'items.html')
    else
        return render(request, 'login.html')
    
  2. Check directly in template and generate corresponding HTML for each case (in my index.html):

    {% if user.is_authenticated %}
        HTML for my items list
    {% else %}
        HTML for my login form
    {% endif %}
    

Question

So which method is better for handling this? Are those methods differ much in performance? Is there any standard that we should handling these in views.py or in template itself?

like image 583
hoan Avatar asked Sep 16 '15 06:09

hoan


People also ask

How check user authenticate or not in Django?

Check the Logged in User in Views in Django We can use request. user. is_authenticated to check if the user is logged in or not. If the user is logged in, it will return True .

What is user Is_authenticated in Django?

is_authenticated which is always False ). This is a way to tell if the user has been authenticated. This does not imply any permissions and doesn't check if the user is active or has a valid session.


2 Answers

I don't think there is a big performance difference. What's most important is how much you should stick to MVC pattern.

A template is meant to just display some sort of data that the view provides. Any kind of logic like deciding what kind of data to show based on requester's state should always be implemented by the view. Thus, you should move your logic into view function for the cleanness of your design.

like image 115
Ozgur Vatansever Avatar answered Sep 23 '22 10:09

Ozgur Vatansever


TL;DR

Logic should be in your python code, not your template as much as possible. Due to maintenance and future-proof reasons.

Elaborate

  • Code quality: you can test your business logic when it's in your python not when it's in templates. The former improve your code quality and your value as a developer ;
  • Future-proof: you don't know which technology your application is going to use in the future, so avoiding tech-mingling will help you when upgrading it (you will be able to upgrade at different pace).
  • Separation of concerns principles: do you want a code that is a spaghetti plate, where you can't refactor a thing without impacting ten others?
  • Code legacy: you don't know who is going to work on your code neither which code you're going to work on. Don't make it hard for them (it would probably be your future self) ;
  • Clean code: that express itself in a single dialect is always better that mixing languages ;
  • Knowledge scope: front-end is often the responsibility of people with low programming skills (HTML/CSS are declarative) and you don't want them to mess with your business logic.
like image 36
Édouard Lopez Avatar answered Sep 22 '22 10:09

Édouard Lopez