[Django]-Create User and UserProfile on user signup with django-allauth

3๐Ÿ‘

โœ…

How to create a User and a UserProfile on signup?

You can create a UserProfile at the same time you save the CustomSignupForm

def save(self, request):
    user = super(CustomSignupForm, self).save(request)
    user.first_name = self.cleaned_data['first_name']
    user.last_name = self.cleaned_data['last_name']
    user.bio = self.cleaned_data['bio']
    user.save()
    
    # Create your user profile
    UserProfile.objects.create(user=user, first_name=self.cleaned_data['first_name'], last_name=self.cleaned_data['last_name'], email=self.cleaned_data['email'], bio=self.cleaned_data['bio'])

Another elegant way is to use Django signals to perform some actions after that an event occur like user creation.

signals.py

from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import UserProfile

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        first_name = instance.first_name
        last_name = instance.last_name
        email = instance.email
        # The bio field is not set because the User instance has not bio attribute by default.
        # But you can still update this attribute with the profile detail form.
        UserProfile.objects.create(user=instance, first_name=first_name, last_name=last_name, email=email)

If you want to update the profile each time an user is updated, then remove the if created in the signal body.

apps.py

class AppNameConfig(AppConfig):
    
    # some code here

    # import your signal in the ready function
    def ready(self):
        import app_name.signals

1๐Ÿ‘

How to create a User and a UserProfile on signup?

 class CustomSignupForm(SignupForm):
    first_name = forms.CharField(max_length=30, label='First Name')
    last_name = forms.CharField(max_length=30, label='Last Name')
    bio = forms.CharField(max_length=255, label='Bio')
    def save(self, request):
        # create user the create profile
        user = super(CustomSignupForm, self).save(request)
        ### now save your profile 
        profile = UserProfile.objects.get_or_create(user=user)
        profile.first_name = self.cleaned_data['first_name']
        profile.last_name = self.cleaned_data['last_name']
        profile.bio = self.cleaned_data['bio']
        profile.save()
        return user

How can add styling to the forms that come with django-allauth, i.e. at

make a new directory in your templates call it /account/login.html and render your form there and add styles as follow

this can be done in many ways

  1. using https://pypi.org/project/django-bootstrap4/
  2. https://pypi.org/project/django-widget-tweaks/
  3. manually render fields https://simpleisbetterthancomplex.com/article/2017/08/19/how-to-render-django-form-manually.html

How can I modify so that when the user logs in, it will redirect him to /profiles/ instead of /accounts/profiles or is it better in terms of REST principles to have it /accounts/profiles/ if yes, then is it possible to modify the profiles app so that it can use django-allauth views?

go to your setting files and add the following

LOGIN_REDIRECT_URL = "/profiles"

you can check more settings here
https://django-allauth.readthedocs.io/en/latest/configuration.html

Leave a comment