【问题标题】:Html form and Rest Api DjangoHtml 表单和 Rest Api Django
【发布时间】:2017-01-24 11:49:25
【问题描述】:

我正在将用户名和密码从我的 HTML 表单发送到 django 后端。

<html> 

<style type="text/css">

    #center_align{
     width: 200px;
    margin: 10px auto;
}

</style>        

<head>
  <title>Login Form</title>
</head>
<body>
      <div id = "center_align">
      <h1>Login</h1>
                <form method = "GET" action="http://127.0.0.1:8000/login/" >
                  Username :<input type ="text" name = "username"><br>
                      Password :<input type ="password" name = "password"><br>
          <input type="submit" value="Submit">    
          </form>
                 </div>
</body>
</html>

在我的 django 中,我在 Views 中写了一个类

class Login(APIView):

    global User_Grps

    def get(request,self):

        state = ""
        username = "Gauss"
        password = settings.AUTH_LDAP_BIND_PASSWORD

        oLdap = LDAPBackend()

        try:
            User = oLdap.authenticate(username=username,password=password) 
            print User.ldap_user.group_dns

            if User is not None:
                User_Grps = User.ldap_user.group_dns

            else:
                User_Grps = "Invalid Group"

        except Exception as e:
                User_Grps = "Error"

        return HttpResponse(User_Grps)  

如何从请求对象中检索我的用户名和密码?就像我需要从方法的请求参数中获取数据一样。

【问题讨论】:

  • 我不明白你为什么在这里使用 APIView,甚至是 django-rest-framework。看起来您正在执行标准表单提交,因此您应该使用标准 Django 视图。

标签: html django django-rest-framework


【解决方案1】:

您应该写一个 custom Django authentication backend 而不是这样做,这将允许您使用来自 DRF 和/或其他 Django 3rd 方的默认登录视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 2015-08-07
    • 2015-12-06
    • 1970-01-01
    • 2019-01-08
    相关资源
    最近更新 更多