Skip to content

Commit

Permalink
add login page
Browse files Browse the repository at this point in the history
  • Loading branch information
gothack329 committed Mar 2, 2018
1 parent 9470492 commit a336ed8
Show file tree
Hide file tree
Showing 33 changed files with 384 additions and 5 deletions.
Binary file modified article/templatetags/__pycache__/timetonow.cpython-36.pyc
Binary file not shown.
5 changes: 3 additions & 2 deletions article/templatetags/timetonow.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ def timetonow(value):
now = timezone.now()
now = now.replace(tzinfo=None)
delta = now-valuetime
s = delta.seconds

s = delta.total_seconds()
if s>86400:
return '%d天前' % (int(s/86400))
elif s>3600:
return '%d小时前' % (int(s/3600))
elif s>120:
return '%d分钟前' % (int(s/120))
return '%d分钟前' % (int(s/60))
else:
return '刚刚'

Expand Down
Binary file modified db.sqlite3
Binary file not shown.
Binary file modified edconline/__pycache__/settings.cpython-36.pyc
Binary file not shown.
Binary file modified edconline/__pycache__/urls.cpython-36.pyc
Binary file not shown.
Binary file modified edconline/__pycache__/views.cpython-36.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions edconline/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@
path('tinymce/', include('tinymce.urls')),
path('article/', include('article.urls')),
path('userpage/', include('userpage.urls')),
path('utils/', include('utils.urls')),
]
8 changes: 8 additions & 0 deletions edconline/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import userpage
from . import globalarg


# Create your views here.

def homepage(request):
Expand All @@ -33,3 +34,10 @@ def homepage(request):
return render(request,'index.html',{'cover':cover,'arts':arts})









Binary file added static/fonts/Monaco.ttf
Binary file not shown.
Binary file added static/upload/church_JNMUCQr.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ <h1>{% if title %}{{title}}{% endif %} </h1>
&nbsp;&nbsp;&nbsp;
<span class="glyphicon glyphicon-eye-open fad"> </span>&nbsp;<span class="fad">{{i.readtime}}</span>
&nbsp;&nbsp;&nbsp;
<span class="glyphicon glyphicon-tag fad"> </span>&nbsp;<span class=" fad">{{i.tag}}</span>
<span class="glyphicon glyphicon-tag fad"> </span>&nbsp;<span class=" fad">{% if i.tag %}{{i.tag}}{% endif %}</span>
<span class="pull-right">
<span><a href="/article/{{i.id}}/">阅读全文</a></span>
</span>
Expand Down
Binary file added userpage/__pycache__/forms.cpython-36.pyc
Binary file not shown.
Binary file modified userpage/__pycache__/urls.cpython-36.pyc
Binary file not shown.
Binary file modified userpage/__pycache__/views.cpython-36.pyc
Binary file not shown.
80 changes: 80 additions & 0 deletions userpage/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from django.core.exceptions import ValidationError
from django import forms
from django.forms import fields
from django.forms import widgets
from django.core.validators import RegexValidator
from django.contrib.auth.models import User


class RegisterForm(forms.Form):
username = fields.CharField(
required=True,
widget=widgets.TextInput(attrs={'class': "form-control",'placeholder': '用户名为8-12个字符'}),
min_length=6,
max_length=12,
strip=True,
error_messages={'required': '标题不能为空',
'min_length': '用户名最少为6个字符',
'max_length': '用户名最不超过为20个字符'},
)
email = fields.EmailField(
required=True,
widget=widgets.TextInput(attrs={'class': "form-control",'placeholder': '请输入邮箱'}),
#strip=True,
error_messages={'required': '邮箱不能为空',
'invalid':'请输入正确的邮箱格式'}
)
pwd = fields.CharField(
widget=widgets.PasswordInput(attrs={'class': "form-control",'placeholder': '请输入密码,必须包含数字,字母,特殊字符'},render_value=True),
required=True,
min_length=6,
max_length=12,
strip=True,
validators=[
# 下面的正则内容一目了然,我就不注释了
RegexValidator(r'((?=.*\d))^.{6,12}$', '必须包含数字'),
RegexValidator(r'((?=.*[a-zA-Z]))^.{6,12}$', '必须包含字母'),
RegexValidator(r'((?=.*[^a-zA-Z0-9]))^.{6,12}$', '必须包含特殊字符'),
RegexValidator(r'^.(\S){6,10}$', '密码不能包含空白字符'),
], #用于对密码的正则验证
error_messages={'required': '密码不能为空!',
'min_length': '密码最少为6个字符',
'max_length': '密码最多不超过为12个字符!',},
)
pwd_again = fields.CharField(
#render_value会对于PasswordInput,错误是否清空密码输入框内容,默认为清除,我改为不清楚
widget=widgets.PasswordInput(attrs={'class': "form-control",'placeholder': '请再次输入密码!'},render_value=True),
required=True,
strip=True,
error_messages={'required': '请再次输入密码!',}

)

def clean_username(self):
# 对username的扩展验证,查找用户是否已经存在
username = self.cleaned_data.get('username')
users = User.objects.filter(username=username).count()
if users:
raise ValidationError('用户已经存在!')
return username

def clean_email(self):
# 对email的扩展验证,查找用户是否已经存在
email = self.cleaned_data.get('email')
email_count = User.objects.filter(email=email).count() #从数据库中查找是否用户已经存在
if email_count:
raise ValidationError('该邮箱已经注册!')
return email

def _clean_new_password2(self): #查看两次密码是否一致
password1 = self.cleaned_data.get('pwd')
password2 = self.cleaned_data.get('pwd_again')
if password1 and password2:
if password1 != password2:
# self.error_dict['pwd_again'] = '两次密码不匹配'
raise ValidationError('两次密码不匹配!')

def clean(self):
#是基于form对象的验证,字段全部验证通过会调用clean函数进行验证
self._clean_new_password2() #简单的调用而已

68 changes: 68 additions & 0 deletions userpage/templates/userpage/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{% extends 'base.html' %}
{% load timetonow %}

{% block header %}
<title> 物志 › 登录 </title>
{% endblock %}

{% block banner %}
<div style="height:30px">
<p></p>
</div>
{% endblock %}

{% block content %}
<div class="card">
<form class="form-inline " method="post" action="" novalidate>{% csrf_token %}
<a href="/">物志</a> › 登录
<hr>
<p style="text-align:center">
<label style="width:100px;text-align:right" for="username">用户名:</label>
<input type="text" name="username" value="gothack" class="form-control" placeholder="请输入用户名" maxlength="12" minlength="6" required="" id="id_username">
</p>

<p style="text-align:center">
<label style="width:100px;text-align:right" for="password">密码:</label>
<input type="password" name="password" class="form-control" placeholder="请输入密码" maxlength="12" minlength="6" required="" id="id_password">
</p>

<p style="text-align:center">
<label style="width:100px;text-align:right"></label>
<img style="display:inline-block;" id="check_code_img" src="/utils/check_code/" onclick="refresh_check_code(this)"></p>
<p style="text-align:center">
<label style="width:100px;text-align:right" for="check_code">验证码:</label>
<input type="text" class="form-control" name="check_code" id="check_code" placeholder="请输入验证码">
</p>

<p style="text-align:center">
<label style="width:100px;text-align:right"><input type="checkbox" value="1" name="auto_login"></label>
一个月内自动登陆
<span class="pull-right"><a href="#">忘记密码?</a></span>
</p>
<p style="text-align:center">
<button class="btn btn-primary" type="submit" >登录</button>
</p>
</form>
</div>


{% if errors %}
{% for error in errors %}
<script type="text/javascript">
$.notify({
message: '{{ error|escape }}',
},{
type: 'warning',
delay: 5000,
timer: 1000,
animate: {
enter: 'animated flipInY',
exit: 'animated flipOutX'
},
},);
</script>
{% endfor %}
{% endif %}


{% endblock %}
2 changes: 1 addition & 1 deletion userpage/templates/userpage/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
{% endfor %}
{% endif %}


{% endblock %}

{% block banner %}
Expand Down Expand Up @@ -128,6 +127,7 @@
{% ifequal member.id user.id %}
<div class="profile-tab" >
{# 消息提醒 #}
<p></p>
{% for i in notice %}
<div class="row member_comments" style="padding-left:20px">
<p style="background-color: #eee;padding:2px"><a href="/userpage/member/{{i.comment.user}}/">{{i.comment.user}}</a><a href="/article/{{i.comment.article.id}}/">{{i.comment.article}}</a> 的评论中提到了您 <span class="pull-right">{{i.comment_time|timetonow}}</span></p>
Expand Down
4 changes: 3 additions & 1 deletion userpage/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@

urlpatterns = [
path('member/<str:username>/', views.profile, name='profile'),
#path('update/', views.update_profile, name='update_profile'),
path('login/', views.userlogin, name='login'),
path('logout/', views.logout, name='logout'),
path('register/', views.register, name='register'),
]
74 changes: 74 additions & 0 deletions userpage/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from django.contrib import messages
from article.models import *
from notification.models import *
from . import forms
from django.contrib.auth import authenticate, login

# Create your views here.

Expand Down Expand Up @@ -57,6 +59,78 @@ def profile(request, username):



def userlogin(request):

if request.method == 'POST':
# print(request.POST)
username = request.POST['username']
password = request.POST['password']
errors = []

post_check_code = request.POST.get('check_code')
session_check_code = request.session['check_code']
user = authenticate(request, username=username, password=password)
if user is not None:
if post_check_code.lower() == session_check_code.lower() :
login(request, user)
if request.POST.get('auto_login'):
request.session.set_expiry(60 * 60 * 24 *30)
return redirect('/')
else:
errors.append('请输入正确的验证码!')
else:
errors.append('用户名不存在或密码错误!')
return render(request, 'userpage/login.html', {'errors':errors})
else:
return render(request,'userpage/login.html')

def logout(request):
try:
#删除is_login对应的value值
del request.session['is_login']
del request.session['user']
except KeyError:
pass
#点击注销之后,直接重定向回登录页面
return redirect('/')

def register(request):
# username = models.CharField(max_length=16, verbose_name='用户名')
# password = models.CharField(max_length=16, verbose_name='密码')
# nickname = models.CharField(max_length=16,verbose_name='昵称')
# email = models.EmailField(max_length=16, verbose_name='邮箱')
# img = models.ImageField(verbose_name='头像',upload_to='static/img/user/',default='static/img/user/1.jpg')
# ctime = models.DateTimeField(auto_created=True,verbose_name='创建时间')

if request.method == 'GET':
obj = forms.Register()
# return render(request,'register.html',{'form':obj})
elif request.method == 'POST':
# print(request.POST)
obj = forms.Register(request.POST)
post_check_code = request.POST.get('check_code')
session_check_code = request.session['check_code']
print(post_check_code,session_check_code)
if obj.is_valid():
if post_check_code == session_check_code:
# values = obj.clean()
data = obj.cleaned_data
print(data)
# models.User.objects.create(
username= data.get('username')
password= data.get('pwd')
email= data.get('email')
nickname = data.get('username')
# )
models.User.objects.create(username=username,nickname =nickname,password =password,email = email )
request.session['is_login'] = 'true'
request.session['user'] = data.get('username')
return redirect('/')
else:
errors = obj.errors
print('hello')

return render(request,'userpage/register.html',{'form':obj})



Expand Down
Binary file added utils/Monaco.ttf
Binary file not shown.
Empty file added utils/__init__.py
Empty file.
Binary file added utils/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file added utils/__pycache__/check_code.cpython-36.pyc
Binary file not shown.
Binary file added utils/__pycache__/urls.cpython-36.pyc
Binary file not shown.
Binary file added utils/__pycache__/views.cpython-36.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions utils/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions utils/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class UtilsConfig(AppConfig):
name = 'utils'
Loading

0 comments on commit a336ed8

Please sign in to comment.