我正在尝试向默认的 Django 表单添加一个新字段,但在尝试从我创建的 forms.py 文件中导入 class 时出现错误
I am trying to add a new field to the default Django Form and am getting an error when I'm trying to import a class from a forms.py file I made
错误发生在 views.py 文件中,当我尝试导入我从我的表单 .py 文件制作的表单 class 时,我收到错误...
Import ".forms" could not be resolved
这是我的 views.py 文件的样子:
from django.shortcuts import render, redirect
from .forms import RegisterForm
# Create your views here.
def register(response):
if response.method == "POST":
form = RegisterForm(response.POST)
if form.is_valid():
form.save()
return redirect("/home")
else:
form = RegisterForm()
return render(response,"register/register.html",{"form":form})
这是我的 forms.py 文件
from django import forms
from django.contrib.auth import login, authenticate
from django.contrib.auth import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class RegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = User
fields = ["username", "email", "password1", "password2"]
为什么我会收到 error/what 我错过了吗?
你的问题来自导入“from django.contrib.auth import forms”所以我发现当你注释掉它或删除它时,一切都应该正常!
from django import forms
from django.contrib.auth import login, authenticate
# This is where your problem stems "from django.contrib.auth import forms"
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class RegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = User
fields = ["username", "email", "password1", "password2"]
在您看来,保存表单后条件语句不满足条件,因此它不会重定向到主页,您需要将重定向放在 form.save( ) 使其满足我所做的条件。
def register(request):
if request.method == "POST":
form = RegisterForm(request.POST)
if form.is_valid():
form.save()
return redirect("/home")
else:
form = RegisterForm()
return render(request,"register/register.html",{"form":form})
错误发生在 views.py 文件中,当我尝试导入我从我的表单 .py 文件制作的表单 class 时,我收到错误...
Import ".forms" could not be resolved
这是我的 views.py 文件的样子:
from django.shortcuts import render, redirect
from .forms import RegisterForm
# Create your views here.
def register(response):
if response.method == "POST":
form = RegisterForm(response.POST)
if form.is_valid():
form.save()
return redirect("/home")
else:
form = RegisterForm()
return render(response,"register/register.html",{"form":form})
这是我的 forms.py 文件
from django import forms
from django.contrib.auth import login, authenticate
from django.contrib.auth import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class RegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = User
fields = ["username", "email", "password1", "password2"]
为什么我会收到 error/what 我错过了吗?
你的问题来自导入“from django.contrib.auth import forms”所以我发现当你注释掉它或删除它时,一切都应该正常!
from django import forms
from django.contrib.auth import login, authenticate
# This is where your problem stems "from django.contrib.auth import forms"
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class RegisterForm(UserCreationForm):
email = forms.EmailField()
class Meta:
model = User
fields = ["username", "email", "password1", "password2"]
在您看来,保存表单后条件语句不满足条件,因此它不会重定向到主页,您需要将重定向放在 form.save( ) 使其满足我所做的条件。
def register(request):
if request.method == "POST":
form = RegisterForm(request.POST)
if form.is_valid():
form.save()
return redirect("/home")
else:
form = RegisterForm()
return render(request,"register/register.html",{"form":form})