问题描述
我有一个django项目与django-allauth应用程序。我需要从用户收到额外的数据。我在这里遇到了类似的:我是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
:
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时如何自定义用户配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!