Skip to content

Commit 2093a78

Browse files
author
yangxueguang
committed
完善登录模板,添加微博登录按钮
1 parent 10694d0 commit 2093a78

File tree

9 files changed

+79
-20
lines changed

9 files changed

+79
-20
lines changed

accounts/forms.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from django.contrib.auth.forms import AuthenticationForm
2+
from django.forms import widgets
3+
4+
from registration.forms import RegistrationFormUniqueEmail
5+
6+
7+
class LoginForm(AuthenticationForm):
8+
def __init__(self, *args, **kwargs):
9+
super(LoginForm, self).__init__(*args, **kwargs)
10+
self.fields['username'].widget = widgets.TextInput(attrs={'placeholder': "用户名"})
11+
self.fields['password'].widget = widgets.PasswordInput(attrs={'placeholder': "密码"})
12+
13+
14+
class RegisterForm(RegistrationFormUniqueEmail):
15+
def __init__(self, *args, **kwargs):
16+
super(RegisterForm, self).__init__(*args, **kwargs)
17+
self.fields['username'].widget = widgets.TextInput(attrs={'placeholder': "用户名"})
18+
self.fields['email'].widget = widgets.EmailInput(attrs={'placeholder': "邮箱"})
19+
self.fields['password1'].widget = widgets.PasswordInput(attrs={'placeholder': "密码"})
20+
self.fields['password2'].widget = widgets.PasswordInput(attrs={'placeholder': "确认密码"})

accounts/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
# Create your models here.
66
class BlogUser(AbstractUser):
77
nickname = models.CharField('昵称', max_length=50, blank=True)
8-
mugshot = models.ImageField('头像', upload_to='upload/mugshots')
8+
mugshot = models.ImageField('头像', upload_to='upload/mugshots', blank=True)

accounts/urls.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.conf.urls import url
2+
from django.contrib.auth import views as auth_view
3+
4+
from . import views
5+
6+
from .forms import LoginForm
7+
8+
urlpatterns = [
9+
url(r'^login/$', auth_view.login, name='login', kwargs={'authentication_form': LoginForm}),
10+
url(r'^register/$', views.RegisterView.as_view(), name='register'),
11+
]

accounts/views.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
from django.shortcuts import render
22

3+
from registration.backends.default.views import RegistrationView
4+
5+
from .forms import RegisterForm
6+
7+
38
# Create your views here.
9+
class RegisterView(RegistrationView):
10+
form_class = RegisterForm

blog/templates/registration/login.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ <h1 class="page-title">登录</h1>
2424
<span class="social-text">社交账号登录</span>
2525
</div>
2626
<div class="social-icon">
27-
<a href=""><i class="icon-sina-weibo"></i></a>
28-
<a href=""><i class="icon-github"></i></a>
27+
<a href="{{ weibo }}"><i class="icon-sina-weibo"></i></a>
28+
{# <a href=""><i class="icon-github"></i></a>#}
2929
</div>
3030

3131
</div>

blog/templates/registration/registration_form.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ <h1 class="page-title">注册</h1>
1111
{% csrf_token %}
1212
<div class="row">
1313
<div class="col-md-4 col-md-offset-4">
14+
{{ form.non_field_errors }}
1415
{% for field in form %}
1516
{{ field }}
17+
{{ field.errors }}
1618
{% endfor %}
1719
<span class="login-text">已有账号?<a href="{% url 'login' %}">立即登录</a></span>
1820

blog_project/urls.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,26 @@
1515
"""
1616
from django.conf.urls import url, include
1717
from django.contrib import admin
18-
from blog import views
18+
from django.views.static import serve
19+
from django.conf import settings
1920

2021
urlpatterns = [
2122
url(r'^admin/', admin.site.urls),
2223
url(r'', include('blog.urls', namespace='blog', app_name='blog')),
2324
url(r'', include('comments.urls')),
2425
url(r'', include('django_comments.urls')),
26+
url(r'', include('accounts.urls')),
2527
url(r'', include('registration.backends.default.urls')),
2628
url(r'', include('django.contrib.auth.urls')),
2729
url(r'^search/', include('haystack.urls')),
2830
url('^notifications/', include('notifications.urls', namespace='notifications')),
2931
url(r'^likes/', include('likes.urls', namespace='pinax_likes')),
3032
url(r'', include('social_login.urls', namespace='social_login')),
3133
]
34+
35+
if settings.DEBUG:
36+
urlpatterns += [
37+
url(r'^media/(?P<path>.*)$', serve, {
38+
'document_root': settings.MEDIA_ROOT,
39+
}),
40+
]

comments/templates/comments/list.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
{% load pinax_likes_tags %}
44
{% for parent in comment_list %}
55
<div class="comment-panel">
6-
<img src="{% static 'blog/img/4040demo.jpg' %}">
7-
<a href=""><span>{{ parent.name }}</span></a>
6+
<img src="
7+
8+
{% if parent.user.social_user %}{{ parent.user.mugshot }}{% else %}{{ parent.user.mugshot.url }}{% endif %}">
9+
<a href=""><span>{{ parent.user.nickname }}</span></a>
810
<div class="comment-content">
911
{{ parent.comment }}
1012
</div>
@@ -16,8 +18,9 @@
1618

1719
{% for child in parent.children.all %}
1820
<div class="inner-panel">
19-
<img src="{% static 'blog/img/4040demo.jpg' %}">
20-
<span>{{ child.name }}</span> · <span>{{ child.submit_date |timesince }}前</span>
21+
<img src="
22+
{% if child.user.social_user %}{{ child.user.mugshot }}{% else %}{{ child.user.mugshot.url }}{% endif %}">
23+
<span>{{ child.user.nickname }}</span> · <span>{{ child.submit_date |timesince }}前</span>
2124
<a href="">回复</a>
2225
<div class="comment-content">
2326
{{ child.comment }}

social_login/context_processors.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,23 @@
1616
# `social_sites` is a lazy object, it readly called just access the `social_sites`
1717

1818

19+
# def social_sites(request):
20+
# def _social_sites():
21+
# def make_site(s):
22+
# s = import_oauth_class(s)()
23+
# return {
24+
# 'site_name': s.site_name,
25+
# 'site_name_zh': s.site_name_zh,
26+
# 'authorize_url': s.authorize_url,
27+
# }
28+
#
29+
# return [make_site(s) for s in socialsites.list_sites()]
30+
#
31+
# return {'social_sites': LazyList(_social_sites)}
32+
1933
def social_sites(request):
20-
def _social_sites():
21-
def make_site(s):
22-
s = import_oauth_class(s)()
23-
return {
24-
'site_name': s.site_name,
25-
'site_name_zh': s.site_name_zh,
26-
'authorize_url': s.authorize_url,
27-
}
28-
29-
return [make_site(s) for s in socialsites.list_sites()]
30-
31-
return {'social_sites': LazyList(_social_sites)}
34+
def make_site(s):
35+
s = import_oauth_class(s)()
36+
return s.authorize_url
37+
38+
return {s.split('.')[-2]: make_site(s) for s in socialsites.list_sites_class()}

0 commit comments

Comments
 (0)