我正在研究django 1.10项目,我正在尝试创建一个注册新用户的视图。以下是我的观点:
class UserFormView(View):
form_class = UserForm
template_name = 'utilisateur/registration.html'
# /Build in fonction for get request/
# /Display a blank form/
def get(self, request):
form = self.form_class(None)
return render(request, self.template_name, {'form': form})
# /Build in fonction for post request/
# /Process form data/
def post(self, request):
form = self.form_class(request.POST)
# /Check validity of data given/
if form.is_valid():
# /Create the object user for the form/
user = form.save(commit=False)
# /Normalize data/
username = form.cleaned_data['username']
password = form.cleaned_data['password']
# /Editable password/
user.set_password(password)
user.save()
# /Check if these data already exist in de db/
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
# Here you can request something you'd show
# About the user e.g: photo etc..
# /Redirect to home page/
return redirect('utilisateur:index')
# /Blank form if user not exist/
return render(request, self.template_name, {'form': form})
这是我的网址文件:
# /RegisterPage/
url(r'register', views.UserFormView.as_view(), name='register'),
这是我的表单文件:
class UserForm(forms.ModelForm):
password = forms.CharField(label="Mot de passe", widget=forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'email', 'password']
问题是,在注册页面上,我收到一条错误消息:'UserForm'对象没有属性'get'。 我没看到我做错了什么,有人能帮帮我吗? 谢谢!