Django 身份验证:员工索引和用户索引(2 个模板)
Django authentication: index for staff and index for user (2 templates)
我目前有 2 个功能可用,一个是 is_staff 用户的特定视图,另一个是任何用户的特定视图。
现在工作人员登录后,他们可以将路径 /dashboard 添加到 URL 并可以访问该路线,而普通用户无法查看。这是意料之中的好事。
问题:我想在登录时将 user.is_staff 直接重定向到此路径 /dashboard,而不是继续首先索引。
我认为我必须以某种方式合并这些功能:
(views.py)
1 @staff_member_required
2 def staff_dashboard(request):
3 users = User.objects.all()
4 customer = Customer.objects.all()
5 accounts = Account.objects.all()
6 context = {
7 'users': users,
8 'customer': customer,
9 'accounts': accounts
10 }
11 return render(request, 'bank_app/dashboard.html', context)
12
13
14 @login_required
15 def index(request):
16 customer = Customer.objects.filter(user=request.user)
17 accounts = Account.objects.filter(user=request.user)
18 context = {
19 'customer': customer,
20 'accounts': accounts
21 }
22 return render(request, 'bank_app/index.html', context)
(urls.py:)
urlpatterns = [
path('dashboard', views.staff_dashboard, name='staff_dashboard'),
path('', views.index, name='index'),
path('create', views.create, name='create'),
path('createaccount', views.createaccount, name='createaccount'),
path('details/<int:pk>', views.details, name='details'),
]
当用户 is_staff 时,显示“dashboard/
”(dashboard.html),而不是 ""
(index.html)
*我还有下面看到的扩展模板,但这并没有解决问题:
28 <p>Welcome {{ user }}</p>
29
30 {% if user.is_staff %}
31 {% block staffcontent %}
32 {% endblock %}
33 {% else %}
34 {% block content %}
35 {% endblock %}
36 {% endif %}
37
38 {% block footer %}
39 {% endblock %}
################### 更新 ############### #####
到目前为止这是有效的,但我仍然需要重构:
10 @login_required
11 def index(request):
12 if request.user.is_staff:
##13 @staff_member_required
##14 def staff_dashboard(request):
15 users = User.objects.all()
16 customer = Customer.objects.all()
17 accounts = Account.objects.all()
18 context = {
19 'users': users,
20 'customer': customer,
21 'accounts': accounts
22 }
23 return render(request, 'bank_app/dashboard.html', context)
##24 return HttpResponseRedirect('staff_dashboard')
25 else:
##26 def user_dashboard():
27 customer = Customer.objects.filter(user=request.user)
28 accounts = Account.objects.filter(user=request.user)
29 context = {
30 'customer': customer,
31 'accounts': accounts
32 }
33 return render(request, 'bank_app/index.html', context)
34
我被指示在声明中使用装饰器 - 教师要求:/ - (因为只有工作人员可以查看部分内容)并使用 HttpResponse 来显示正确的 html。
知道如何重构这个吗?
我正在尝试添加在注释掉的行 (##) 中看到的逻辑
(urls.py):
5 urlpatterns = [
6 path('', views.index, name='index'),
##7 path('staff_dashboard', views.index, name='staff_dashboard'),
##8 path('user_dashboard/', views.index, name='user_dashboard'),
############## 解决方案###################
按照评论中的建议,我已修复 urls.py 并将请求拆分为 views.py:
1 @login_required
2 def index(request):
3 if request.user.is_staff:
4 return HttpResponseRedirect('staff_dashboard/')
5 else:
6 return HttpResponseRedirect('user_dashboard/')
7
8
9 @staff_member_required
10 def staff_dashboard(request):
11 users = User.objects.all()
12 customer = Customer.objects.all()
13 accounts = Account.objects.all()
14 context = {
15 'users': users,
16 'customer': customer,
17 'accounts': accounts
18 }
19 return render(request, 'bank_app/dashboard.html', context)
20
21
22 @login_required
23 def user_dashboard(request):
24 customer = Customer.objects.filter(user=request.user)
25 accounts = Account.objects.filter(user=request.user)
26 context = {
27 'customer': customer,
28 'accounts': accounts
29 }
30 return render(request, 'bank_app/index.html', context)
(urls.py):
1 urlpatterns = [
2 path('', views.index, name='index'),
3 path('staff_dashboard/', views.staff_dashboard, name='staff_dashboard'),
4 path('user_dashboard/', views.user_dashboard, name='user_dashboard'),
尝试这样的事情。
@login_required
def index(request):
if request.user.is_superuser:
return redirect('staff_dashboard')
else:
customer = Customer.objects.filter(user=request.user)
accounts = Account.objects.filter(user=request.user)
context = {
'customer': customer,
'accounts': accounts
}
return render(request, 'bank_app/index.html', context)
要检查用户是否 is_staff 你可以这样做 user.is_staff 或 user.is_superuser.
让我知道它是否有效。
我目前有 2 个功能可用,一个是 is_staff 用户的特定视图,另一个是任何用户的特定视图。 现在工作人员登录后,他们可以将路径 /dashboard 添加到 URL 并可以访问该路线,而普通用户无法查看。这是意料之中的好事。
问题:我想在登录时将 user.is_staff 直接重定向到此路径 /dashboard,而不是继续首先索引。
我认为我必须以某种方式合并这些功能: (views.py)
1 @staff_member_required
2 def staff_dashboard(request):
3 users = User.objects.all()
4 customer = Customer.objects.all()
5 accounts = Account.objects.all()
6 context = {
7 'users': users,
8 'customer': customer,
9 'accounts': accounts
10 }
11 return render(request, 'bank_app/dashboard.html', context)
12
13
14 @login_required
15 def index(request):
16 customer = Customer.objects.filter(user=request.user)
17 accounts = Account.objects.filter(user=request.user)
18 context = {
19 'customer': customer,
20 'accounts': accounts
21 }
22 return render(request, 'bank_app/index.html', context)
(urls.py:)
urlpatterns = [
path('dashboard', views.staff_dashboard, name='staff_dashboard'),
path('', views.index, name='index'),
path('create', views.create, name='create'),
path('createaccount', views.createaccount, name='createaccount'),
path('details/<int:pk>', views.details, name='details'),
]
当用户 is_staff 时,显示“dashboard/
”(dashboard.html),而不是 ""
(index.html)
*我还有下面看到的扩展模板,但这并没有解决问题:
28 <p>Welcome {{ user }}</p>
29
30 {% if user.is_staff %}
31 {% block staffcontent %}
32 {% endblock %}
33 {% else %}
34 {% block content %}
35 {% endblock %}
36 {% endif %}
37
38 {% block footer %}
39 {% endblock %}
################### 更新 ############### #####
到目前为止这是有效的,但我仍然需要重构:
10 @login_required
11 def index(request):
12 if request.user.is_staff:
##13 @staff_member_required
##14 def staff_dashboard(request):
15 users = User.objects.all()
16 customer = Customer.objects.all()
17 accounts = Account.objects.all()
18 context = {
19 'users': users,
20 'customer': customer,
21 'accounts': accounts
22 }
23 return render(request, 'bank_app/dashboard.html', context)
##24 return HttpResponseRedirect('staff_dashboard')
25 else:
##26 def user_dashboard():
27 customer = Customer.objects.filter(user=request.user)
28 accounts = Account.objects.filter(user=request.user)
29 context = {
30 'customer': customer,
31 'accounts': accounts
32 }
33 return render(request, 'bank_app/index.html', context)
34
我被指示在声明中使用装饰器 - 教师要求:/ - (因为只有工作人员可以查看部分内容)并使用 HttpResponse 来显示正确的 html。 知道如何重构这个吗?
我正在尝试添加在注释掉的行 (##) 中看到的逻辑
(urls.py):
5 urlpatterns = [
6 path('', views.index, name='index'),
##7 path('staff_dashboard', views.index, name='staff_dashboard'),
##8 path('user_dashboard/', views.index, name='user_dashboard'),
############## 解决方案###################
按照评论中的建议,我已修复 urls.py 并将请求拆分为 views.py:
1 @login_required
2 def index(request):
3 if request.user.is_staff:
4 return HttpResponseRedirect('staff_dashboard/')
5 else:
6 return HttpResponseRedirect('user_dashboard/')
7
8
9 @staff_member_required
10 def staff_dashboard(request):
11 users = User.objects.all()
12 customer = Customer.objects.all()
13 accounts = Account.objects.all()
14 context = {
15 'users': users,
16 'customer': customer,
17 'accounts': accounts
18 }
19 return render(request, 'bank_app/dashboard.html', context)
20
21
22 @login_required
23 def user_dashboard(request):
24 customer = Customer.objects.filter(user=request.user)
25 accounts = Account.objects.filter(user=request.user)
26 context = {
27 'customer': customer,
28 'accounts': accounts
29 }
30 return render(request, 'bank_app/index.html', context)
(urls.py):
1 urlpatterns = [
2 path('', views.index, name='index'),
3 path('staff_dashboard/', views.staff_dashboard, name='staff_dashboard'),
4 path('user_dashboard/', views.user_dashboard, name='user_dashboard'),
尝试这样的事情。
@login_required
def index(request):
if request.user.is_superuser:
return redirect('staff_dashboard')
else:
customer = Customer.objects.filter(user=request.user)
accounts = Account.objects.filter(user=request.user)
context = {
'customer': customer,
'accounts': accounts
}
return render(request, 'bank_app/index.html', context)
要检查用户是否 is_staff 你可以这样做 user.is_staff 或 user.is_superuser.
让我知道它是否有效。