现在运行项目就可以发送验证邮件了。然后render页面回去
class RegisterView(View):
def get(self,request):
register_form = RegisterForm()
return render(request,"register.html",{'register_form':register_form})
def post(self,request):
register_form = RegisterForm(request.POST)
if register_form.is_valid():
user_name = request.POST.get("email","")
pass_word = request.POST.get("password","")
user_profile = UserProfile()
user_profile.username = user_name
user_profile.email = user_name
user_profile.is_active = False
user_profile.password = make_password(pass_word)
user_profile.save()
send_register_email(user_name,"register")
return render(request, "login.html")
else:
return render(request, "register.html",{"register_form":register_form})
如果注册时填写信息有误也应该有提示,并且重新填时候输入框里保存着刚才填的内容,这需要在input后面加上个value=""。模仿login.html:
<div class="tab-form">
<form id="email_register_form" method="post" action="{% url 'register' %}" autocomplete="off">
<input type='hidden' name='csrfmiddlewaretoken' value='gTZljXgnpvxn0fKZ1XkWrM1PrCGSjiCZ' />
<div class="form-group marb20 {% if register_form.errors.email %}errorput{% endif %}">
<label>邮 箱</label>
<input type="text" id="id_email" name="email" value="{{ register_form.email.value }}" placeholder="请输入您的邮箱地址" />
</div>
<div class="form-group marb8 {% if register_form.errors.password %}errorput{% endif %}">
<label>密 码</label>
<input type="password" id="id_password" name="password" value="{{ register_form.password.value }}" placeholder="请输入6-20位非中文字符密码" />
</div>
<div class="form-group marb8 captcha1 {% if register_form.errors.captcha %}errorput{% endif %}">
<label>验 证 码</label>
{{ register_form.captcha }}
</div>
<div class="error btns" id="jsEmailTips">{% for key,error in register_form.errors.items %}{{ error }}{% endfor %}{{ msg }}</div>
<div class="auto-box marb8">
</div>
<input class="btn btn-green" id="jsEmailRegBtn" type="submit" value="注册并登录" />
<input type='hidden' name='csrfmiddlewaretoken' value='5I2SlleZJOMUX9QbwYLUIAOshdrdpRcy' />
{% csrf_token %}
</form>
</div>
刚注册完的用户is_active应该为false,验证邮箱后变为True。用户访问了带有验证码的地址对应的响应函数:
from users.models import UserProfile,EmailVerifyRecord
class ActiveUserView(View):
def get(self,request,active_code):
all_records = EmailVerifyRecord.objects.filter(code=active_code)
if all_records:
for record in all_records:
email =record.email
user = UserProfile.objects.get(email=email)
user.is_active = True
user.save()
return render(request,"login.html")
urls.py:
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
from users.views import LoginView, RegisterView,ActiveUserView
import xadmin
urlpatterns = [
url(r'^xadmin/', xadmin.site.urls),
url('^$', TemplateView.as_view(template_name="index.html"),name="index"),
url('^login/$', LoginView.as_view(),name="login"), #as_view()方法判断是post还是get然后返回响应函数名
url('^register/$', RegisterView.as_view(),name="register"),
url(r'^captcha/', include('captcha.urls')),
url('^active/(?P<active_code>.*)/$', ActiveUserView.as_view(),name="user_active"),
]
其中(?P<active_code>.*)用来匹配验证码并把它保存到active_code里,传到响应函数,响应函数检查验证码是否在数据库里存在,如果存在则把对应的用户全都active,返回登录网页。否则报错。但不知道为什么我的pycharm的variables一直显示{NameError}name 'register_form' is not defined。虽然也可以正常完成上述功能。
在Templates里新建一个active_fail.html,在邮箱验证链接不对时候显示链接失效
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>链接失效</p>
</body>
</html>
views.py里
class ActiveUserView(View):
def get(self,request,active_code):
all_records = EmailVerifyRecord.objects.filter(code=active_code)
if all_records:
for record in all_records:
email =record.email
user = UserProfile.objects.get(email=email)
user.is_active = True
user.save()
else:
return render(request,"active_fail.html")
return render(request, "login.html")
用户以注册过的邮箱注册要报错,:
class RegisterView(View):
def get(self,request):
register_form = RegisterForm()
return render(request,"register.html",{'register_form':register_form})
def post(self,request):
register_form = RegisterForm(request.POST)
if register_form.is_valid():
user_name = request.POST.get("email","")
if UserProfile.objects.filter(email=user_name):
return render(request, "register.html", {"register_form": register_form,"msg":"用户已存在"})
pass_word = request.POST.get("password","")
user_profile = UserProfile()
user_profile.username = user_name
user_profile.email = user_name
user_profile.is_active = False
user_profile.password = make_password(pass_word)
user_profile.save()
send_register_email(user_name,"register")
return render(request, "login.html")
else:
return render(request, "register.html",{"register_form":register_form})
网友评论