简体   繁体   中英

Redirect to Admin Panel(Superuser) Login Page from views.py and template in Django

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM