简体   繁体   中英

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?

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.

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.

It depends on your html. If you want to change only a little part of your code based on the condition, check inside a template:

{% if user.is_authenticated %}
    <h3>Welcome</h3>
{% else %}
    <a href="/path/to/login">Login</a>
{% endif %}

But if items.html and login.html are different and big templates, you should definitely do the login inside your view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM