Hello! This is Ponta, a Shiba Inu. I had a dream of being chased by a wild boar in the mountains. I was very scared.
By the way, in the custom user model created last time, I added dogname and introduction, but as shown below, there was no input item on the registration screen.
Let's add input items for dog name and introduction here.
Add it to the Signup Form so that you can enter the dog name and introduction in the input form.
accounts/forms.py
rom allauth.account.forms import SignupForm
from django import forms
from .models import CustomUser
from django.utils.translation import gettext, gettext_lazy as _, pgettext
class CustomSignupForm(SignupForm):
dogname = forms.CharField(label=_("Dog Name"),
widget=forms.TextInput(
attrs={"placeholder": _('Dog Name')}
)
)
introduction = forms.CharField(label=_("Introduction"),
widget=forms.Textarea(
attrs={"placeholder": _('Introduction')}
))
class Meta:
model = CustomUser
def signup(self, user):
user.dogname = self.cleaned_data['dogname']
user.introduction = self.cleaned_data['introduction']
user.save()
return user
class UpdateProfileForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.user = kwargs.get('instance', None)
for field in self.fields.values():
field.widget.attrs['class'] = 'form-control'
class Meta:
model = CustomUser
fields = ('email', 'username', 'dogname', 'introduction')
Overrides the DefaultAccountAdapter method save_user () to store user information. The point is to set the save_user argument commit to True.
accounts/adapter.py
from allauth.account.adapter import DefaultAccountAdapter
class AccountAdapter(DefaultAccountAdapter):
def save_user(self, request, user, form, commit=True):
"""
This is called when saving user via allauth registration.
we override this to set additional data on user object.
"""
user = super(AccountAdapter, self).save_user(request, user, form, commit=False)
user.dogname = form.cleaned_data.get('dogname')
user.introduction = form.cleaned_data.get('introduction')
user.save()
Set up to use CustomSignupForm and AccountAdapter.
shiba_app/settings.py
ACCOUNT_FORMS = {
'signup': 'accounts.forms.CustomSignupForm',
}
ACCOUNT_ADAPTER = 'accounts.adapter.AccountAdapter'
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = False
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = False So, I try to enter the password only once.
http://127.0.0.1:8000/accounts/signup/ Go to and make sure you can enter the Dog Name and Introduction.
After entering all the items, save it and go to the management screen to confirm that it is entered correctly
It went well! See you dear! Bye bye!
Reference article 1: Create an authentication function using django-allauth and CustomUser in Django Reference article 2: Creating and Populating User instances
Recommended Posts