【发布时间】:2021-08-18 15:23:05
【问题描述】:
您好,我正在尝试向我的 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
我的表单.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
控制台我无法理解问题出在哪里以更改将标签添加到注册区域任何帮助都非常感谢下午好:)
【问题讨论】:
-
这里提到了用户字段,您可以从中选择docs.djangoproject.com/en/3.2/ref/contrib/auth
-
我想知道您不能将自定义字段归功于用户字段吗?
-
你可以,但你没有。
-
在 CreateUserForm 中提到的用户模型中没有名为 dateofbirth 的字段..
-
我遇到了类似的错误
django.core.exceptions.FieldError: Unknown field(s) (memberscreated_by) specified for Project,因为我忘记在表单字段之间添加逗号,例如'members' 'created_by',应该是'members', 'created_by',
标签: python django visual-studio debugging