-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
56 lines (47 loc) · 2.34 KB
/
views.py
File metadata and controls
56 lines (47 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from django.shortcuts import render
from django.utils import timezone
from django.core.mail import send_mail
from block.models import Block
from django.contrib.auth.models import User
from usercenter.models import ActivateCode
import uuid
import datetime
def index(request):
# block_infos = Block.objects.all().order_by("-id")
block_infos = Block.objects.filter(status=0).order_by("-id")
return render(request, "index.html", {"blocks": block_infos})
def register(request):
error = ""
if request.method == "GET":
return render(request, "register.html", {})
else:
username = request.POST['username'].strip()
email = request.POST['email'].strip()
password = request.POST['password'].strip()
re_password = request.POST['re_password'].strip()
if not username or not password or not email or not re_password:
error += "任何字段都不能为空;"
if password != re_password:
error += "两次密码不一致;"
if User.objects.filter(username=username).count() > 0:
error += "用户名已存在;"
if User.objects.filter(email=email).count() > 0:
error += "邮箱已注册;"
if not error:
user = User.objects.create_user(username=username, email=email, password=password, is_active=False)
user.save()
new_code = str(uuid.uuid4()).replace("-", "")
expire_time = timezone.now() + datetime.timedelta(days=2)
code_record = ActivateCode(owner=user, code=new_code, expire_timestamp=expire_time)
code_record.save()
activate_link = "http://%s/activate/%s" % (request.get_host(), new_code)
activate_email = '''点击<a href="%s">这里</a>激活''' % activate_link
send_mail(subject='[Python部落论坛]激活邮件',
message='点击链接激活: %s' % activate_link,
html_message=activate_email,
from_email='290498254@qq.com',
recipient_list=[email],
fail_silently=False)
return render(request, "success_hint.html", {"msg": "注册成功,激活邮件已经发送到您的邮箱,请点击邮箱中的激活链接完成激活"})
else:
return render(request, "register.html", {"error": error})