django @login_required logon restrictions (2) - Back to the successful landing

This secondary function is to achieve access unregistered view function, you need to jump to the login page, successfully landed the jump back.

Before the Internet to find a lot of information, we do not find a solution.

Go to the login page to get well is successful landing jumps back out of the question, because the value of the url before next post requests after login can not get in, it is only necessary when initiating get request, the value of the next passed to the input of label templates in, type is hidden, then after post request to get back on it!

 

 Just need to get the next value in the url, after the successful landing redirected to this value can be achieved.

Specific code as follows:

urls.py

urlpatterns = [
    url(r'^01-temp/$',views.temp_views),
    url(r'^form/$',views.fun,name='form'),
    url(r'^loginv/',views.loginl,name="login"),
    url(r'^createuser/$',views.create_user),
    url(r'^auth/$',views.auth),
    url(r'^logoutv/$',views.logoutl),
]
views.py

def loginl(request):
    if request.method=="GET":
        nexturl = request.GET.get("next", "")
        print(request.user.is_authenticated)
        if request.user.is_authenticated:
            if nexturl:
                # login(request,request.user)
                return the redirect (the nexturl)
             the else :
                 return the HttpResponse ( " login success " )
         the else : # determines whether the user login is successful
             return the render (Request, ' the login.html ' , { 'the nexturl' : the nexturl} )

    elif request.method == "POST":
        nexturl=request.POST.get("nexturl","")
        uname=request.POST.get("uname","")
        upwd = request.POST.get("upwd", "")
        print(uname, upwd,nexturl,request.get_full_path)
        print(request.POST.get("next"))
        user=authenticate(request,username=uname,password=upwd)
        if user is not  None:
            if user.is_active:
                # res = HttpResponse("sucess")
                # res.set_cookie(key='uname',value=uname,max_age=20000)
                # res.set_cookie(key='upwd', value=upwd,max_age=20000)
                login(request,user)
                IF nexturl: # need to return after successful landing
                    print(uname, upwd, nexturl, request.get_full_path(), nexturl)
                    redirect return (nexturl)
                 the else : # no nexturl, you can return to the home page

                    return HttpResponse("sucess")
        else:
            return HttpResponse("账户不存在")

def logoutl(request):
    logout(request)
    return HttpResponse ( " a successful exit " )

@login_required(login_url="/loginv/")
def temp_views(request):

    return HttpResponse("ok")
login.html

<form action="{% url 'login' %}" method="POST">
        {% csrf_token %}
        姓名<input type="text" name="uname">
        <br>
        密码<input type="password" name="upwd">
        <br>
        <input type="hidden" name="nexturl" value="{{ nexturl }}">
        <input type="submit" value="提交">
    </form>

 

Sign out

 

 Access 01-temp, need to sign in

 

 Successful landing, access to 01-temp view function

Guess you like

Origin www.cnblogs.com/pfeiliu/p/12013907.html