Search code examples
pythondjangodjango-viewsdjango-templatesdjango-authentication

Django authentication: index for staff and index for user (2 templates)


I have 2 functions currently working, one is a specific view for is_staff users, the other for any users. Right now after staff logs in they can add path /dashboard to the URL and can access the route, while normal users cannot view it. That is expected and good.

Problem: I want to at login, to redirect user.is_staff straight to this path /dashboard, instead of going to index first.

The functions somehow I think I have to merge: (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'),
        ] 

When user is_staff, show "dashboard/"(dashboard.html), not ""(index.html)

*I also have extension template seen below but that did not fix the issue:

 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 %}

################### UPDATE ####################

So far this works, but I still need to refactor:

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

I was instructed to use the decorator inside the statement- Teacher requirement :/ - (cause only staff can view part of the content) and use HttpResponse to display the correct html. Any idea how to refactor this?

I am trying to add the logic seen on the commented out lines (##)

(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'),

############## SOLUTION ###################

As suggested in the comments, I have fixed the urls.py and split the request in 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'),

Solution

  • try something like this.

    @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)
    

    To check whether a user is is_staff you can do something like this user.is_staff or user.is_superuser.
    let me know if it works.