Sachihiro
Sachihiro

Reputation: 1779

Create User and UserProfile on user signup with django-allauth

I am using django-allauth for taking car of accounts, login, logout, signup, but I need that on create the user must create a profile and I am using the model UserProfile as it can be seen on the code. The problem is that when I created the custom signup form, now it creates a user with [username, email, first_name, last_name, password] but it does not create a UserProfile. And I have three questions:

  1. How to create a User and a UserProfile on signup?
  2. How can add styling to the forms that come with django-allauth, i.e. at /accounts /login/
  3. 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?

My custom signup form:

# django_project/profiles/forms.py
from django import forms
from allauth.account.forms import SignupForm
 
 
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):
        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()
        return user

Settings:

# django_project/django_project/settings.py
ACCOUNT_FORMS = {
    'signup': 'profiles.forms.CustomSignupForm',
}

And main url patterns:

# django_project/django_project/urls.py
urlpatterns = [
    path('admin/', admin.site.urls),
    path('profiles/', include('profiles.urls')),
    path('accounts/', include('allauth.urls')),
] 

Url patterns in profiles app:

# django_project/profiles/urls.py
app_name = 'profiles'
urlpatterns = [
    path('<str:user>/', ProfileView.as_view(), name='profile-detail'),
]

And this is my ProfileView:

class ProfileView(LoginRequiredMixin, View):
    def get(self, request, user, *args, **kwargs):
        profile = UserProfile.objects.get(user=user)
        my_user = profile.user
        context = {
            'user': my_user,
            'profile': profile,
        }
        return render(request, 'profile/profile.html', context)

I have a different user profile than the User model that comes with django user model:

User = settings.AUTH_USER_MODEL

class UserProfile(models.Model):
    user = models.OneToOneField(User, primary_key=True, verbose_name='user',
                                related_name='profile', on_delete=models.CASCADE)
    first_name = models.CharField(max_length=30, blank=True, null=True)
    last_name = models.CharField(max_length=30, blank=True, null=True)
    email = models.CharField(max_length=30, blank=True, null=True)
    bio = models.TextField(max_length=500, blank=True, null=True)

The Signals of user creation:

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)


@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

Upvotes: 3

Views: 2754

Answers (3)

Игорь
Игорь

Reputation: 1

You can hook to allauth.account.signals.user_signed_up(request, user) signal. Add in your models.py, where the UserProfile class is described:

from allauth.account.signals import user_signed_up
from django.conf import settings
from django.contrib.auth.models import User
from django.db import models
from django.dispatch import receiver

@receiver(user_signed_up)
def create_user_profile(request, user, **kwargs):
    UserProfile.objects.create(user=user)

Upvotes: 0

Rvector
Rvector

Reputation: 2432

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

Upvotes: 3

Mohamed Beltagy
Mohamed Beltagy

Reputation: 623

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

Upvotes: 1

Related Questions