Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I set that when user is admin only then he can view a specific html page in Django

I’ve a html page called admin_dashboard.html. So, there is a button in the navbar called Admin Dashboard only admin can get this button. If admin click this button he will go to the admin_dashboard.html but a staff can also go to that page by using urls which is http://127.0.0.1:8000/admin_dashboard. So I want that a if staff manually types this url and tries to go to the admin_dashboard.html it will throw an error.

here is my code for that button

            {%if user.is_superuser%}
            <li><a class="dropdown-item" href="admin_dashboard">Admin Dashboard</i></a></li>
            {%endif%}

urls.py:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   path("admin_dashboard", views.admin_dashboard, name='admin_dashboard')

views.py

@login_required(login_url='/login')
def admin_dashboard(request):
    return render(request,'admin_dashboard.html')

>Solution :

You can do it this way :


@login_required(login_url='/login')
def admin_dashboard(request):
    if request.user.is_superuser :  
      return render(request,'admin_dashboard.html')
    else: 
        return render(request,'html_view_with_error',{"error" : "PERMISSION DENIED"})



Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading