Search
castle

requirements&settings

  • django-allauth, requests, jwt, PyJWT
INSTALLED_APPS = [
...
    "django.contrib.sites",
    

    "allauth",
    "allauth.account",
    "allauth.socialaccount",
    "allauth.socialaccount.providers.google",
...
]

...
MIDDLEWARE = [
    "allauth.account.middleware.AccountMiddleware",
]

...

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
)
SIE_ID = 1
# deprecated
# ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_SIGNUP_FIELDS = ['email*', 'username*', 'password1*', 'passwords2*']
ACCOUNT_EMAIL_VERIFICATION = 'none'
LOGIN_REDIRECT_URL = '/blog/'
LOGOUT_REDIRECT_URL = '/blog/'

...

google register

image.png


image.png

Admin

  • 생성 완료 시 admin 화면에 SITES, SOCIAL_ACCOUNTS 항목 활성화

image.png

  • SITES 항목 추가
    • 기존의 example.com → localhost, 이름 변경(이름은 아무거나)

image.png

  • Social application 항목 추가
    • google cloud auth의 json 항목 작성
    • client id : client_id
    • Secret Key : client_secret

image.png

접속 확인 authontification

html

  • {% load socialaccount %} : load django-allauth
  • href="{% provider_login_url 'google' %}" : google account 연결
  • user.is_authenticated : 로그인 상태 확인
# blog\templates\include\nav.html

{% load socialaccount %}
...
<!-- 유저 정보 display -->
<ul class="navbar-nav ms-auto">
	{% if user.is_authenticated %}
	<li class="nav-item">user : {{ user.username }}</li>
		<a class="nav-link" href="/accounts/logout/">
			logout
		</li>
		
	{% else %}
	<li class="nav-item">
		<a class="nav-link" href="#" data-bs-toggle="modal" data-bs-target="#exampleModal">Log in</a>
	</li>
	{% endif %}
...
  • 댓글
# blog\templates\blog\detail.html
{% load socialaccount %}
...
{% if user.is_authenticated and comment.author == user %}
    <div class="col-lg-2">
        <a href='/blog/{{ post_pk.pk }}/comment_update/{{ comment.id }}' class="mb-4">
            <input type='submit' value='수정' class="btn btn-primary"></input>
        </a>
    </div>

    <div class="col-lg-1">
        <a href='/blog/{{ post_pk.pk }}/comment_delete/{{ comment.id }}' class="mb-4">
            <input type='submit' value='삭제' class="btn btn-primary"></input>
        </a>
    </div>
{% endif %}
...

backend(views)

# blog\views.py

from django.contrib.auth.decorators import login_required

@login_required(login_url='/accounts/google/login/')
def detail(request, pk, commentform=None):
    category_total_list = Category.objects.all() # db SELECT * FROM POST ... ASC
    post_pk = Post.objects.get(pk=pk)
    comments = Comment.objects.filter(post=post_pk).order_by('created_date')
    commentform = CommentForm() if commentform is None else commentform
    
    return render(request,
                  template_name='blog/detail.html',
                  context={'post_pk':post_pk,
                        'categories' : category_total_list,
                        'comments' : comments,
                        'commentform' : commentform,
                        }
                  )
left
right

C

Contents