예제 #1
0
def reg(request):
    context = {}
    context['return_to'] = request.META.get('HTTP_REFERER')
    context[
        'extra_content'] = '<button type="button" style="margin-top:1em;margin-left:2em;" id="send_code" class="btn btn-primary">发送验证码</button>'
    if request.method == 'GET':
        reg_form = forms.RegForm(data=None, request=None)
        context['reg_form'] = reg_form
        return render(request, 'register.html', context)
    else:
        reg_form = forms.RegForm(data=request.POST, request=request)
        if reg_form.is_valid():
            username = reg_form.cleaned_data['username']
            email = reg_form.cleaned_data['email']
            password = reg_form.cleaned_data['password']
            # 创建用户
            user = auth.models.User.objects.create_user(
                username, email, password)
            user.save()
            # 清除session
            del request.session['register']
            # 登录
            user = auth.authenticate(username=username, password=password)
            auth.login(request, user)
            if request.POST.get('return_to') == 'None':
                return redirect('/')
            else:
                return redirect(request.POST.get('return_to'))
        else:
            context['reg_form'] = reg_form
            return render(request, 'register.html', context)
예제 #2
0
파일: views.py 프로젝트: LXianB/blog02
def register(request):
    if request.method == "POST":
        ret = {"status": 0, "msg": ""}
        form_obj = forms.RegForm(request.POST)
        # 帮我做校验
        if form_obj.is_valid():
            # 校验通过,去数据库创建一个新的用户
            form_obj.cleaned_data.pop("re_password")
            avatar_img = request.FILES.get("avatar")  # 这是头像
            print(avatar_img.name)
            models.User.objects.create_user(**form_obj.cleaned_data,
                                            avatar=avatar_img)
            ret["msg"] = "/article/index/"
            return JsonResponse(ret)
        else:
            print(form_obj.errors)
            ret["status"] = 1
            ret["msg"] = form_obj.errors
            print(ret)
            print("=" * 120)
            return JsonResponse(ret)
    # 生成一个form对象
    form_obj = forms.RegForm()
    # print(form_obj.fields)
    return render(request, "register.html", {"form_obj": form_obj})
예제 #3
0
파일: views.py 프로젝트: LXianB/blog2
class Set(LoginRequiredMixin, View):
    form_obj = forms.RegForm()

    def get(self,request):
        return render(request,'user_set.html',{"form_obj": self.form_obj})

    def post(self,request):
        pass
예제 #4
0
파일: views.py 프로젝트: LXianB/blog02
class Register(View):
    form_obj = forms.RegForm()

    def get(self, request):
        '''显示注册页面'''
        return render(request, 'register.html', {"form_obj": self.form_obj})

    def post(self, request):
        '''注册页面逻辑处理'''
        ret = {"status": 0, "msg": ""}
        form_obj = forms.RegForm(request.POST)
        # 帮我做校验
        if form_obj.is_valid():
            # 校验通过,去数据库创建一个新的用户
            form_obj.cleaned_data.pop("re_password")
            avatar_img = request.FILES.get("avatar")  # 这是头像
            print(avatar_img.name)
            models.User.objects.create_user(**form_obj.cleaned_data,
                                            avatar=avatar_img)
            user_name = form_obj.cleaned_data.get('username')
            user = models.User.objects.filter(username=user_name).first()
            # print(user.nid)
            # print(user.username)

            # 发送激活邮件:包含激活链接:http://127.0.0.1:8000/user/active/(user_id)
            # 激活链接中需要包含用户信息(并且要把身份信息加密)

            # 加密用户的身份信息,生成激活token
            serializer = Serializer(settings.SECRET_KEY, 300)  # 加密的秘钥,过期时间300秒
            info = {'confirm': user.nid}
            token = serializer.dumps(info).decode('utf8')
            # print(token)

            # # 发邮件
            # subject = '博客欢迎信息'
            # message = ''
            # send = settings.EMAIL_FROM
            # recipient = [user.email]    # 收件人邮箱,一次可以发送给多个邮箱
            # html_message = "<h2>%s,欢迎注册本博客,恭喜您成为本博客用户</h2>,请点击以下链接激活账号<a href='http://127.0.0.1:8000/user/active/%s'>http://127.0.0.1:8000/user/active/%s</a>"%(user.username,token,token)
            # # send_mail阻塞执行,等到邮件发送了,页面才能跳转到index,可以使用celery异步执行,
            # send_mail(subject, message, send, recipient_list=recipient,html_message=html_message)

            # 采用celery发送邮件
            send_register_active_email.delay(user, token)
            # 处理者:Linux系统中,把项目文件导过去
            # 在项目目录下,执行 celery -A celery_tasks.tasks worker -l info

            ret["msg"] = "/article/index/"
            return JsonResponse(ret)

        else:
            print(form_obj.errors)
            ret["status"] = 1
            ret["msg"] = form_obj.errors
            print(ret)
            print("=" * 120)
            return JsonResponse(ret)