码迷,mamicode.com
首页 > 其他好文 > 详细

django 127.0.0.1 将您重定向的次数过多

时间:2018-04-12 14:17:43      阅读:1129      评论:0      收藏:0      [点我收藏+]

标签:one   username   too   error   turn   profile   int   word   from   

"GET /?next=/%3Fnext%3D/%253Fnext%253D/ HTTP/1.1" 302 0

solution reference

from django.contrib.auth.decorators import login_required

@login_required
def my_view(request):
    ...

@login_required官网解释:If the user isn’t logged in, redirect to settings.LOGIN_URL, passing the current absolute path in the query string
即将settings.LOGIN_URL设置为你的登陆页面地址

对于我的情况如下:
settings.py
LOGIN_URL = "/login/"
urls.py

url(r‘^$‘, views.index, name="index"),
url(r‘^login/$‘, views.user_login, name="login"),

views.py

@login_required
def index(request):
    return render(request, "index1.html")


def user_login(request):
    if request.method == "POST":
        # try:
        #     m = models.UserProfile.objects.get(email=request.POST[‘username‘])
        # except Exception:
        #     print("username doesn‘t exist")
        #     return render(request, "login.html")
        username = request.POST["username"]
        password = request.POST["password"]
        user = authenticate(request, email=username, password=password)
        error_msg = "账号或密码错误,请重新输入"
        none_msg = "please input username and password"
        if user is not None:
            if user.is_active:
                login(request, user)
                # request.session[‘member_id‘] = m.id
                return redirect("/")        # redirect to 127.0.0.1:8000/ 
            else:
                print(none_msg)
                return render(request, "login.html", {"error_msg": none_msg})
        else:
            print(error_msg)
            return render(request, "login.html", {"error_msg": error_msg})
    else:
        return render(request, "login.html")

django 127.0.0.1 将您重定向的次数过多

标签:one   username   too   error   turn   profile   int   word   from   

原文地址:https://www.cnblogs.com/vickey-wu/p/8807376.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!