Skip to content

Commit

Permalink
第三方登录
Browse files Browse the repository at this point in the history
  • Loading branch information
stacklens committed Jun 21, 2019
1 parent a173a2c commit 09aa941
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -60,6 +60,7 @@
32. [多级评论](https://www.dusaiphoto.com/article/detail/63/)
33. [消息通知](https://www.dusaiphoto.com/article/detail/64/)
34. [锚点定位](https://www.dusaiphoto.com/article/detail/65/)
35. [第三方登录](https://www.dusaiphoto.com/article/detail/66/)

以及:
- [小功能集合](https://www.dusaiphoto.com/article/detail/53/)
Expand Down
Binary file modified db.sqlite3
Binary file not shown.
Binary file modified my_blog/__pycache__/settings.cpython-37.pyc
Binary file not shown.
Binary file modified my_blog/__pycache__/urls.cpython-37.pyc
Binary file not shown.
22 changes: 21 additions & 1 deletion my_blog/settings.py
Expand Up @@ -38,6 +38,14 @@
'django.contrib.messages',
'django.contrib.staticfiles',

'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
# 可添加需要的第三方登录
'allauth.socialaccount.providers.github',
'allauth.socialaccount.providers.weibo',

'password_reset',
'taggit',
'ckeditor',
Expand Down Expand Up @@ -146,7 +154,7 @@
# 是否使用 TLS
EMAIL_USE_TLS = True
# 默认的发件人
DEFAULT_FROM_EMAIL = 'xxx blog <your email>'
DEFAULT_FROM_EMAIL = 'your email'

# 媒体文件地址
MEDIA_URL = '/media/'
Expand Down Expand Up @@ -181,3 +189,15 @@
'extraPlugins': ','.join(['codesnippet', 'prism', 'widget', 'lineutils']),
}
}

AUTHENTICATION_BACKENDS = (
# 此项使 Django 后台可独立于 allauth 登录
'django.contrib.auth.backends.ModelBackend',
# 配置 allauth 独有的认证方法,如 email 登录
'allauth.account.auth_backends.AuthenticationBackend',
)

# 设置站点
SITE_ID = 1
# 重定向 url
LOGIN_REDIRECT_URL = '/'
6 changes: 6 additions & 0 deletions my_blog/urls.py
Expand Up @@ -7,10 +7,14 @@

import notifications.urls

from article.views import article_list


# 存放了映射关系的列表
urlpatterns = [
path('admin/', admin.site.urls),
# home
path('', article_list, name='home'),
# 重置密码app
path('password-reset/', include('password_reset.urls')),
# 新增代码,配置app的url
Expand All @@ -23,5 +27,7 @@
path('inbox/notifications/', include(notifications.urls, namespace='notifications')),
# notice
path('notice/', include('notice.urls', namespace='notice')),
# django-allauth
path('accounts/', include('allauth.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
10 changes: 10 additions & 0 deletions requirements.txt
@@ -1,13 +1,23 @@
certifi==2019.6.16
chardet==3.0.4
defusedxml==0.6.0
Django==2.1
django-allauth==0.39.1
django-ckeditor==5.6.1
django-js-asset==1.2.2
django-model-utils==3.1.2
django-mptt==0.10.0
django-notifications-hq==1.5.0
django-password-reset==2.0
django-taggit==0.23.0
idna==2.8
jsonfield==2.0.2
Markdown==2.6.11
oauthlib==3.0.1
Pillow==5.3.0
Pygments==2.2.0
python3-openid==3.1.0
pytz==2018.5
requests==2.22.0
requests-oauthlib==1.2.0
urllib3==1.25.3
76 changes: 76 additions & 0 deletions templates/account/login.html
@@ -0,0 +1,76 @@
{% extends "base.html" %}

{% load i18n %}
{% load account socialaccount %}

{% block title %}登录{% endblock %}

{% block content %}
<div class="container">
<div class="row">
<div class="col-12">
<br>

{% get_providers as socialaccount_providers %}

{% if socialaccount_providers %}
<p>
{% blocktrans with site.name as site_name %}请登录已有本地账号或<a href="{{ signup_url }}">注册</a>新账号
也可以通过第三方登录:{% endblocktrans %}
</p>

<div class="socialaccount_ballot">
<h5 class="mb-2 mt-4">第三方登录</h5>
<ul class="socialaccount_providers">
{% include "socialaccount/snippets/provider_list.html" with process="login" %}
</ul>
<h5 class="mb-2 mt-4">本地登录</h5>
</div>

{% include "socialaccount/snippets/login_extra.html" %}

{% else %}
<p>{% blocktrans %}If you have not created an account yet, then please
<a href="{{ signup_url }}">sign up</a> first.{% endblocktrans %}</p>
{% endif %}
<div class="col-6">
<form class="login" id="login_form" method="POST" action="{% url 'account_login' %}">
{% csrf_token %}
<div class="form-group">
<label for="id_login">账号: </label>
<input type="text" name="login" placeholder="请输入用户名或Email" autofocus="autofocus" required
id="id_login" class="form-control" />
<small class="form-text text-muted ml-1">
还没有账号
<a href="{% url 'account_signup' %}" style="color: cornflowerblue; ">
注册新账号
</a>
</small>
</div>
<div class="form-group mb-1">
<label for="id_password">
密码:
</label>
<input type="password" name="password" placeholder="请输入密码" required id="id_password"
class="form-control" />
<small class="form-text text-muted ml-1">
<a class="secondaryAction layui-text" href="{% url 'account_reset_password' %}">
忘记密码?
</a>
</small>
</div>
<div class="custom-control custom-checkbox mb-2">
<input type="checkbox" name="remember" id="id_remember" checked class="custom-control-input" />
<label for="id_remember" class="custom-control-label">
保持登录
</label>
</div>
<button class="primaryAction btn btn-primary" type="submit" hidden id="submit_login">确认</button>
<button class="primaryAction btn btn-primary" type="button" id="on_submit_login">确认</button>
</form>
</div>
</div>
</div>
</div>

{% endblock %}
2 changes: 1 addition & 1 deletion templates/header.html
Expand Up @@ -46,7 +46,7 @@
<!-- 如果用户未登录则显示登录-->
{% else %}
<li class="nav-item">
<a class="nav-link" href="{% url 'userprofile:login' %}">登录</a>
<a class="nav-link" href="{% url 'account_login' %}">登录</a>
</li>
<!-- if 语句在这里结束 -->
{% endif %}
Expand Down
Binary file modified userprofile/__pycache__/models.cpython-37.pyc
Binary file not shown.
Binary file modified userprofile/__pycache__/views.cpython-37.pyc
Binary file not shown.
24 changes: 12 additions & 12 deletions userprofile/models.py
@@ -1,9 +1,9 @@
from django.db import models
from django.contrib.auth.models import User
# 引入内置信号
from django.db.models.signals import post_save
# from django.db.models.signals import post_save
# 引入信号接收器的装饰器
from django.dispatch import receiver
# from django.dispatch import receiver


# 用户扩展信息
Expand All @@ -21,14 +21,14 @@ def __str__(self):
return 'user {}'.format(self.user.username)


# 信号接收函数,每当新建 User 实例时自动调用
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
# 旧教程中采用了信号接收函数,在后台添加User时有时会产生bug
# 已采用其他方法实现其功能,废除了此信号接收函数
# @receiver(post_save, sender=User)
# def create_user_profile(sender, instance, created, **kwargs):
# if created:
# Profile.objects.create(user=instance)


# 信号接收函数,每当更新 User 实例时自动调用
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
# @receiver(post_save, sender=User)
# def save_user_profile(sender, instance, created, **kwargs):
# if not created:
# instance.profile.save(by_signal=True)
12 changes: 10 additions & 2 deletions userprofile/views.py
Expand Up @@ -86,8 +86,16 @@ def user_delete(request, id):
@login_required(login_url='/userprofile/login/')
def profile_edit(request, id):
user = User.objects.get(id=id)
# user_id 是 OneToOneField 自动生成的字段
profile = Profile.objects.get(user_id=id)

# 旧教程代码
# profile = Profile.objects.get(user_id=id)
# 新教程代码: 获取 Profile
if Profile.objects.filter(user_id=id).exists():
# user_id 是 OneToOneField 自动生成的字段
profile = Profile.objects.get(user_id=id)
else:
profile = Profile.objects.create(user=user)


if request.method == 'POST':
# 验证修改数据者,是否为用户本人
Expand Down

0 comments on commit 09aa941

Please sign in to comment.