使用django-allauth时如何自定义用户配置文件 [英] How to customize user profile when using django-allauth
问题描述
Per 提供的文档 django-allauth
:
ACCOUNT_SIGNUP_FORM_CLASS
(=无
)
指向在注册过程中使用的自定义表单类(例如
'myapp.forms.SignupForm'
)的字符串,以询问用户的其他输入(例如通讯注册,出生日期)。该类应该实现一个'save'
方法,接受新注册的用户作为其唯一的参数。
我是django的新手,正在努力与此。有人可以提供这样的自定义表单类的示例吗?我还需要添加一个模型类以及用户对象的链接,如这个?
假设您想在注册时询问用户的姓/名。您需要将这些字段放在您自己的表单中,如下所示:
class SignupForm(forms.Form):
first_name = forms.CharField(max_length = 30,label ='Voornaam')
last_name = forms.CharField(max_length = 30,label ='Achternaam')
def signup ,request,user):
user.first_name = self.cleaned_data ['first_name']
user.last_name = self.cleaned_data ['last_name']
user.save()
然后,在您的设置中指向此表单:
ACCOUNT_SIGNUP_FORM_CLASS ='yourproject.yourapp.forms.SignupForm'
这就是全部。
I have a django project with the django-allauth app. I need to collect additional data from the user at signup. I came across a similar question here but unfortunately, no one answered the profile customization part.
Per the documentation provided for django-allauth
:
ACCOUNT_SIGNUP_FORM_CLASS
(=None
)A string pointing to a custom form class (e.g.
‘myapp.forms.SignupForm’
) that is used during signup to ask the user for additional input (e.g. newsletter signup, birth date). This class should implement a‘save’
method, accepting the newly signed up user as its only parameter.
I am new to django and am struggling with this. Can someone provide an example of such a custom form class? Do I need to add a model class as well with a link to the user object like this ?
Suppose you want to ask the user for his first/last name during signup. You'll need to put these fields in your own form, like so:
class SignupForm(forms.Form):
first_name = forms.CharField(max_length=30, label='Voornaam')
last_name = forms.CharField(max_length=30, label='Achternaam')
def signup(self, request, user):
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.save()
Then, in your settings point to this form:
ACCOUNT_SIGNUP_FORM_CLASS = 'yourproject.yourapp.forms.SignupForm'
That's all.
这篇关于使用django-allauth时如何自定义用户配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!