Django个人博客搭建教程---用户登录控制页面展示应用

写在最前面:

 

-----2019.7.21更新

index.html部分:

views.py部分:

def login_view(request):
    if request.session.get('is_login', None):
        return redirect('/JiaBlog/mylist/')
    elif request.user.is_authenticated:
        return redirect('/JiaBlog/index/')
    global next

    if request.method == "POST":
        login_form = UserForm(request.POST)
        request.session['login_from'] = request.META['HTTP_REFERER']
        print(request.META['HTTP_REFERER'])
        message = "请检查填写的内容!"
        if login_form.is_valid():
            username = login_form.cleaned_data['username']
            password = login_form.cleaned_data['password']
            print(username, password)
            try:
                user = models.BlogUser.objects.get(name=username)
                if user.status == 'active' or user.status == '有效':
                    if user.password == password:
                        request.session['is_login'] = True
                        request.session['user_id'] = user.id
                        request.session['user_name'] = user.name
                        if next == '':
                            return HttpResponseRedirect(request.session['login_from'])
                        else:
                            return redirect(next)
                    else:
                        message = "密码不正确!"
                else:
                    message = "用户状态信息异常,请联系管理员(18351922995)! "
            except:
                message = "用户不存在!"
        return render(request, 'jialogin.html', locals())

    else:
        login_form = UserForm()
        next = request.GET.get('next', '')
        return render(request, 'jialogin.html', locals())
def logout_view(request):
    logout(request)
    if not request.session.get('is_login', None):
        # 如果本来就未登录,也就没有登出一说
        return redirect("/JiaBlog/index/")
    # request.session.flush()
    # 或者使用下面的方法
    # logout(request)
    del request.session['is_login']
    del request.session['user_id']
    del request.session['user_name']
    return redirect("/JiaBlog/login/")

 

-----以下是之前的demo

 

这里只做一个demo展示,因为我觉得博客没有必要做用户权限控制。

 

首先你的index.html需要做一个展示控制

{% extends 'base.html' %}
{% block title %}主页{% endblock %}
{% block content %}
    {% if request.session.is_login %}
    

你好,{{ request.session.user_name }}!欢迎回来!

{% else %}

你尚未登录,只能访问公开内容!

{% endif %} {% endblock %}

这里最重要的事这个is_login,这个状态怎么获取呢?

看views.py中的login函数

def login(request):
    if request.session.get('is_login', None):
        return redirect('/Blog/new')
    if request.method == "POST":
        login_form = UserForm(request.POST)
        message = "请检查填写的内容!"
        if login_form.is_valid():
            username = login_form.cleaned_data['username']
            password = login_form.cleaned_data['password']
            print(username, password)
            try:
                user = models.User.objects.get(name=username)
                if user.password == password:
                    request.session['is_login'] = True
                    request.session['user_id'] = user.id
                    request.session['user_name'] = user.name
                    print(request.session['is_login'],request.session['user_name'])
                    print('ok')
                    return redirect('/Blog/new/')
                else:
                    message = "密码不正确!"
            except:
                message = "用户不存在!"
        return render(request, 'login.html', locals())

    login_form = UserForm()
    return render(request, 'login.html', locals())

当登录成功,会记录用户状态,用户名称

这样你的index展示的时候会展示你对于登录状态的页面

 

未登录的时候进入index

 

我们还需要一个logout函数 ,去清除你的登录session

def logout(request):
    if not request.session.get('is_login', None):
        # 如果本来就未登录,也就没有登出一说
        return redirect("/Blog/newindex/")
    #request.session.flush()
    # 或者使用下面的方法
    del request.session['is_login']
    del request.session['user_id']
    del request.session['user_name']
    return redirect("/Blog/new/")

这里的跳转地址你自己定义

 

 

你可能感兴趣的:(Django,python,python,django,登录登出)