0

I am building a Django based website and I am facing an issue when trying to redirect users to profile/landing page if they are not super users. Only super user who is logged in should have access to admin page.

Right now I am working on localhost.

Logged In scenario: Non super users are still able to access http://127.0.0.1/admin and http://127.0.0.1/admin/login

Not Logged In scenario: Not logged in users are still able to access http://127.0.0.1/admin/login

Logged in but Non super user view when trying to access http://127.0.0.1/admin:

enter image description here

Logged in but Non super user view when trying to access http://127.0.0.1/admin/login:

enter image description here

Not logged in users when trying to access http://127.0.0.1/admin:

enter image description here

Not logged in users when trying to access http://127.0.0.1/admin/login:

enter image description here

My urls.py looks like:

from imports *

admin.autodiscover()
admin.site.admin_view = admin_view
admin.site.login = login_required(admin.site.login)
admin.site.login = staff_member_required(admin.site.login, login_url=settings.LOGIN_URL)


urlpatterns = [
    path('', views.index, name ='index'),
    path('dummy', views.loggedin, name ='dummy'),
    url(r'^admin/login/', views.loggedin, name ='dummy'),
    url(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()

What am I doing wrong here?

gujaratiraja
  • 371
  • 2
  • 19
  • What happens when you do just this line: `admin.site.login = staff_member_required(admin.site.login, login_url=settings.LOGIN_URL)` ? – Daniel Dec 04 '20 at 21:07
  • if i just keep that line even with /admin it automatically goes to /admin/login for logged in and non logged in users @Daniel – gujaratiraja Dec 04 '20 at 21:10
  • Can you try my example below? Let's see if we can pinpoint where the issue stems from. – Daniel Dec 04 '20 at 21:23
  • Still no luck. Here is the gist https://gist.github.com/parikhparth23/672b93f30fac0efb2e94d74c13db5994 which i am using after going through your answer below. @Daniel – gujaratiraja Dec 04 '20 at 21:37

1 Answers1

0

Looks like you have some extra routes in your urlpatterns - maybe these are the issue?

Here is my working urls.py that accomplishes what you are trying to do:

from django.contrib.admin.views.decorators import staff_member_required
from django.urls import path, include
from django.contrib import admin

admin.site.login = staff_member_required(admin.site.login, login_url='index')

urlpatterns = [
    
    path('admin/', admin.site.urls),
    ...

]
Daniel
  • 3,228
  • 1
  • 7
  • 23