简体   繁体   English

如何通过 django 正确重定向到取决于不同用户组的页面?

[英]How can I redirect to a page depend on different user group correctly by django?

I am creating a feature to let the banned user (user without any group) go to a specific page banned_alert when they click the "post", because they are not allowed to do so.我正在创建一个功能,让被禁止的用户(没有任何组的用户)go 在他们单击“帖子”时访问特定的页面banned_alert ,因为他们不允许这样做。 But then when I test this feature, the Chrome shows This page isn't working | the IP redirected you too many times.但是当我测试此功能时,Chrome 显示This page isn't working | the IP redirected you too many times. This page isn't working | the IP redirected you too many times. Can somebody tell me how to do it correctly?有人可以告诉我如何正确地做到这一点吗? Did I miss any configuration?我错过了任何配置吗? Below is my code snippets.下面是我的代码片段。 Thank you for your time!感谢您的时间!

base.html: ( has_group function already works correctly somewhere else) base.html:( has_group function 已经在其他地方正常工作)

{% load get_group %}
{% if request.user|has_group:"mod" or request.user|has_group:"default" or user.is_staff %}
<a class="nav-link" href="/create-post">Post</a>
{% else %}
<a class="nav-link" href="/banned_alert">Post</a>
{% endif %}

banned_alert.html:禁止警报.html:

{% extends 'main/base.html' %}
{% block title %}Your account has been banned by Admin{% endblock %}
{% load crispy_forms_tags %}
{% block content %}
<h2>Please contact the Admin!</h2>
{% endblock %}

view.py视图.py

def banned_alert(request):
    return redirect('/banned_alert')

urls.py网址.py

urlpatterns = [
    path('', views.home, name='home'),
    path('home', views.home, name='home'),
    path('sign-up', views.sign_up, name='sign_up'),
    path('create-post', views.create_post, name='create_post'),
    path('banned_alert', views.banned_alert, name='banned_alert'),
]

You have a recursion:你有一个递归:

this is your path这是你的路

path('banned_alert', views.banned_alert, name='banned_alert'),

  1. User visit this banned_alert path,用户访问这个banned_alert路径,
  2. it redirects user to view banned_alert它重定向用户查看banned_alert
  3. Which redirects user back to url banned_alert它将用户重定向回banned_alert
  4. and it redirects back to view banned_alert它重定向回查看banned_alert
  5. reapeat N times (it has no ending)重复N次(没有结局)

best way to ban user is to create a flag in his model:禁止用户的最佳方法是在他的 model 中创建一个标志:

models.py

class User(AbstractUser):
    is_banned = models.BooleanField(default=False)

if you want to ban user , change user's field is_banned to true , and then in your template or view, check if user is banned or not, and do your logic according to it如果要禁止user ,请将user's字段is_banned更改为true ,然后在您的模板或视图中,检查用户是否被禁止,并根据它执行您的逻辑

By the way, in your ulrs.py you need to close paths with backslash like this path('home/', views.home, name='home'),顺便说一句,在你的ulrs.py中,你需要用反斜杠关闭路径,比如这个path('home/', views.home, name='home'),

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

相关问题 如何将用户从登录页面重定向到不同应用程序 Django 中的注册页面? - How can I redirect a user from the login page to the signup page in a different app Django? 如何在 Django 中重定向到不同的 URL? - How can I redirect to a different URL in Django? 重定向到 django 上的不同页面 - Redirect to different page on django 当函数执行 Django 时,如何重定向到同一页面 - How can i redirect to the same page when a function executed Django 如何在 Django 中为某些路由重定向到同一页面? - How can i redirect to same page for certain routes in Django? 如何使用python将用户重定向到其他服务器? - how can i redirect a user to a different server with python? 如何将用户重定向到登录页面,直到他们完成授权? - How can I redirect the user to the login page until they complete the authorization? 如果用户在 django 注册后首次登录,如何将用户重定向到不同的页面? - How to redirect a user to a different page if he's logging in for the first time after registration in django? 如何在Django中使用不同的用户组重新发布视图 - How can I rediredct the views with different user groups in django 如何为 Django 中的每个用户随机显示不同的数据? - How can I show randomly different data for each user in Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM