【发布时间】:2019-09-25 09:09:12
【问题描述】:
我正在尝试制作一个用于在 django 中注册的网页,但我无法解决此错误:
from accounts.forms.forms import AuthForm, SignUpForm
文件“C:\Users\tonik\OneDrive\Plocha\Python projects\chytry-lock-master\smartlock\accounts\forms\forms.py”,第 7 行,在 类 SignUpForm(UserCreationForm): new 中的文件“C:\Users\tonik\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\models.py”,第 256 行 apply_limit_choices_to=假, 文件“C:\Users\tonik\AppData\Local\Programs\Python\Python36\lib\site-packages\django\forms\models.py”,第 139 行,位于 fields_for_model opts = model._meta AttributeError: 'tuple' 对象没有属性 '_meta'
我的表单.py:
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.forms import AuthenticationForm
from django.forms.widgets import PasswordInput, TextInput
class SignUpForm(UserCreationForm):
email = forms.EmailField(required=True, help_text='Required. Please enter a valid e-mail address.')
email = forms.EmailField(required=True, widget=TextInput(attrs={'class': 'span2', 'placeholder': 'e-mail'}), help_text='Required. Please enter a valid e-mail address.')
class Meta:
model = UserCreationFormFields = ('username', 'email', 'password1', 'password2')
fields = "__all__"
class AuthForm(AuthenticationForm):
class Meta:
model = AuthenticationForm
def __init__(self, *args, **kwargs):
super(AuthenticationForm, self).__init__(*args, **kwargs)
for field in self.fields.values():
field.error_messages = {'required': '{fieldname} is required'.format(fieldname=field.label)}
username = forms.CharField(widget=TextInput(attrs={'placeholder': 'nickname'})),
email = forms.CharField(widget=TextInput(attrs={'placeholder': 'e-mail'})),
password = forms.CharField(widget=PasswordInput(attrs={'placeholder':'password'}))
我的观点:
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login, authenticate
from smartlock.forms import SignUpForm
def signup(request):
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
first_password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=first_password)
login(request, user)
return redirect('home')
else:
form = UserCreationForm()
return render(request, 'signup.html', {'form': form})
else:
form = UserCreationForm()
form = SignUpForm()
return render(request, 'signup.html', {'form': form})
花了几个小时试图解决这个问题,仍然不确定是什么问题。
【问题讨论】:
-
这条线应该做什么?
model = UserCreationFormFields = ('username', 'email', 'password1', 'password2')? -
A
model = ...应该始终指定模型,而不是表单,也不是字段列表/元组/...。