How can I not use Django's admin login view?

2019-02-04 06:45发布

I created my own view for login. However if a user goes directly to /admin it brings them to the admin login page and doesn't use my custom view. How can I make it redirect to the login view used for everything not /admin?

9条回答
Animai°情兽
2楼-- · 2019-02-04 07:00

You can redirect admin login url to the auth login view :

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('your_app.urls')),
    path('accounts/', include('django.contrib.auth.urls')),
    path('admin/login/', RedirectView.as_view(url='/accounts/login/?next=/admin/', permanent=True)),
    path('admin/', admin.site.urls),
]
查看更多
老娘就宠你
3楼-- · 2019-02-04 07:02

http://blog.montylounge.com/2009/07/5/customizing-django-admin-branding/ (web archive)

I'm trying to solve exactly this problem and I found the solution at this guys blog. Basically, override the admin template and use your own template. In short, just make a file called login.html in /path-to-project/templates/admin/ and it will replace the admin login page. You can copy the original (django/contrib/admin/templates/login.html) and modify a line or two. If you want to scrap the default login page entirely you can do something like this:

{% extends "my-login-page.html" %}

There it is. One line in one file. Django is amazing.

查看更多
Root(大扎)
4楼-- · 2019-02-04 07:04

In your ROOT_URLCONF file (by default, it's urls.py in the project's root folder), is there a line like this:

urlpatterns = patterns('',
...
    (r'^admin/', include(admin.site.urls)),
...
)

If so, you'd want to replace include(admin.site.urls) with the custom view you created:

(r'^admin/', 'myapp.views.myloginview'),

or if your app has its own urls.py, you could include it like this:

(r'^admin/', include(myapp.urls)),
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-02-04 07:04

I had the same issue, tried to use the accepted answer, but has the same issue as pointed in the comment above. Then I've did something bit different, pasting here if this would be helpful to someone.

def staff_or_404(u):
    if u.is_active:
        if u.is_staff:
            return True
        raise Http404()
    return False

admin.site.login = user_passes_test(
        staff_or_404,
    )(admin.site.login)

The idea is that if the user is login, and tried to access the admin, then he gets 404. Otherwise, it will force you to the normal login page (unless you are already logged in)

查看更多
不美不萌又怎样
6楼-- · 2019-02-04 07:07

I found that the answer above does not respect the "next" query parameter correctly.

An easy way to solve this problem is to use a simple redirect. In your site's urls file, immediately before including the admin urls, put a line like this:

   url(r'^admin/login$', RedirectView.as_view(pattern_name='my_login_page', permanent=True, query_string=True))
查看更多
Animai°情兽
7楼-- · 2019-02-04 07:08

From http://djangosnippets.org/snippets/2127/—wrap the admin login page with login_required. For example, in urls.py:

from django.contrib.auth.decorators import login_required
from django.contrib import admin
admin.autodiscover()
admin.site.login = login_required(admin.site.login)

You probably already have the middle two lines and maybe even the first line; adding that fourth line will cause anything that would have hit the admin.site.login function to redirect to your LOGIN_URL with the appropriate next parameter.

查看更多
登录 后发表回答