Using Django to write a user registration through the mail verification code. A

views.py:


##The registration function
def vip_reg(request):
if request.method == 'POST':
form = Reg_Form(request.POST, request.FILES)
if form.is_valid():
user = User.objects.create_user(
username = form.cleaned_data['username'],
password = form.cleaned_data['password1'],
email = form.cleaned_data['email'],
sex = form.cleaned_data['sex'],
addr_zz = form.cleaned_data['addr_zz'],
ahjs = form.cleaned_data['ahjs'],

)

user.is_active = False
user.save()
send_email(user)
###Send a letter here. E-mail verification audits? Let is_active=True
return HttpResponseRedirect('/vip/reg/ok/')
else:
return HttpResponse(u'Error occurred when the registered user, please return the modified re registration!')
else:
form = Reg_Form()
variables = RequestContext(request, {'form':form})
return render_to_response('registration/register.html', variables)


def send_email(user):
email = user.email
if isinstance(email, unicode):
email = email.encode('utf-8')
#email_code = uuid.uuid5(uuid.NAMESPACE_DNS, email+str(time.time())).hex


html_content = render_to_string('send_email.html', {'email_code':active_code(email), 'user':user, 'pk':user.id })
subject, form_email, to = u'You registered the site', '', email
text_content = 'aaaaaa123'
msg = EmailMultiAlternatives(subject, text_content, form_email, [to])
msg.attach_alternative(html_content, 'text/html')
msg.send()
return HttpResponse(u'Please check your registered mailbox register!!')

def reguseryz(request, pk, email_code):
#pk = int(pk)
try:
pk = int(pk)
user = User.objects.get(id=pk)
except (User.DoesNotExist,ValueError):
return render_to_response('ok.html')
if user.is_active:
return HttpResponseRedirect('/accounts/login/')
elif email_code == active_code(user.email):
user.is_active = True
user.save()
return render_to_response('ok.html')

def active_code(email):
"""
The activation code
"""
email_code = uuid.uuid5(uuid.NAMESPACE_DNS, email+str(time.time())).hex
return email_code



urls.py:


url(r'^vip/reg/ok/$', 'vipbbs.views.reg_ok'),
url(r'^vip/reguser/(?P<pk>\d+)/(\w+)/$', 'vipbbs.views.reguseryz'),
url(r'^accounts/login/$', 'django.contrib.auth.views.login'),


send_email.html:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
Hello,Welcome to zb.com!




Or copy the links below to the browser



{{pk}}/{{email_code}}
</body>
</html>



Now the question is:

The above code is not run correctly. Can receive mail. Is almost above is covered with writing. Ideas do not know not?

How to put the activation code and the user? I tried if an activation code. It can activate other users. How to let the activation code verification after the first failure?

The above registration through the mail user verification code which friends to help me see?

Started by Clark at January 03, 2017 - 8:43 PM

You generate the activation code in the database without preservation have a state should be very simple.

Posted by Amos at January 03, 2017 - 9:36 PM

Learn not the wrong Oh

Posted by Sharon at January 04, 2017 - 10:35 PM

Have solved.
Is to build a field in models. Used to generate the activation code. Then when the audit out check. After a successful through the audit. And delete the activation code. Thank you

Posted by Clark at January 05, 2017 - 10:55 PM