diff --git a/README.md b/README.md index 26bcdf17..8ec1c86c 100644 --- a/README.md +++ b/README.md @@ -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/) diff --git a/db.sqlite3 b/db.sqlite3 index 2e0e0f77..5a2cb954 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/my_blog/__pycache__/settings.cpython-37.pyc b/my_blog/__pycache__/settings.cpython-37.pyc index b2d20b33..90cae7f5 100644 Binary files a/my_blog/__pycache__/settings.cpython-37.pyc and b/my_blog/__pycache__/settings.cpython-37.pyc differ diff --git a/my_blog/__pycache__/urls.cpython-37.pyc b/my_blog/__pycache__/urls.cpython-37.pyc index e8ca9fd0..0f716c7a 100644 Binary files a/my_blog/__pycache__/urls.cpython-37.pyc and b/my_blog/__pycache__/urls.cpython-37.pyc differ diff --git a/my_blog/settings.py b/my_blog/settings.py index 50dcd495..f3ef3e29 100644 --- a/my_blog/settings.py +++ b/my_blog/settings.py @@ -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', @@ -146,7 +154,7 @@ # 是否使用 TLS EMAIL_USE_TLS = True # 默认的发件人 -DEFAULT_FROM_EMAIL = 'xxx blog ' +DEFAULT_FROM_EMAIL = 'your email' # 媒体文件地址 MEDIA_URL = '/media/' @@ -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 = '/' \ No newline at end of file diff --git a/my_blog/urls.py b/my_blog/urls.py index 8e68c320..6574f6e9 100644 --- a/my_blog/urls.py +++ b/my_blog/urls.py @@ -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 @@ -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) diff --git a/requirements.txt b/requirements.txt index f63cba9f..61125bf0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,8 @@ +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 @@ -6,8 +10,14 @@ 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 diff --git a/templates/account/login.html b/templates/account/login.html new file mode 100644 index 00000000..6a7cedaa --- /dev/null +++ b/templates/account/login.html @@ -0,0 +1,76 @@ +{% extends "base.html" %} + +{% load i18n %} +{% load account socialaccount %} + +{% block title %}登录{% endblock %} + +{% block content %} +
+
+
+
+ + {% get_providers as socialaccount_providers %} + + {% if socialaccount_providers %} +

+ {% blocktrans with site.name as site_name %}请登录已有本地账号或注册新账号。 + 也可以通过第三方登录:{% endblocktrans %} +

+ +
+
第三方登录:
+
    + {% include "socialaccount/snippets/provider_list.html" with process="login" %} +
+
本地登录:
+
+ + {% include "socialaccount/snippets/login_extra.html" %} + + {% else %} +

{% blocktrans %}If you have not created an account yet, then please + sign up first.{% endblocktrans %}

+ {% endif %} +
+ +
+
+
+
+ +{% endblock %} diff --git a/templates/header.html b/templates/header.html index bf8d577f..6b348679 100644 --- a/templates/header.html +++ b/templates/header.html @@ -46,7 +46,7 @@ {% else %} {% endif %} diff --git a/userprofile/__pycache__/models.cpython-37.pyc b/userprofile/__pycache__/models.cpython-37.pyc index b7846cf5..73847b5b 100644 Binary files a/userprofile/__pycache__/models.cpython-37.pyc and b/userprofile/__pycache__/models.cpython-37.pyc differ diff --git a/userprofile/__pycache__/views.cpython-37.pyc b/userprofile/__pycache__/views.cpython-37.pyc index 201ac10d..6b1dd83b 100644 Binary files a/userprofile/__pycache__/views.cpython-37.pyc and b/userprofile/__pycache__/views.cpython-37.pyc differ diff --git a/userprofile/models.py b/userprofile/models.py index 8b83bd85..6b1443f8 100644 --- a/userprofile/models.py +++ b/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 # 用户扩展信息 @@ -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() \ No newline at end of file +# @receiver(post_save, sender=User) +# def save_user_profile(sender, instance, created, **kwargs): +# if not created: +# instance.profile.save(by_signal=True) \ No newline at end of file diff --git a/userprofile/views.py b/userprofile/views.py index d7193658..046ba204 100644 --- a/userprofile/views.py +++ b/userprofile/views.py @@ -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': # 验证修改数据者,是否为用户本人