[英]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'),
banned_alert
path,banned_alert
路径,banned_alert
banned_alert
banned_alert
banned_alert
banned_alert
banned_alert
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 thispath('home/', views.home, name='home'),
顺便说一句,在你的
ulrs.py
中,你需要用反斜杠关闭路径,比如这个path('home/', views.home, name='home'),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.