unfoldadmin / django-unfold

Modern Django admin theme for seamless interface development
https://unfoldadmin.com
MIT License
1.83k stars 186 forks source link

About the setting of "Django Allauth" and "Django unfold"... #759

Closed Huanghibo closed 1 month ago

Huanghibo commented 1 month ago

Sorry, my regional visit to gitub is very unstable.Unable to view responses in a timely manner

https://docs.allauth.org/en/latest/common/admin.html

I've tried the following, a simple example, and I don't know if this method meets your requirements: 3 4

Huanghibo commented 1 month ago
  1. Override the Django Admin login view:

from unfold.sites import UnfoldAdminSite from django.conf import settings from django.contrib import admin from allauth.account.views import login

admin.autodiscover()

class CustomUnfoldAdminSite(UnfoldAdminSite): def login(self, request, extra_context=None):

Use Allauth‘s login

    return login(request)

admin_site = CustomUnfoldAdminSite()

2 . Add to urls.py .# usrls.py from django.urls import path, include from . import admin_site

urlpatterns = [ path('admin/', admin_site.urls), path('accounts/', include('allauth.urls')),

... other

3.Customize your Admin landing page (optional):

ACCOUNT_AUTHENTICATION_METHOD = 'username_email' ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = True ACCOUNT_LOGOUT_ON_GET = True ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 5 ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT = 300 # 5 minutes

Customize your Admin landing templates page

ACCOUNT_ADAPTER = 'path.to.your.adapter.Adapter'

It's like this 5

Huanghibo commented 1 month ago

But I'm sorry, I can't configure Unfold in "Settings-Unfold". The "UNFOLD->sidebar" item, which could not be set as required by my permission control, made me temporarily abandon it.

For the permission management of "AdminSite", only "Django Jazzmin" meets my needs at the moment. But "Django-Unfold" uses "Tailwind css", that's great, and that's why I wanted to use it.

Thank you!

justinqian42 commented 1 month ago
  1. Override the Django Admin login view:

from unfold.sites import UnfoldAdminSite from django.conf import settings from django.contrib import admin from allauth.account.views import login

admin.autodiscover()

class CustomUnfoldAdminSite(UnfoldAdminSite): def login(self, request, extra_context=None): # Use Allauth‘s login return login(request)

admin_site = CustomUnfoldAdminSite()

2 . Add to urls.py .# usrls.py from django.urls import path, include from . import admin_site

urlpatterns = [ path('admin/', admin_site.urls), path('accounts/', include('allauth.urls')), # ... other

3.Customize your Admin landing page (optional):

ACCOUNT_AUTHENTICATION_METHOD = 'username_email' ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = True ACCOUNT_LOGOUT_ON_GET = True ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 5 ACCOUNT_LOGIN_ATTEMPTS_TIMEOUT = 300 # 5 minutes

Customize your Admin landing templates page

ACCOUNT_ADAPTER = 'path.to.your.adapter.Adapter'

It's like this 5

Thank you so much mate!!! Great stuff.

So you didn't use the decorator from allauth but overrode the login method of UnfoldAdminSite class.

justinqian42 commented 1 month ago

But I'm sorry, I can't configure Unfold in "Settings-Unfold". The "UNFOLD->sidebar" item, which could not be set as required by my permission control, made me temporarily abandon it.

For the permission management of "AdminSite", only "Django Jazzmin" meets my needs at the moment. But "Django-Unfold" uses "Tailwind css", that's great, and that's why I wanted to use it.

Thank you!

Alright, back to your reported case. Taking the code snippet from the unfold doc -

{ "title": _("Dashboard"), "icon": "dashboard", # Supported icon set: https://fonts.google.com/icons "link": reverse_lazy("admin:index"), "badge": "sample_app.badge_callback", "permission": lambda request: request.user.is_superuser, },

As you can see the permission is followed by a lambda function which invokes the actual django's request object. Luke gave an example that judges the user's identity.

So... actually we can definitely do something like

"permission": lambda request: request.path.endswith("add/"),

In your case, I'd try to do this "permission": lambda request: request.path.find("/custom/"),

You can even do a complex one like "permission": lambda request: ( request.path.find("/custom/") and request.user.is_superuser ),

The sidebar permission control of unfold is actually more arbitrary.

lukasvinclav commented 1 month ago

What exactly is the problem? Are you pointing at the missing styles on custom login page provided by django-allauth?

When you are using secure_admin_login decorator, it redirects you to the login page coming from django-allauth and it ignores the default login page in Django admin. Then it is up to you to provide visual styles. Unfold does not provide any styling for django-allauth templates.