1. URL
name은 {% url 'account_signup' %} 이런식으로 사용이 가능하다.
2. 라이브러리
allauth 에서 제공하는 라이브러리는 아래와 같다.
{% load socialaccount %}
<body>
<h1>hello world</h1>
{% if user.is_authenticated %}
<span>{{ user }}님이 로그인중입니다.</span>
{% for account in user.socialaccount_set.all %}
{% comment %} show avatar from url {% endcomment %}
<h2 style="text-transform:capitalize;">{{ account.provider }} account data</h2>
<p><img width="50" height="50" src="{{ account.get_avatar_url }}"/></p>
<p>UID: <a href="{{ account.extra_data.link }}">{{ account.uid }}</a></p>
<p>Username: {{ account.extra_data.username }}</p>
<p>First Name: {{ account.extra_data.first_name }}</p>
<p>Last Name: {{ account.extra_data.last_name }}</p>
<p>Dashboard Link:
<a href="{{ account.extra_data.link }}">{{ account.extra_data.link }}</a></p>
{% empty %}
<p>you haven't any social account please</p>
{% endfor %}
{% endif %}
<h2><a href="/accounts/login">로그인</a></h2>
<h2><a href="{% provider_login_url 'facebook' method='oauth2' %}">페이스북 로그인</a></h2>
<h2><a href="/accounts/logout">로그아웃</a></h2>
<h2><a href="/accounts/signup">회원가입</a></h2>
</body>
# https://whatisthenext.tistory.com/129
{% load socialaccount %} 로 소셜로그인 정보들을 불러오고, for 문을 돌려 안에 있는 값들을 마음대로 꺼낼 수 있다.
제가 제작하고있는 polls 앱의 경우 일반로그인과 소셜로그인이 다 되게 해두었고, 소셜 로그인으로 로그인 시 로그인 한 사람의 name을 가져오고, 일반로그인을 한 경우 그 로그인한 username을 가져오게 하고 싶었다.
아래의 코드를 작성하였고, {% empty %} 라는 새로운 태그를 알게되었다.
{% empty %} 는 값이 비어있으면 출력되는 구문이다.
제가 작성한 코드는 아래와 같습니다.
{% for account in user.socialaccount_set.all %}
<p style="float:right; font-style:oblique; margin-right:20px; font-weight:bold">{{ account.provider }}로 {{ account.extra_data.name }}님이 로그인 중 입니다.</p>
{% empty %}
{% if user.is_authenticated %}
<p style="float:right; font-style:oblique; margin-right:20px; font-weight:bold">{{ user.username }}님이 로그인 중 입니다.</p>
{% endif %}
{% endfor %}
Google로 로그인하면 empty는 작동되지 않기 때문에 실행되지않고,
일반로그인을 하게 되면 empty가 작동되고, 거기 안에 들어있는 구문이 실행되게 된다.
'Web > Django' 카테고리의 다른 글
Django - admin 페이지 커스터마이징 (0) | 2019.10.29 |
---|---|
Django Trend 프로젝트 정리 (0) | 2019.10.11 |
Django - 소셜 로그인 추가하기(allauth, facebook,google) (0) | 2019.09.06 |
Django - Nginx설치 배포 (0) | 2019.08.27 |
Django - Slug (0) | 2019.08.23 |
댓글