You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django 1.11+Cookiecutter-Django+Allauth仅邮箱登录报错求助

Fixing Email-Only Login Error in Cookiecutter-Django with Custom User Model

Hey there! Let's work through this email-only login issue you're facing with your Cookiecutter-Django ecommerce setup. You've got the right core configuration in place, but the default users app code needs a few tweaks to play nice with your custom accounts.User model. Here's how to fix it step by step:

1. Validate Core Configuration & Migrations

First, let's confirm your settings are correctly applied (you already have these, but let's tie up loose ends):

# settings.py
ACCOUNT_USER_MODEL_USERNAME_FIELD = None
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
AUTH_USER_MODEL = 'accounts.User'
LOGIN_REDIRECT_URL = 'users:redirect'
LOGIN_URL = 'account_login'

Double-check you've run migrations for your custom accounts.User model—this is critical to avoid database mismatches:

python manage.py makemigrations accounts
python manage.py migrate

2. Update the Default users App Code

The out-of-the-box users app is built for Django's standard User model, so we need to adjust it to use your custom model.

a. users/models.py

You can delete this file entirely (since AUTH_USER_MODEL already points to your accounts.User model) or strip it down to avoid conflicts. If you keep it, remove any duplicate User model definitions—only add helper functions if you need them.

b. users/urls.py

The URL structure is fine, but ensure it references the updated view we'll tweak next:

# users/urls.py
from django.urls import path
from . import views

app_name = 'users'

urlpatterns = [
    path("redirect/", views.user_redirect_view, name="redirect"),
]

c. users/views.py

The default redirect view might have hardcoded references to the standard User model. Update it to use Django's dynamic auth model getter:

# users/views.py
from django.contrib.auth import get_user_model
from django.shortcuts import redirect

User = get_user_model()

def user_redirect_view(request):
    """Redirect users to your desired post-login page"""
    # Adjust this to your ecommerce homepage or user profile URL
    return redirect("/")

Using get_user_model() ensures we always reference your active accounts.User model, no hardcoding required.

3. Fix Allauth's Login Template & Form

The "Sign In" button is likely pulling Allauth's default template, which still includes a username field. Override it to only show email and password:

  1. Create a templates/account/login.html file in your project root
  2. Use this simplified template (remove the username field section):
<!-- templates/account/login.html -->
{% extends "base.html" %}
{% load i18n %}
{% load account socialaccount %}

{% block head_title %}{% trans "Sign In" %}{% endblock %}

{% block content %}
<h1>{% trans "Sign In" %}</h1>

{% if not socialaccount_providers %}
<p>{% blocktrans %}If you don't have an account yet, <a href="{{ signup_url }}">sign up</a> first.{% endblocktrans %}</p>
{% endif %}

<form class="login" method="POST" action="{% url 'account_login' %}">
  {% csrf_token %}
  <!-- Keep only email and password fields -->
  {{ form.email }}
  {{ form.password }}
  {% if redirect_field_value %}
  <input type="hidden" name="{{ redirect_field_name }}" value="{{ redirect_field_value }}" />
  {% endif %}
  <a class="button secondaryAction" href="{% url 'account_reset_password' %}">{% trans "Forgot Password?" %}</a>
  <button class="primaryAction" type="submit">{% trans "Sign In" %}</button>
</form>
{% endblock %}

4. Clear Cache & Restart

Django sometimes caches old model references, so:

  • Clear your browser cache
  • Restart your Django dev server
  • If using a caching backend (like Redis), clear that too

5. Troubleshoot Common Errors

If you still see issues, check these:

  • "User matching query does not exist": Ensure you created a user with an email (use python manage.py createsuperuser—it should ask for email instead of username if your model is set up right)
  • "AttributeError: 'User' object has no attribute 'username'": Some code is still looking for a username field. Double-check your accounts.User model and ensure all views/templates use email instead of username
  • Template not updating: Confirm your custom login template is in templates/account/login.html so Allauth uses it instead of the default

Once you've made these changes, test the "Sign In" button again—it should now work smoothly with just an email and password!

内容的提问来源于stack exchange,提问作者Dhaval Savalia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:46:05