【问题标题】:Authenticate a user with Django使用 Django 对用户进行身份验证
【发布时间】:2019-01-10 01:42:08
【问题描述】:

我有登录 html 页面和索引 html 页面,但是当我写下用户名和密码时,页面只刷新而不做任何事情。 我使用 Django 2.x 和 Python 3.4

登录html中的代码:

      <form class="form-signin" action='{% url 'myapp:index' %}' method="post">
    {% csrf_token %}

    <img class="mb-4" src='{% static 'app/img/logo.png' %}' alt="" width="300" height="100">

    <h1 class="h3 mb-3 font-weight-normal">mysite</h1>
    <br>
    <div class="group">
        <input type="username" id="inputUser" name="username" class="form-control" placeholder="Username" required autofocus alt="" width="300" height="100">
    </div>

    <div class="group">
        <input type="password" id="inputPassword" name="password" class="form-control" placeholder="Password" required alt="" width="300" height="100">
    </div>

    <div>
        <button class="btn btn-lg btn-primary btn-block" type="submit">Accedi</button>
    </div>

  </form>

这是我的观点:

def index(request):
    username = request.post["username"]
    password = request.post["password"]
    user = authenticate(request, username=username, password=password)
    if user is not None:
        login(request, user)
        return render(request, "myapp/index.html")

我做错了什么? 谢谢

编辑:

我的错误日志

AH00558:httpd.exe:无法可靠地确定服务器的完全限定域名,使用 fe80::1c06:8ac5:1b1e:aa2f。全局设置“ServerName”指令以禁止显示此消息 [Thu Aug 02 12:35:17.911829 2018] [mpm_winnt:notice] [pid 4196:tid 452] AH00455: Apache/2.4.34 (Win64) mod_wsgi/4.6.4 Python/3.6 已配置——恢复正常操作 [2018 年 8 月 2 日星期四 12:35:17.912829] [mpm_winnt:notice] [pid 4196:tid 452] AH00456:Apache Lounge VC15 服务器建成时间:2018 年 7 月 11 日 13:09:01 [Thu Aug 02 12:35:17.912829 2018] [core:notice] [pid 4196:tid 452] AH00094:命令行:'c:\Apache\bin\httpd.exe -d C:/Apache' [Thu Aug 02 12:35:17.913829 2018] [mpm_winnt:notice] [pid 4196:tid 452] AH00418: Parent: Created child process 2112 AH00558:httpd.exe:无法可靠地确定服务器的完全限定域名,使用 fe80::1c06:8ac5:1b1e:aa2f。全局设置“ServerName”指令以禁止显示此消息 AH00558:httpd.exe:无法可靠地确定服务器的完全限定域名,使用 fe80::1c06:8ac5:1b1e:aa2f。全局设置“ServerName”指令以禁止显示此消息 [Thu Aug 02 12:35:18.335853 2018] [mpm_winnt:notice] [pid 2112:tid 472] AH00354: Child: 启动 64 个工作线程。

我的 access.log:

myip - - [02/Aug/2018:12:35:28 +0200] "GET /myapp/ HTTP/1.1" 200 2633 myip - - [02/Aug/2018:12:35:48 +0200] "POST /myapp/ HTTP/1.1" 200 2633

编辑2: 我将 url.py 从路径更改为 url 并解决了问题...现在页面加载正确,感谢大家

【问题讨论】:

  • 您的预期行为是什么?
  • 我希望当我点击“Accedi”时,程序会验证用户是否存在于数据库中,如果存在,则加载索引页面
  • 您的服务器跟踪日志中有任何消息吗?
  • 我在帖子中添加了日志

标签: django python-3.x


【解决方案1】:
user = authenticate(request, username=username, password=password)

不需要将请求作为参数传递

user = authenticate(username=username,password=password)

它可能会解决你的错误。

另一种方式- 你也可以参考这个教程: https://simpleisbetterthancomplex.com/tutorial/2016/06/27/how-to-use-djangos-built-in-login-system.html

【讨论】:

  • 删除“请求”并不能解决问题,我去看看你建议我的链接
  • 还将 request.post 更改为 request.POST(区分大小写)。
  • 没有什么像 更改为
  • 我应用了所有更改,但完全没有效果
  • 我认为有一个服务器错误参考这个:unix.stackexchange.com/questions/190085/…
猜你喜欢
  • 2020-10-30
  • 2012-08-17
  • 2021-06-02
  • 1970-01-01
  • 2021-06-11
  • 2012-08-16
  • 2015-08-30
  • 2020-10-07
  • 1970-01-01
相关资源
最近更新 更多