简体   繁体   English

Django最好在views.py或模板中检查user.is_authenticated吗?

[英]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 ): 检查用户是否在views.py中进行了身份验证并呈现相应的视图(在我的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 ): 直接检查模板并为每个案例生成相应的HTML(在我的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? 我们应该在views.py或模板本身处理这些标准吗?

I don't think there is a big performance difference. 我不认为有很大的性能差异。 What's most important is how much you should stick to MVC pattern. 最重要的是你应该坚持多少MVC模式。

A template is meant to just display some sort of data that the view provides. template仅用于显示view提供的某种数据。 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 TL; DR

Logic should be in your python code, not your template as much as possible. 逻辑应该在你的python代码中,而不是你的模板尽可能多。 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. 代码质量 :您可以在python中测试业务逻辑 ,而不是在模板中。 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. 知识范围 :前端通常是低编程技能的人(HTML / CSS是声明性的)的责任,并且您不希望它们弄乱您的业务逻辑。

It depends on your html. 这取决于你的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. 但是如果items.htmllogin.html是不同的大模板,那么你一定要在视图中进行登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果Django使用user.is_authenticated,则模板无法正确呈现 - Template not rendering properly with if user.is_authenticated for Django 如何在views.py中进行request.user.is_authenticated()? Django的 - How to request.user.is_authenticated() in views.py? Django Django user.is_authenticated 不同视图返回不同结果 - Django user.is_authenticated returns different results in different views 为什么 django 针对这种情况返回模板错误 {% if user.is_authenticated %}? - why django returns me a template Error for this case {% if user.is_authenticated %}? Django如果user.is_authenticated不起作用 - Django if user.is_authenticated not working 如果 user.is_authenticated 未登录用户 django - if user.is_authenticated not getting logged in user django Django allauth未检测到user.is_authenticated - Django allauth does not detect user.is_authenticated 为什么 user.is_authenticated 不起作用? django, LoginRequiredMixin - Why user.is_authenticated not working? django, LoginRequiredMixin 如何在不使用views.py的情况下让所有用户都进入django模板? - How to get all user in django template without using views.py? Django 在 UserCreationForm 之前检查 user.is_authenticated 以防止用户注册两次 - Django check user.is_authenticated before UserCreationForm to prevent a user to signup twice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM