def register(request): context = {} regis_form = RegistrationForm(request.POST) context['regis_form'] = regis_form context['search_form'] = SearchForm() if request.method == 'GET': return redirect(request.POST['next']) if not regis_form.is_valid(): return HttpResponse(regis_form.errors) new_user = User.objects.create_user(username=regis_form.cleaned_data['username'], email=regis_form.cleaned_data['email'], password=regis_form.cleaned_data['password1']) new_user.save() new_user = authenticate(username=regis_form.cleaned_data['username'], email=regis_form.cleaned_data['email'], password=regis_form.cleaned_data['password1']) profile = Profile(user = new_user) profile.save() # Update the user_vector update_user_vector_all(new_user) # Update the vector movies_all = Movie.objects.all() for m in movies_all: s = smart_bytes(m.vector, encoding='utf-8', strings_only=False, errors='strict') s += ',0' m.vector = s m.save() auto_login(request, new_user) return redirect(request.POST['next'])
def login(request): if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') user = authenticate(request, username=username, password=password) if user: auto_login(request, user) return HttpResponseRedirect(reverse('dashboard')) else: messages.error(request, 'Invalid User Credentials !!') return render(request, 'index.html')
def log_in(request): if request.method == 'GET': return redirect('/') login_form = LoginForm(request, data= request.POST) if login_form.is_valid(): auto_login(request, login_form.get_user()) return redirect(request.POST['next']) else: return HttpResponse("error")
def activate(request, uidb64, token): try: uid = force_text(urlsafe_base64_decode(uidb64)) user = User.objects.get(pk=uid) except (TypeError, ValueError, OverflowError, User.DoesNotExist): user = None if user is not None and account_activation_token.check_token(user, token): user.is_active = True user.save() auto_login(request, user) return render(request, 'accounts/signup_confirm.html', {'user': user})
def login(request): # if not request.session.get('is_login', None): # 代码后面设置了,如果登陆成功,session里会加入is_login,不允许重复登录 # if request.COOKIES.get("is_login",None): # return redirect('/') ssession_look = request.session.flush() cookie_look = request.COOKIES print("ssession_look", ssession_look) print("cookie_look", cookie_look) if request.method == 'POST': login_form = forms.UserForm(request.POST) #把request.POST转成form表单类 message = '请检查填写的内容!' if login_form.is_valid( ): #cleaned_data {'password': '******', 'username': '******', 'captcha': ['90392e2c93228b7fc4cafc438df139659a715991', '']} username = login_form.cleaned_data.get( 'username' ) #cleaned_data: html中input标签里输入的内容,字典类型,input标签中,name=username password = login_form.cleaned_data.get('password') # try: # user = models.Userprofile.objects.get(Q(username=username) | Q(mobile=username)) #将表单验证过的数据拿出来,创建models # except : # message = '用户不存在!' #通过locals() 传入模板中的{{ message }} # return render(request, 'login/login.html', locals()) # if not Userprofile.has_confirmed: #判断user的has_confirmed字段 # message = '该用户还未经过邮件确认!' # return render(request, 'login/login.html', locals()) user = authenticate(username=username, password=password) if user: # if Userprofile.password == hash_code(password) or user.password == password: #用户、有限认证、密码都通过后,设置session里字典内容 auto_login(request, user) request.session['is_login'] = True request.session['user_id'] = user.id request.session['user_name'] = user.username print("auth/////////////") # reverse('reverse:userInfo',kwargs={'user_id':10}) url = reverse("blog:index") print(url) response = redirect(url) # response.set_signed_cookie("is_login",True,salt="blog") # response.set_signed_cookie("user_id",user.id,salt="blog") # response.set_signed_cookie("user_name",user.username,salt="blog") return response else: message = '密码不正确!' return render(request, 'login/login.html', locals()) else: return render(request, 'login/login.html', locals()) login_form = forms.UserForm() #清空login_form变量 内容 return render( request, 'login/login.html', locals()) #locals会把本地变量,此处有message、login_form、username、password变量传入模板中
def password_change_complete(request): context = {} context.update(get_form_context()) if request.method == 'GET': return redirect('/profile/'+str(request.user.id)) change_password_form = ChangePasswordForm(request.POST) if not change_password_form.is_valid(): return redirect('not valid') user_to_edit = authenticate(username= request.user.username, password=change_password_form.cleaned_data['old_password']) if user_to_edit is None: return HttpResponse('error') user_to_edit.password = make_password(change_password_form.cleaned_data['new_password1']) user_to_edit.save() user_editted = authenticate(username= user_to_edit.username, password=change_password_form.cleaned_data['new_password1']) auto_login(request, user_editted) return render(request, 'password_change_complete.html', context)