What is a dog? Django--Create a custom user model 2

Registration of additional information

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.

スクリーンショット 2020-08-30 20.46.29.png

Let's add input items for dog name and introduction here.

Signup Form Override

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')

Override DefaultAccountAdapter

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()

ACCOUNT_FORMS, ACCOUNT_ADAPTER settings

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.

Display test

http://127.0.0.1:8000/accounts/signup/ Go to and make sure you can enter the Dog Name and Introduction.

スクリーンショット 2020-09-02 6.31.03.png

After entering all the items, save it and go to the management screen to confirm that it is entered correctly

スクリーンショット 2020-09-02 6.34.55.png

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

What is a dog? Django--Create a custom user model
What is a dog? Django--Create a custom user model 2
What is a super user?
User is not added successfully after creating a custom User model
What is a dog? Django installation volume
What is a dog? Python installation volume
Implement a Custom User Model in Django
What is a distribution?
What is a dog? Challenge Django templates! Volume
What is a terminal?
What is a hacker?
What is a pointer?
What is a dog? POST Sending Volume Using Django--forms.py
What is a dog? Django App Creation Start Volume--startapp
What is a dog? Django App Creation Start Volume--startproject
What is a decision tree?
What is a Context Switch?
[Definition] What is a framework?
What is a callback function?
What is a python map?
[Python] What is a zip function?
[Python] What is a with statement?
What is a lexical scope / dynamic scope?
What is a Convolutional Neural Network?
[My note] Django's custom User model
What is a dog? Volume of GET request and query parameters
What is a dog? Django--Get Name and Date from URL Volume
What is on_delete used in django's model?
It's a Mac. What is the Linux command Linux?
Tell me what a conformal map is, Python!
What is namespace
What is copy.copy ()
What is Django? .. ..
What is dotenv?
What is POSIX?
What is a dog? Django--Volume of using values obtained from URLs in class-based views
What is klass?
What is SALOME?
What is Linux?
What is python
What is hyperopt?
What is Linux
What is pyvenv
What is __call__
What is Linux
What is Python
Basics of Python learning ~ What is a string literal? ~
What is a recommend engine? Summary of the types
What is God? Make a simple chatbot with python
To myself as a Django beginner (2) --What is MTV?
What is a dog? Django--Getting Started with Form for the First Time POST Transmission Volume