【问题标题】:Facing issues with HttpResponseRedirect in Django在 Django 中面临 HttpResponseRedirect 的问题
【发布时间】:2018-02-20 05:00:18
【问题描述】:

我正在为用户创建一个基本的登录功能。如果登录成功,我会将用户重定向到我在 HttpResponseRedirect 中硬编码的索引页面。但是一旦我点击登录按钮,它会说,

/Login/ 处的类型错误 login() 接受 1 个位置参数,但给出了 2 个

urls.py 中的索引页面 id 定义如下,

url(r'^$',views.index,name='index'),

下面是我的索引视图和登录视图的代码:

def index(request):
    return render(request,'authentication_app/index.html')

def login(request):
    if request.method=='POST':
        username=request.POST.get('name')
        password=request.POST.get('password')

        user = authenticate(username=username,password=password)
        if user:
            if user.is_active:
                login(request,user)
                return HttpResponseRedirect('/')
            else:
                return HttpResponse('Your Account has become Invalid')
        else:
            print('Someone tried to login with wrong credentials:{0},{1}'.format(username,password))
            return HttpResponse('Username and Password mismatch.Please enter valid credentials')

    else:
        return render(request,'authentication_app/login.html',{})

【问题讨论】:

    标签: django


    【解决方案1】:

    您需要将 函数名称login() 更改为 user_login()

    def user_login(request):
    

    它与此处使用的login(request,user)冲突

    【讨论】:

    • @Deepak,如果有帮助,您可以选择答案。谢谢。
    【解决方案2】:

    您可以将导入的登录名更改为其他名称 喜欢

    from django.contrib.auth import login as auth_login
    

    用于执行登录的用户 auth_login

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-26
      相关资源
      最近更新 更多