繁体   English   中英

django.core.exceptions.FieldError:为用户指定的未知字段(出生日期)

[英]django.core.exceptions.FieldError: Unknown field(s) (dateofbirth) specified for User

嗨,我正在尝试向我的 django 项目添加 DOB 字段,但我不断收到此错误,但我找不到在哪里可以找到它

 File "C:\Users\Ugur\Documents\Reply\Replyproject\accounts\urls.py", line 2, in <module>
    from . import views
  File "C:\Users\Ugur\Documents\Reply\Replyproject\accounts\views.py", line 13, in <module>
    from .forms import CreateUserForm
  File "C:\Users\Ugur\Documents\Reply\Replyproject\accounts\forms.py", line 9, in <module>
    class CreateUserForm(UserCreationForm):
  File "C:\Users\Ugur\Anaconda3\envs\ECS639U\lib\site-packages\django\forms\models.py", line 276, in __new__
    raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (dateofbirth) specified for User

我的 Forms.py

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




class CreateUserForm(UserCreationForm):
    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2', 'dateofbirth']

我的观点.py

from django.shortcuts import render, redirect 
from django.http import HttpResponse
from django.forms import inlineformset_factory
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import authenticate, login, logout
from django.contrib import messages
from django.contrib.auth.decorators import login_required



# Create your views here.
from .models import *
from .forms import CreateUserForm

def registerPage(request):
    if request.user.is_authenticated:
        return redirect('/home')
    else:
        form = CreateUserForm()
        if request.method == 'POST':
            form = CreateUserForm(request.POST)
            if form.is_valid():
                form.save()
                user = form.cleaned_data.get('username')
                messages.success(request, 'Account was created for ' + user)

                return redirect('/login')
            

        context = {'form':form}
        return render(request, 'accounts/register.html', context)

def loginPage(request):
    if request.user.is_authenticated:
        return redirect('/home')
    else:
        if request.method == 'POST':
            username = request.POST.get('username')
            password =request.POST.get('password')

            user = authenticate(request, username=username, password=password)

            if user is not None:
                login(request, user)
                return redirect('/home')
            else:
                messages.info(request, 'Username OR password is incorrect')

        context = {}
        return render(request, 'accounts/login.html', context)

def logoutUser(request):
    logout(request)
    return redirect('/login')


@login_required(login_url='/login')
def home(request):
    context = {}
    return render(request, 'accounts/dashboard.html', context)

我的模型.py

from django.db import models

# Create your models here.

class Customer(models.Model):
    name = models.CharField(max_length=200, null=True)
    phone = models.CharField(max_length=200, null=True)
    email = models.CharField(max_length=200, null=True)
    dateofbirth= models.CharField(max_length=200, null=True)
    creditcard = models.CharField(max_length=16, null = True)
    date_created = models.DateTimeField(auto_now_add=True, null=True)

    def __str__(self):
        return self.name

我无法理解问题所在的控制台要更改以将 label 添加到寄存器区域任何帮助都非常感谢,下午好:)

在您的 forms 中,您为 Modal = User 指定了一个字段,但您的 Class 称为 Customer。 当您说modal = User Django 尝试使用自己的没有 dateofbirth 字段的用户模式时。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM