Home > Back-end >  Redirect to Admin Panel(Superuser) Login Page from views.py and template in Django
Redirect to Admin Panel(Superuser) Login Page from views.py and template in Django

Time:11-14

I am trying to keep a link of Django's prebuilds admin pannel on my website.

My Project's urls.py:

urlpatterns = [
    path('', include('diagnosis.urls')),
    path('admin/', admin.site.urls, name='admin'),
]

In Template:

<a href="{% url 'admin' %}" class="btn btn-sm btn-primary px-6">Explore Admin Pannel!</a>

But it gives errors like:

NoReverseMatch at /
Reverse for 'admin' not found. 'admin' is not a valid view function or pattern name.

How can I fix this?

I also tried redirecting to admin in views.py like:

if (condition):
        return redirect('admin')

This approach also does not work. How can I redirect in admin pannel from views.py also?

CodePudding user response:

You need to use admin:index instead of admin.

In a template:

<a href="{% url 'admin:index' %}" class="btn btn-sm btn-primary px-6">Explore Admin Pannel!</a>

In a view:

if (condition):
    return redirect('admin:index')
  • Related