Home > OS >  Create User and UserProfile on user signup with django-allauth
Create User and UserProfile on user signup with django-allauth

Time:11-16

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)

CodePudding user response:

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 /accounts /login/

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

CodePudding user response:

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.

  • Related