Django Allauth适配器无效:添加适配器后登录界面仍显示
Let's break down what's happening here and get your setup working as intended:
First, a quick clarification: Your NoNewUsersAccountAdapter with is_open_for_signup(self, request): return False does correctly block new user registrations—if someone tries to visit the /signup URL directly, they'll be denied access. However, this setting doesn't automatically remove registration links from the login page, nor does it hide the login interface itself (that's not what this adapter method is designed for).
Here's how to address the common issues you might be facing:
1. Double-Check Your Adapter Configuration
First, make sure your setup is properly wired up—typos or missing app registrations are frequent culprits:
- Confirm the
accountsapp is listed inINSTALLED_APPSin yoursettings.py:INSTALLED_APPS = [ # ... your other apps 'accounts', 'allauth', 'allauth.account', 'allauth.socialaccount', ] - Verify your
ACCOUNT_ADAPTERpath is 100% accurate:ACCOUNT_ADAPTER = 'accounts.adapter.NoNewUsersAccountAdapter' - Restart your Django server—adapter changes often don't take effect until you restart the process.
2. Remove Registration Links from the Login Template
Allauth's default login template still displays signup links even when registration is disabled. To clean this up, override the template:
- Create a
templates/accountfolder inside youraccountsapp. - Copy the original
login.htmltemplate from Allauth's package (you'll find this in your Python site-packages underallauth/templates/account/login.html) into this new folder. - Edit the copied template and delete or comment out the signup link section. Look for code like this:
<p>{% trans "Don't have an account?" %} <a href="{{ signup_url }}">{% trans "Sign Up" %}</a></p>
3. Redirect Users Who Try to Access Signup Directly
If you want to send users straight to the login page when they try to visit /signup, update your adapter with a redirect method:
from allauth.account.adapter import DefaultAccountAdapter from django.shortcuts import redirect class NoNewUsersAccountAdapter(DefaultAccountAdapter): def is_open_for_signup(self, request): return False def get_signup_redirect_url(self, request): # Send users to the login page instead of allowing signup return self.get_login_url(request)
4. If You Actually Want to Hide the Login Interface (Not Just Signup)
If your goal was to disable login entirely (rather than just blocking new registrations), this adapter approach isn't the right tool. Instead, you'd need to:
- Use Django's permission system or middleware to restrict access to the login URL.
- Remove all login links from your site's templates.
- Adjust
LOGIN_URLsettings if needed to redirect unauthenticated users appropriately.
内容的提问来源于stack exchange,提问作者user12151542

