How to configure user profile fields in allauth after registration

I used allauth for the registration function in my web application. The socialaccount account registers email users, password, first name and last name. In the login form, I just collect email and password from users.

After registering, my application redirects the user to the profile page, where it is assumed that he will update his profile (including new and configured fields, like the "institution" that I added) if he wants to.

The question is as new to django-allauth, I would like to know how to add these new custom fields to my user profile and how to update this data after user registration.

what i have done so far:

In settings.py

AUTH_USER_MODEL = 'auth.User'

In my_project / models.py

from django.db import models
from django.contrib.auth.models import User

class UserProfile(models.Model):

    institution = models.TextField(max_length=254)

In profile_page.html

{% extends 'base.html' %}
{% block title %}Profile Page{%endblock title%}

{% if user.is_authenticated %}

{%block body%}

    <div class="col-md-6" >
        <div class="panel panel-default">
            <div class="panel-body">
                <h1 class="text-center"><b>MY ONTOLOGIES</b></h1><br>
            </div>
        </div>
    </div>
    <div class="col-md-6" >
        <div class="panel panel-default">
            <div class="panel-body">
                <h1 class="text-center"><b>MY PROFILE</b></h1><br>
                    <div class="form-group">
                        {% csrf_token %}
                        <label for="id_login" class="col-sm-2 control-label">E-mail:</label>
                        <div class="col-sm-10">
                            <input type="email" id="asd" value="{{user.email}}" name="login"   class="form-control" required />
                        </div><br><br>
                        <label for="first_name" class="col-sm-2 control-label">First Name:</label>
                        <div class="col-sm-10">
                            <input type="text" id="id_first_name" value="{{user.first_name}}" name="first_name" class="form-control" required />
                        </div><br><br>
                        <label for="last_name" class="col-sm-2 control-label">Last Name:</label>
                        <div class="col-sm-10">
                            <input type="text" id="id_last_name" value="{{user.last_name}}" name="last_name" class="form-control" required />
                        </div><br><br>
                        <label for="institution" class="col-sm-2 control-label">Institution:</label>
                        <div class="col-sm-10">
                            <input type="text" id="id_institution" value="{{user.institution}}" name="institution" class="form-control" placeholder="University/Company" required />
                        </div><br><br>
                        <label for="id_password1" class="col-sm-2 control-label">New Password:</label>
                        <div class="col-sm-10">
                            <input class="form-control" id="id_password1" name="password1" placeholder="New Password" type="password" />
                        </div><br><br>
                        <label class="col-sm-2" for="id_password2">New Password (again):</label>
                        <div class="col-sm-10">
                            <input class="form-control" id="id_password2" name="password2" placeholder="New Password (again)" type="password" />
                        </div>

                    </div>
            </div>
        </div>
    </div>

{%endblock body%}

{% endif %}

, , .

+4
1

, :

  • ( user_table):

, CustomUser, , allauth.

settings.py

INSTALLED_APPS = [
    ...
    'CustomUser.apps.CustomuserConfig',
]

AUTH_USER_MODEL = 'CustomUser.CustomUser'

CustomUser/models.py

from __future__ import unicode_literals

from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    add_your_custom_fields_here 

CustomUser/apps.py

from __future__ import unicode_literals

from django.apps import AppConfig


class CustomuserConfig(AppConfig):
    name = 'CustomUser'

CustomUser/admin.py

from django.contrib import admin
from .models import CustomUser

admin.site.register(CustomUser)

migrate , . , .

  1. - .

User allauth. form.py CustomUser, :

django .models import CustomUser

class UpdateUserForm(forms.ModelForm):
    class Meta:
        model = CustomUser
        # Add here the fields you want to be present in the update form
        fields = ('first_name', 'last_name', 'institution')

CustomUser/templates/profile.html . :

Update your data <br><br>

<form action="." method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit" />
</form> 

CustomUser/views.py, profile.html

from django.shortcuts import render
from .forms import UpdateUserForm
from django.http import HttpResponseRedirect
from django.views.generic import UpdateView
from django.core.urlresolvers import reverse_lazy

def profile(UpdateView):
    if request.method == 'POST':
        form = UpdateUserForm(request.POST)
        if form.is_valid():
            return HttpResponseRedirect('/accounts/profile/')
    else:
        form = UpdateUserForm()

    return render(request, 'profile.html', {'form':form})

class UserUpdate(UpdateView):
        form_class = UpdateUserForm
        template_name = 'profile.html'
        success_url = reverse_lazy('profile')

        #get object
        def get_object(self, queryset=None): 
            return self.request.user

, profile.html urls.py voilà:

from CustomUser import views

url(r'^accounts/profile/$',  views.UserUpdate.as_view() , name='profile'), 

, -...

+2

Source: https://habr.com/ru/post/1661979/


All Articles