【用户登录的优化】
在前面,我已经实现了登录页面的建立和后台处理逻辑的实现!,在实际使用中,用户往往是需要评论的时候,才需要登录,这就涉及到一用户体验问题:用户在某篇文章看完文章后,想发表评论,因为我们要求登录用户才可以评论,用户再点击了前往登录按钮后,当他登录完成,如果不能自动跳回原来的文章,用户体验就非常差,因此,我们需要做好自动调回功能!我的实现思路如下:
1.用户从哪个页面前往的登录页,我就将当前页的URL传递到登录页,方法:
?next={{ request.get_full_path }}
2.在login处理视图里,使用GETf获取
#登陆 def loginView(request): if request.method == 'GET': post_urls = request.GET.get('next') # 获取url上携带的来源地址,用于登录后跳转回去! return render(request, 'user/login.html',{"post_urls":post_urls}) #判读是否是post提交数据 if request.method == 'POST': #就将数据给表单验证 user= LoginForm(request.POST)
3.将获取的内容传递到login.html,当用户登录的时候,跟着POST再次传回来!
用一个input标签,传递,并隐藏。
<input type="hidden" name="post_urls" class="form-control" placeholder="密码" value="{{ post_urls|default_if_none:'/'}}">
这里要使用一个过滤器,如果用户是直接来到登录页,前面就GET不到东西,就会出错,因此,如果GET不到,就默认使用'/',就是回到首页去!
4.后端再次接受,级得去登录表单定义一个字段接受它
class LoginForm(forms.Form):
username = forms.CharField(min_length=3,max_length=20,error_messages={"error": "用户名长度3-20个字符",})
password = forms.CharField(min_length=8,max_length=20,error_messages={"error": "密码长度应8-20个字符",})
post_urls = forms.CharField()
5.前端在POST里获取出来,如果用户登录成功,就跳转获取,失败就回到首页
..... post_urls = user.cleaned_data['post_urls'] #把用户放进表里查询,看看有没有这个用户 user_name =User.objects.filter(username=username) #如果有这个用户 if user_name: # 把用户密码交给User的验证器匹配 user = authenticate(username=username,password=password) # 如果验证成功且不是空 if user is not None: # 如果验证成功,并且用户状态为激活状态 if user.is_active: #通过,就用login方法登陆 login(request,user) #登陆后就跳转或来之前的页面,如果获取不到,就重定向到首页 return redirect(post_urls,reverse('index'))
主要思路就是这样了,实验了几次,没发现BUG,后期有发现再优化吧,代码岁啰嗦,但目标实现了,也是一个大的进步!
完整代码如下:
1.在需要实现这个跳转功能的页面都先加上?next={{ request.get_full_path }}
【post_read.html】
评论判断哪里,如果没登录,就显示登录链接
<div class="form-group"> <p class="help-block">登陆后方可评论,请<a href="{% url 'login' %}?next={{ request.get_full_path }}">前往登陆</a></p> </div>
2.去登录的处理视图里接收并传递地址
def loginView(request): if request.method == 'GET': post_urls = request.GET.get('next') # 获取url上携带的来源地址,用于登录后跳转回去! return render(request, 'user/login.html',{"post_urls":post_urls}) #判读是否是post提交数据
3.login.html完整代码,接收并隐藏传来的url
<form class="form-signin" action="{% url 'login' %}" method="post">
{% csrf_token %}
<h2 class="form-signin-heading">用户登陆</h2>
<input type="text" name="username" class="form-control" placeholder="用户名" required autofocus>
<input type="password" name="password" class="form-control" placeholder="密码" required>
<input type="hidden" name="post_urls" class="form-control" placeholder="密码" value="{{ post_urls|default_if_none:'/'}}">
<div class="checkbox">
<label>
<input type="checkbox" value="Ture" name="required"> 记住我
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">登入</button>
{% if error %}
<div class="alert alert-info" role="alert">{{error }}</div>
{% endif %}
</form>
4.【LoginForm】
class LoginForm(forms.Form): username = forms.CharField(min_length=3,max_length=20,error_messages={"error": "用户名长度3-20个字符",}) password = forms.CharField(min_length=8,max_length=20,error_messages={"error": "密码长度应8-20个字符",}) post_urls = forms.CharField()
5.【login视图里写POST部分】
#登陆 def loginView(request): if request.method == 'GET': ........ #判读是否是post提交数据 if request.method == 'POST': #就将数据给表单验证 user= LoginForm(request.POST) #如果验证成功 if user.is_valid(): #从表单里获取来用户名和密码 username= user.cleaned_data['username'] password = user.cleaned_data['password'] post_urls = user.cleaned_data['post_urls'] #把用户放进表里查询,看看有没有这个用户 user_name =User.objects.filter(username=username) #如果有这个用户 if user_name: # 把用户密码交给User的验证器匹配 user = authenticate(username=username,password=password) # 如果验证成功且不是空 if user is not None: # 如果验证成功,并且用户状态为激活状态 if user.is_active: #通过,就用login方法登陆 login(request,user) #登陆后就跳转或来之前的页面,如果获取不到,就重定向到首页 return redirect(post_urls,reverse('index')) else: return render(request, 'user/login.html',{"error":"用户名没有激活,请前往激活"}) else: return render(request, 'user/login.html', {"error": "密码不正确"}) else: return render(request, 'user/login.html', {"error": "该用户不是注册用户"}) else: return render(request, 'user/login.html', {"error": "用户名或密码不正确"}) else: return render(request, 'user/login.html')
整体完成!
0
登陆后方可评论