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

Django报错:'User' object has no attribute 'is_valid' 技术求助

Fixing the 'User' object has no attribute 'is_valid' Error in Django 1.8/Python 3.5

Hey there, let's break down why you're hitting this error and how to fix it quickly.

The Root Cause

That error pops up because you're trying to call is_valid() directly on a User model instance—but is_valid() is a method belonging to Django's Form or ModelForm classes, not the User model itself. When you extend AbstractUser, you still need a form layer to handle user input validation, creation, and saving.

Step 1: Create a Custom User Creation Form

First, make a forms.py file in your mainpage app (if you don't have one already) to define a form tied to your custom User model. We'll inherit from UserCreationForm to get built-in password validation logic:

from django import forms
from django.contrib.auth.forms import UserCreationForm
from .models import User

class CustomUserCreationForm(UserCreationForm):
    # Add your custom fields here
    bio = forms.CharField(widget=forms.Textarea, required=False)
    date_of_birth = forms.DateField(required=False)

    class Meta:
        model = User
        # Include all fields you want on the signup form
        fields = ('username', 'email', 'bio', 'date_of_birth', 'password1', 'password2')

Step 2: Update Your Signup View

Now modify your signup/views.py to use this form instead of directly instantiating the User model. The form will handle validation and user creation:

from django.shortcuts import render, redirect
from mainpage.forms import CustomUserCreationForm  # Import your new form

def signup(request):
    if request.method == 'POST':
        # Pass POST data to the form
        form = CustomUserCreationForm(request.POST)
        # Call is_valid() on the FORM, not the User instance
        if form.is_valid():
            # Save the form to create a new User instance
            user = form.save()
            # Optional: Log the user in immediately
            # from django.contrib.auth import login
            # login(request, user)
            return redirect('mainpage:home')  # Redirect to your main page
    else:
        # Empty form for GET requests
        form = CustomUserCreationForm()
    
    return render(request, 'signup/signup.html', {'form': form})

Step 3: Double-Check Your Settings

Make sure you've registered your custom User model in settings.py—this is critical for Django to recognize your extended model:

AUTH_USER_MODEL = 'mainpage.User'

Key Notes to Avoid Future Issues

  • Never call validation methods directly on model instances. Forms are designed to handle user input validation, including checking for duplicate usernames, valid password formats, and custom field rules.
  • Since you're using Django 1.8, ensure your template uses the form correctly (e.g., rendering fields with {{ form.username }}, {{ form.errors }} to show validation messages).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 06:55:30