https://www.pythoneatstail.com/en/overview-all-articles/set-allauth-django-project/

 

Set up authentication for a Python Django site - PythonEatsTail

With a few modifications in the logout.html file we copied, we use the self-created blocks card-header and card-body, as follows:

www.pythoneatstail.com

 

 

1. base.py

 

 

pip3 install django-allauth
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',
    ...
)

 

INSTALLED_APPS

'django.contrib.auth',
'django.contrib.messages',
'django.contrib.sites',

'allauth',
'allauth.account',
'allauth.socialaccount',
SITE_ID = 1

 

 

2. urls.py

 

pet/urls.py

from django.urls import path

urlpatterns = [
    ...
    path('accounts/', include('allauth.urls')),
    ...
]

 

 

3. HTML

 

{% extends 'account/base_card.html' %}

{% load i18n static %}

{% block title %}{% trans "Profile" %}{% endblock %}

{% block card-header %}
    <h3>{% trans "Account profile" %}</h3>
{% endblock %}

{% block card-body %}

    <div class="container">
       <div class="row">
            <div class="col border">
                <small>{% trans "First name" %}:</small><br>
                {{ request.user.first_name|default:'' }}
            </div>
            <div class="col border">
                <small>{% trans "Last name" %}:</small><br>
                {{ request.user.last_name|default:'' }}
            </div>
        </div>
    </div>

{% endblock %}

만약에 first_name 하고 last_name이 없을 경우 default를 줘서 공백으로 처리한다.

 

 

4. models.py

 

 

def get_absolute_url(self):
    return reverse('account_profile')

모델이 생성되었을 때, 바로 프로필 페이지로 넘어간다. account_profile은 profile page url의 name이다. 

+ Recent posts