简体   繁体   English

Django:1个针对不同用户的视图?

[英]Django: 1 view for different users?

In my project, I'd like to split pages between a side menu and a content page. 在我的项目中,我想在侧边菜单和内容页面之间拆分页面。 I'd like to show a sidemenu which is different for user types (there are 5 user types, so there will be 5 sidemenus). 我想展示一个针对用户类型不同的补充菜单(有5个用户类型,因此会有5个补充菜单)。 But sometimes, the content page to display can be the same for these 5 user types. 但是有时候,这5个用户类型要显示的内容页面可以相同。

I was looking for a "pattern" to handle this, I thought about one, but never seen it in another project. 我想到了一个“模式”来解决这个问题,但我从未在另一个项目中看到它。 I'd like you to tell me if it's a good or bad practice: 我想告诉我这是好事还是坏事:

  • 1 URI for all user types (eg. /myapp/display_home) 所有用户类型的1个URI(例如/ myapp / display_home)
  • In views.py, 1 view for all user types. 在views.py中,为所有用户类型提供1个视图。 Into the view, I try to know if the user is logged in, and then which type of user we are dealing with. 在视图中,我尝试知道用户是否已登录,然后知道我们正在处理哪种类型的用户。 Then, redirect to the HTML, passing in context a variable containing the user type. 然后,重定向到HTML,并在上下文中传递包含用户类型的变量。
  • In the end, in the template, something like this: 最后,在模板中,如下所示:

Example of template : 模板示例

{% if user_type == "anonymous" %}
    <!-- display anonymous sidemenu here -->
{% elif user_type == "landlord" %}
    <!-- display landlord sidemenu here -->
{% endif %}

<-- Here is the same content for all user types -->

Do you have any suggestion? 你有什么建议吗? another pattern to propose? 提出另一种模式?

Check this answer for discussion on user.is_authenticated . 检查此答案以获取有关user.is_authenticated讨论。 You can also check within the view to see who the user is ( see here ) and modify the context being sent to the template accordingly. 您也可以在视图中检查用户的身份( 请参阅此处 ),并相应地修改发送到模板的上下文。 Another option would be to modify your view to simply show different types of users different templates. 另一个选择是修改视图以简单地向不同类型的用户显示不同的模板。

Lots of ways to accomplish this, not sure if there's a "correct" one. 有很多方法可以做到这一点,不确定是否有一个“正确的”方法。


Your comment made me realize that this is exactly what the Groups feature of the auth package is for. 您的评论使我意识到,这正是auth软件包的Groups功能所针对的。 You can assign users to groups, query whether they're a member of the group, and then show content based on that. 您可以将用户分配给组,查询他们是否是组的成员,然后根据该内容显示内容。 I apologize that I don't have time to write out sample code, but there's tons of stuff here on SO about it ; 抱歉,我没有时间写出示例代码,但是SO上有很多关于它的内容 hopefully that's a good starting point for you. 希望这对您来说是一个很好的起点。

I don't know if it is good style. 我不知道这是不是很好的风格。 But you could use 但是你可以使用

{% include user_type_sidemenu_template with arg1=... %}

in your template and provide different templates for the sidemenu for each user_type . 在您的模板中,并为每个user_type的侧面菜单提供不同的模板。 You can then set the template variable user_type_sidemenu_template to the corresponding template name in your view. 然后,可以在视图中将模板变量user_type_sidemenu_template设置为相应的模板名称。

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

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