def login(request): """登录""" """ username = request.POST.get('username',None) password = request.POST.get('password',None) user = auth.authenticate(request, username=username, password=password) referer = request.META.get('HTTP_REFERER',reverse('home')) print(user) if user is not None: auth.login(request, user) # Redirect to a success page. return redirect(referer) else: # Return an 'invalid login' error message. context = {} context['message'] = '用户名或密码错误' return render(request,'error.html',context) """ if request.method == "POST": login_form = LoginForm(request.POST) #从forms表单获取数据 if login_form.is_valid(): #检查数据是否正确 user = login_form.cleaned_data['user'] #获取用户 auth.login(request, user) #登录 # Redirect to a success page. print(request.GET.get("from",reverse('home'))) return redirect(request.GET.get("from",reverse('home'))) #跳转回原页面,最先获取from数据,没有则用reverse('home')代替 else: login_form = LoginForm() context = {} context['login_form'] = login_form return render(request, 'account/login.html', context)
def my_view(request): if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): cd = form.cleaned_data username = cd['username'] password = cd['password'] #username = request.POST['username'] #password = request.POST['password'] user = authenticate(username=username,password=password) if user is not None: if user.is_active: login(request,user) # Redirect to a success page. #return HttpResponse("Login Successfully!") #email = user.email #html = "<html><body>Welcome %s , your email in systme is %s.</body></html>" % (username, email) #return HttpResponse(html) return redirect('mhome') else: # Return a 'disabled account' error message return HttpResponse("Sorry, the user is a disabled account!") else: # Return a 'invalid login' error message return HttpResponse("Sorry, username or password is not correct!Please try it again!") else: form = LoginForm( # initial = {'subject':'I love your site!'} #initial = {'subject':'我喜欢你的网站!'} ) return render(request,'login_form.html',{'form':form})
def login_page(self, request): if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): result = self.login(request, request.POST['login'], request.POST['password']) if result: return redirect(settings.rootURL + "page/home/") form = LoginForm({'login': request.POST.get('login', '')}) return render(request, 'login/form.html', locals())
def login(request): if len(request.POST) > 0: form = LoginForm(request.POST) if form.is_valid(): user_email = form.cleaned_data['email'] logged_user = Person.objects.get(email=user_email) request.session['logged_user_id'] = logged_user.id return redirect('/welcome') else: return render(request, 'login.html', {'form': form}) else: form = LoginForm() return render(request, 'login.html', {'form': form})
def login(request): redirect_to = request.GET.get('next') if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): auth.login(request, form.cleaned_data['user']) if redirect_to: return redirect(redirect_to) return redirect('/') else: form = LoginForm() return render(request, 'include/login.html', {'form': form})
def UserLogout(request):#注销 auth.logout(request) if request.method == 'POST': # POST方法请求页面 login_form = LoginForm(request.POST) if login_form.is_valid(): user = login_form.cleaned_data['user'] auth.login(request, user) return redirect(request.GET.get('', reverse('index'))) else: # get方法请求页面 login_form = LoginForm() context = {} context['login_form'] = login_form return render(request, 'login.html', context)
def blog_detail(request, blog_pk): blog = get_object_or_404(Blog, pk=blog_pk) read_cookie_key = read_statistics_once_read(request, blog) blog_content_type = ContentType.objects.get_for_model(blog) # comments = Comment.objects.filter(content_type=blog_content_type, object_id=blog.pk, parent=None) context = {} context['previous_blog'] = Blog.objects.filter( created_time__gt=blog.created_time).last() context['next_blog'] = Blog.objects.filter( created_time__lt=blog.created_time).first() context['blog'] = blog context['login_form'] = LoginForm() # context['comments'] = comments.order_by('-comment_time') # 下面这段不删除,用来去和99行的initial=data去匹配的。 data = {} data['content_type'] = blog_content_type.model data['object_id'] = blog_pk data['reply_comment_id'] = 0 # context['comment_count'] = Comment.objects.filter(content_type=blog_content_type, object_id=blog.pk).count() # context['comment_form'] = CommentForm(initial=data) # 或者写成 context['comment_form'] = CommentForm(initial={'content_type':blog_content_type.model, 'object_id':blog_pk}) # context['user'] = request.user response = render(request, 'blog/blog_detail.html', context) # 响应 response.set_cookie(read_cookie_key, 'true') # 阅读cookie标记 return response
def blog_detail(request, blog_pk): # 博客文章 blog = get_object_or_404(Blog, pk=blog_pk) read_cookies_key = read_account_once_read(request, blog) blog_content_type = ContentType.objects.get_for_model(blog) #得到该博客的一级评论数据,不包括回复 comments = Comment.objects.filter(content_type=blog_content_type, object_id=blog.pk, parent=None) context = {} blog = get_object_or_404(Blog, pk=blog_pk) context['previous_blog'] = Blog.objects.filter( create_time__gt=blog.create_time).last() context['next_blog'] = Blog.objects.filter( create_time__lt=blog.create_time).first() context['blog'] = blog context['login_form'] = LoginForm() context['comments'] = comments.order_by('-comment_time') # #得到评论数,传给前端页面 # context['comment_count'] = Comment.objects.filter(content_type=blog_content_type,object_id=blog.pk).count() #初始化form中2个字段的值 context['comment_form'] = CommentForm( initial={ 'content_type': blog_content_type.model, 'object_id': blog_pk, 'reply_comment_id': 0 }) response = render(request, 'blog/blog_detail.html', context) response.set_cookie( read_cookies_key, 'true' ) # 阅读cookies标记 设置有效期 1: max_age=60 以秒为单位 2: expires=datetime对象 具体时间,到了时间就失效 return response
def blog_login(request): # 登陆处理 if request.method == "POST": login_form = LoginForm( request.POST) # 点击提交用户名密码后,处理函数获取到login_form实例对象 if login_form.is_valid(): # 判断数据是否合法,如果合法 user = login_form.cleaned_data[ "user"] # 获取实例对象中的cleaned_date的方法返回的验证后的user login(request, user) # 执行登陆操作 return redirect(request.GET.get( "from", reverse("blog:home"))) # 否则返回登陆前的页面或者首页 else: # 如果不是通过 post 访问的页面,那么就创建一个空的对象,返回到前端页面 login_form = LoginForm() context = {} context["login_form"] = login_form return render(request, "login.html", context)
def login_modal(request): ''' Generates a login form and returns it. ''' return { 'login_form': LoginForm(), }
def login(request): errors = [] if request.method == "POST": form = LoginForm(request.POST) if form.is_valid(): data = form.cleaned_data username = data["username"] password = data["password"] if login_validate(username, password): return render_to_response("welcome.html", {"user": username}) else: errors.append("Username or password is incorrect.") else: errors.append("Please input both username nad password.") else: form = LoginForm() return render_to_response("login_form.html", { "form": form , "errors": errors }, \ context_instance=RequestContext(request))
def login_for_model(request): login_form = LoginForm(request.POST) data = {} if login_form.is_valid(): # 判断数据是否合法,如果合法 user = login_form.cleaned_data[ "user"] # 获取实例对象中的cleaned_date的方法返回的验证后的user login(request, user) # 执行登陆操作 data['status'] = 'SUCCESS' else: data['status'] = 'ERROR' return JsonResponse(data)
def login_custom(request): ''' Returns a page for logging in. ''' next = request.POST.get('next', request.GET.get('next', '')) if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): user = authenticate(username=form.cleaned_data.get('username'), password=form.cleaned_data.get('password')) if user is not None: login(request, user) if next: return redirect(next) return redirect('dashboard') else: form = LoginForm() context = {'form': form} return render(request, 'registration/login_static.html', context)
def blog_detail(request, blog_pk): blog = get_object_or_404(Blog, pk=blog_pk) read_cookie_key = read_statistics_once_read(request, blog) context = {} context['previous_blog'] = Blog.objects.filter(created_time__gt=blog.created_time).last() context['next_blog'] = Blog.objects.filter(created_time__lt=blog.created_time).first() context['blog'] = blog context['login_form'] = LoginForm() response = render(request, 'blog/blog_detail.html', context) # 响应 response.set_cookie(read_cookie_key, 'true') # 阅读cookie标记 return response
def Login(request):#登录 context = {} if request.method == 'POST': # POST方法请求页面 login_form = LoginForm(request.POST) if login_form.is_valid(): #user = login_form.cleaned_data['user'] #auth.login(request, user) username = login_form.cleaned_data['username'] password = login_form.cleaned_data['password'] user = auth.authenticate(username=username, password=password) auth.login(request, user) return redirect(request.GET.get('', reverse('index'))) #return render(request, 'personalcenter.html', context) else: # get方法请求页面 login_form = LoginForm() context = {} context['login_form'] = login_form return render(request, 'login.html', context)
def login_page(request): form = LoginForm(request.POST or None) context = {'form': form} print("User logged in") print(request.user.is_authenticated) if form.is_valid(): print(form.cleaned_data) username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = authenticate(request, username=username, password=password) if user is not None: # context['form'] = LoginForm() print("User is now validated and logedin") print(request.user.is_authenticated) login(request, user) return redirect('/') else: context['form'] = LoginForm() print("Error: user is not validated and logedin") return render(request, 'auth/login_page.html', context)
def login_view(request): form = LoginForm(None or request.POST) if form.is_valid(): data = form.cleaned_data user = authenticate(username=data['Username'], password=data['Password']) if user is not None: login(request, user) return HttpResponseRedirect(reverse('homepage')) return render(request, 'login.html', {'form': form})
def blog_detail(request, blog_pk): blog = get_object_or_404(Blog, pk=blog_pk) read_cookie_key = read_statistics_once_read(request, blog) context = {} context['previous_blog'] = Blog.objects.filter(created_time__gt=blog.created_time).last() context['next_blog'] = Blog.objects.filter(created_time__lt=blog.created_time).first() context['blog'] = blog context['login_form'] = LoginForm() response = render(request,'blog/blog_detail.html',context) # response.set_cookie('blog_%s_readed' % blog_pk,'true') ###第一个参数是记录主键值;第二参数随便填,标明已读即可;第三个参数,max_age=60是60s记录一次 response.set_cookie(read_cookie_key,'true') #阅读cookie标记 return response
def blog_detail(request, blog_pk): # 博客详情 blog = get_object_or_404(Blog, pk=blog_pk) # 取出对应的blog_pk中的Blog查询集 read_cookie_key = read_statistics_once_read(request, blog) context = {} context["previous_blog"] = Blog.objects.filter( created_time__gt=blog.created_time).last() context["next_blog"] = Blog.objects.filter( created_time__lt=blog.created_time).first() context["blog"] = blog context["login_form"] = LoginForm() response = render(request, "blog/blog_detail.html", context) response.set_cookie(read_cookie_key, "true") return response
def login(): if current_user.is_authenticated: return redirect(url_for("home")) form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user and bcrybt.check_password_hash(user.password, form.password.data): login_user(user, remember=form.remember.data) n_page = request.args.get("next") return redirect(n_page) if n_page else redirect(url_for("home")) else: flash('Login Unsuccessful. Please check email and password', 'danger') return render_template('login.html', title='Login', form=form)
def login_modal(request): ''' Returns a form for logging in, in JSON format. Displayed in Modal. ''' data = dict() data['user_inactive'] = False if request.method == 'POST': form = LoginForm(request.POST) if form.is_valid(): data['form_is_valid'] = True user = authenticate(username=form.cleaned_data.get('username'), password=form.cleaned_data.get('password')) login(request, user) else: data['form_is_valid'] = False else: form = LoginForm() context = {'form': form} data['html_form'] = render_to_string('registration/login.html', context, request=request) return JsonResponse(data)
def blog_detail(request, blog_pk): blog = get_object_or_404(Blog, pk=blog_pk) read_cookie_key = read_statistics_once_read(request, blog) ''' if not request.COOKIES.get('blog_%s_readed' % blog_pk): ct=ContentType.objects.get_for_model(blog) if ReadNum.objects.filter(content_type=ct,object_id=blog.pk).count(): readnum=ReadNum.objects.get(content_type=ct,object_id=blog.pk) #存在记录 else: #不存在记录 readnum=ReadNum(content_type=ct,object_id=blog.pk) #技术+1 readnum.read_num+=1 readnum.save() ''' ##如果已经打开过,即已经有cookie则不会加1 #必须要存储下来,否则会出错,但是当这个字段更改的时候,最后更改日期也变换了 #blog_content_type=ContentType.objects.get_for_model(blog) #comments=Comment.objects.filter(content_type=blog_content_type,object_id=blog.pk,parent=None).order_by('-comment_time') context = {} context['blog'] = blog context['login_form'] = LoginForm() context['previous_blog'] = Blog.objects.filter( created_time__gt=blog.created_time).last() # 使用__双下划线魔法方法来进行对属性筛选范围的锁定 context['next_blog'] = Blog.objects.filter( created_time__lt=blog.created_time).first() #context['user']=request.user 使用render的话,就直接有user #context['comments']=comments.order_by('-comment_time')[:7]#这里倒叙,但是在插入的是也是从后边插,所以显示就正了。 ##需要先order_by再使用切片 # data={} # data['content_type']=blog_content_type.model##将类型变为字符串 # data['object_id']=blog_pk #context['comment_form']=CommentForm(initial={'content_type':blog_content_type.model,'object_id':blog_pk,'reply_comment_id':0})##要输入一个实例化的form对象,必须用initial不能直接 #context['comment_count']=Comment.objects.filter(content_type=blog_content_type,object_id=blog.pk).count() response = render(request, 'blog/blog_detail.html', context) ##获得这个响应与请求是对应的 response.set_cookie(read_cookie_key, 'true') #阅读cookie标记 #max_age=60,expires=datetime#让浏览器保存信息,cookie有个有效期,过了有效期,浏览器就不提交了 # 主要有4个参数,字典键、字典键值、最大有效期单位为秒、指定一个时间(datetime类型),这两个参数是冲突的,如果后两个都不写,表示浏览器结束cookie才失效 ##返回評論的數量 return response
def blog_detail(request, blog_pk): context = {} blog = get_object_or_404(Blog, pk=blog_pk) read_cookie_key = read_statistics_once_read(request, blog) # 返回了文章的key context['previous_blog'] = Blog.objects.filter( create_time__gt=blog.create_time).last() # 过滤大小 context['next_blog'] = Blog.objects.filter( create_time__lt=blog.create_time).first() # 前后页 context['blog'] = blog context['login_form'] = LoginForm() # blog_content_type.model <class 'str'> 类型 跟字符串'blog'一样 # initial初始化了字段content_type和object_id的值 response = render(request, 'blog/blog_detail.html', context) response.set_cookie( read_cookie_key, 'true') # response的set_cookies方法将cookies保存到浏览器,等关闭浏览器时cookies更新 return response
def login(): form = LoginForm() if current_user.is_authenticated: return redirect(url_for('index')) if form.validate_on_submit(): user = User.query.filter_by(username=form.username.data).first() if user is None or not user.check_password(form.password.data): flash('Неправильное имя пользователя и/или пароль', 'danger') return redirect(url_for('login')) login_user(user, remember=form.remember_me.data) return redirect(url_for('index')) return render_template('login.html', title='Вход', form=form)
def login(): form = LoginForm() if current_user.is_authenticated: return redirect(url_for('home')) else: if request.method == 'POST': user = User.query.filter_by(email=form.email.data).first() if user and bcrypt.check_password_hash(user.password, form.password.data): login_user(user, remember=form.remember.data) next_page = request.args.get('next') return redirect(next_page) if next_page else redirect( url_for('home')) else: flash('Login Unsuccessful. Please check email and password', 'danger') else: return render_template('login.html', title='Login', form=form)
def post(self, request, *args, **kwargs): form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] user = authenticate(username=username, password=password) if user: if user.is_active: login(request, user) messages.success(request, 'You are login') return HttpResponseRedirect('/cinema/') else: messages.warning(request, 'Your login or password not correct') return HttpResponseRedirect('/cinema/') context = {'form': form} return render(request, 'login.html', context)
def login(): if current_user.is_authenticated: return redirect(url_for('home')) form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user and bcrypt.check_password_hash(user.password, form.password.data): login_user(user, remember=form.remember.data) next_page = request.args.get('next') return redirect(next_page) if next_page else redirect( url_for('home')) flash('Login não foi possível. Verifique as credenciais.', 'danger') popular = Post.query.order_by(desc(Post.total_score)).limit(3) return render_template('login.html', title='Login', form=form, popular_posts=popular)
def Update(request, user_pk): referer = request.META.get('HTTP_REFERER', reverse('index')) context = {} user = User.objects.get(pk=user_pk) username = user.username if request.method == 'POST': email = request.POST['email'] password = request.POST['password'] password_again = request.POST['password_again'] if len(password) < 6: message = "密码长度最少为6位" context['message'] = message context['redirect_to'] = referer return render(request, 'error.html', context) if password != password_again: message = "两次输入的密码不一致" context['message'] = message context['redirect_to'] = referer return render(request, 'error.html', context) if User.objects.filter(email=email).count() > 1: message = "邮箱已存在" context['message'] = message context['redirect_to'] = referer return render(request, 'error.html', context) user.username = username user.email = email user.set_password(password) user.save() login_form = LoginForm() context['login_form'] = login_form return render(request, 'login.html', context)
def blog_detail(request, blog_pk): """ 博客详情 """ context = {} blog = get_object_or_404(Blog, pk=blog_pk) read_cookie_key = read_statistics_once_read(request, blog) # blog_content_type = ContentType.objects.get_for_model(blog) # comments = Comment.objects.filter(content_type=blog_content_type, object_id=blog.pk, parent=None) context["previous_blog"] = Blog.objects.filter( create_time__gt=blog.create_time).last() context["next_blog"] = Blog.objects.filter( create_time__lt=blog.create_time).first() # context["blog_dates"] = Blog.objects.dates("create_time", "month", order="DESC") context["blog"] = blog context["login_form"] = LoginForm() response = render(request, "blog/blog_detail.html", context) response.set_cookie(read_cookie_key, "true", max_age=60) return response
def blog_detail(request, blog_pk): blog = get_object_or_404(Blog, pk=blog_pk) read_cookie_key = read_statistics_once_read(request, blog) context = {} # 获取当前博客上一条博客和下一条博客 context['previous_blog'] = Blog.objects.filter( created_time__gt=blog.created_time).last() context['next_blog'] = Blog.objects.filter( created_time__lt=blog.created_time).first() context['blog'] = blog context['login_form'] = LoginForm() # 下面部分放到tags去实现 #comments = Comment.objects.filter(content_type=blog_content_type, object_id=blog.pk, parent=None) #blog_content_type = ContentType.objects.get_for_model(blog) # 获得模块Blog #context['comments'] = comments.order_by('-comment_time') #context['comment_form'] = CommentForm(initial={'content_type':blog_content_type.model,'object_id':blog_pk, 'reply_comment_id':'0'}) response = render(request, 'blog/blog_detail.html', context) # 响应 # 给客户端添加cookie,可做一些判断操作 response.set_cookie( read_cookie_key, 'ture') # , max_age=60, expires=datetime) 这两个参数设置有效期,任一设置一个即可 return response