muare / profilesapi

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

profilesapi

Course DRF level 3

Token authentication

Step 1 - installation

  • install django-rest-auth
  • install django-allauth

Step 2 - modification of settings

  • api-auth/ from rest_framework.urls are necessary for browsable API
  • rest-auth/ from rest_auth.urls are ready for REST API
# modification in settings.py

INSTALLED_APPS = [
    ...,
    'rest_framework',
    'rest_framework.authtoken',
    'rest_auth',
]
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    ]
}

Step 3 - modification of urls

urlpatterns = [
    ...,
    url('rest-auth/', include('rest_auth.urls'))
    url('api-auth/', include('rest_framework.urls'))
]

User registration

Step 1 - modificatioin of settings

INSTALLED_APPS = (
    ...,
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'rest_auth.registration',
)

SITE_ID = 1

Step 2 - modification of urls

urlpatterns = [
    ...,
    path('api/rest-auth/registration/', include('rest_auth.registration.urls')),
]

Signals for profile creation automatically when User is newly created

Definition

# signals.py in app
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

Register in app config

class ProfilesConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'profiles'

    def ready(self) -> None:
        import profiles.signals

About


Languages

Language:Python 100.0%