我正在尝试制作一个用于在Django中注册的网页,但无法解决此错误:
from accounts.forms.forms import AuthForm, SignUpForm
第7行中的文件“ C:\ Users \ tonik \ OneDrive \ Plocha \ Python projects \ chytry-lock-master \ smartlock \ accounts \ forms \ forms.py” 类SignUpForm(UserCreationForm): 新建中的文件“ C:\ Users \ tonik \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ django \ forms \ models.py”,第256行 apply_limit_choices_to = False, 文件“ C:\ Users \ tonik \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ django \ forms \ models.py”,第139行,在fields_for_model中 opts = model._meta AttributeError:“元组”对象没有属性“ _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})
花了几个小时试图解决这个问题,仍然不确定出什么问题。