Django 中的用户停用
User deactivation in Django
我试图让用户在我的 django 网站上停用他们的帐户。这是我尝试过的:
views.py
from django.contrib.auth.models import User
@login_required
def deactivate_user(request, username):
context = {}
try:
user = User.objects.get(username=username)
user.is_active = False
user.save()
context['msg'] = 'Profile successfully disabled.'
except User.DoesNotExist:
context['msg'] = 'User does not exist.'
except Exception as e:
context['msg'] = e.message
return render(request, 'index.html', context=context)
urls.py
path('deactivate-user/<slug:username>', deactivate_user, name='deactivate_user'),
base.html
<form action="{% url 'users:deactivate_user' slug=username.slug %}" method="post">
{% csrf_token %}
<button type="submit" class="active">Yes, Deactivate</button>
<button type="button" data-dismiss="modal">Cancel</button>
</form>
我收到一个 NoReverseMatch 错误 Reverse for 'deactivate_user' with keyword arguments '{'slug': ''}' not found. 1 pattern(s) tried: ['users/deactivate\-user/(?P<username>[-a-zA-Z0-9_]+)$']
已经在模板中尝试了几个不同的参数,但没有任何效果。
你说的是
<form action="{% url 'users:deactivate_user' slug=username.slug %}" method="post">
slug
不是您的模式中的关键字,它是一种类型,类似于 <int:pk>
,应该是 username=username.slug
我试图让用户在我的 django 网站上停用他们的帐户。这是我尝试过的:
views.py
from django.contrib.auth.models import User
@login_required
def deactivate_user(request, username):
context = {}
try:
user = User.objects.get(username=username)
user.is_active = False
user.save()
context['msg'] = 'Profile successfully disabled.'
except User.DoesNotExist:
context['msg'] = 'User does not exist.'
except Exception as e:
context['msg'] = e.message
return render(request, 'index.html', context=context)
urls.py
path('deactivate-user/<slug:username>', deactivate_user, name='deactivate_user'),
base.html
<form action="{% url 'users:deactivate_user' slug=username.slug %}" method="post">
{% csrf_token %}
<button type="submit" class="active">Yes, Deactivate</button>
<button type="button" data-dismiss="modal">Cancel</button>
</form>
我收到一个 NoReverseMatch 错误 Reverse for 'deactivate_user' with keyword arguments '{'slug': ''}' not found. 1 pattern(s) tried: ['users/deactivate\-user/(?P<username>[-a-zA-Z0-9_]+)$']
已经在模板中尝试了几个不同的参数,但没有任何效果。
你说的是
<form action="{% url 'users:deactivate_user' slug=username.slug %}" method="post">
slug
不是您的模式中的关键字,它是一种类型,类似于 <int:pk>
,应该是 username=username.slug