Абстрактный пользователь Django — как реализовать его с помощью allauth

#python #django #django-authentication #django-allauth #django-custom-user

Вопрос:

У меня нет стандартной модели пользователя Django, на которую можно положиться. Как бы вы объединили оба, AbstractUser и Allauth. Должен ли я сначала создать представления «SignUpView.as_view» или мне следует выяснить, как путь «учетные записи/» должен быть связан с allauth?

Я пытался адаптировать функционирующий сайт электронной коммерции, удалив/включив аспекты, одной из этих адаптаций была пользовательская модель пользователя.

Я следую этому уроку:

https://simpleisbetterthancomplex.com/tutorial/2018/01/18/how-to-implement-multiple-user-types-with-django.html

Это отлично, просто не знаю, как разместить allauth, который должен быть в этом проекте из-за простоты его социального входа.

urls.py

 from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static


urlpatterns = [
    path('', include('home.urls')),
    path('admin/', admin.site.urls),
    path('accounts/', include('allauth.urls')),
    path('projects/', include('projects.urls')),
    # path('profiles/', include('profiles.urls')),
    # path('accounts/signup/', users.SignUpView.as_view(), name='signup'),
    # path('accounts/signup/volunteer/', volunteers.VolunteerSignUpView.as_view(), name='volunteer_signup'),
    # path('accounts/signup/organisation/', organisations.OrganisationsSignUpView.as_view(), name='organisation_signup'),
]   static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

 

models.py

 from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.html import escape, mark_safe


class User(AbstractUser):
    is_volunteer = models.BooleanField(default=False)
    is_organisation = models.BooleanField(default=False)



class Volunteer(models.Model):
   
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)

    def __str__(self):
        return self.user.username 
 

Вот мой settings.py файл

settings.py

 ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.linkedin_oauth2',
    'home',
    'projects',
    'profiles' , 

    # Other
    'crispy_forms',

]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'projectSaasy.urls'

CRISPY_TEMPLATE_PACK = 'bootstrap4'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
            os.path.join(BASE_DIR, 'templates', 'allauth'), 
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
            'builtins': [
                'crispy_forms.templatetags.crispy_forms_tags',
                'crispy_forms.templatetags.crispy_forms_field',
            ]
        },
    },
]

AUTHENTICATION_BACKENDS = [
    
    # Needed to login by username in Django admin, regardless of `allauth`
    'django.contrib.auth.backends.ModelBackend',

    # `allauth` specific authentication methods, such as login by e-mail
    'allauth.account.auth_backends.AuthenticationBackend',
    
]

SITE_ID = 1

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_SIGNUP_EMAIL_ENTER_TWICE = True
ACCOUNT_USERNAME_MIN_LENGTH = 4
LOGIN_URL = '/accounts/login/'
LOGIN_REDIRECT_URL = '/'

# Social accounts
# This is the most helpful guide: 
# https://python.plainenglish.io/why-you-should-let-the-user-to-authenticate-with-social-accounts-df30b0ba4793

SOCIALACCOUNT_PROVIDERS = {
    'linkedin': {
        'SCOPE': [
            'r_basicprofile',
            'r_emailaddress'
        ],
        'PROFILE_FIELDS': [
            'id',
            'first-name',
            'last-name',
            'email-address',
            'picture-url',
            'public-profile-url',
        ]
    }
}

# This allows social sign-up to be instantaneous (change back to "True")
SOCIALACCOUNT_AUTO_SIGNUP = True


WSGI_APPLICATION = 'projectSaasy.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

AUTH_USER_MODEL = "profiles.User"


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

 

Вот это forms.py

forms.py

 from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.db import transaction

from profiles.models import Volunteer, User

class VolunteerSignUpForm(UserCreationForm):

    class Meta(UserCreationForm.Meta):
        model = User

    @transaction.atomic
    def save(self):
        user = super().save(commit=False)
        user.is_volunteer = True
        user.save()
        volunteer = Volunteer.objects.create(user=user)
        volunteer.interests.add(*self.cleaned_data.get('interests'))
        return user
 

И в views.py

views.py

 from django.contrib.auth import login
from django.shortcuts import redirect
from django.views.generic import CreateView

from .forms import VolunteerSignUpForm
from .models import User

class VolunteerSignUpView(CreateView):
    model = User
    form_class = VolunteerSignUpForm
    template_name = 'registration/signup_form.html'

 

Ответ №1:

если ваш сигнал работает плохо, вы можете использовать следующие функции сигнала отдельно:

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

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

если все еще не работает, вы можете создать отдельный файл с именем signals.py и скопируйте и вставьте весь мой код, написанный выше.
С уважением, Абубакар

Комментарии:

1. билл, ты можешь обновить свои сигналы так же, как и мои?

Ответ №2:

Сначала удалите свою базу данных и папку «Все миграции», а затем внесите следующие изменения

В вашем models.py, внесите несколько изменений

 from django.conf import settings

class Volunteer(models.Model):
   
    user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, primary_key=True)

    def __str__(self):
        return self.user.username 
 

И зарегистрируйте эту модель в admin.py

 from django.contrib import admin

from .models import *

admin.site.register(User)
 

После
внесения следующих изменений выполните следующую команду

 python manage.py makemigrations <your_app_name_in_which_AbstractUserModel_is_present>
python manage.py migrate