通过表格发送时没有Django发布数据?

发布于 2025-02-12 08:11:31 字数 1763 浏览 0 评论 0原文

我在开发服务器(无CSRF保护)上,通过Web表单将登录数据发送到Django(Django:127.0.0.1:8000)。 html:

<!DOCTYPE html>
<html>
<head>
<title>Log In</title>
<meta charset="utf-8">
</head>
<body>
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<header>

</header>
<script> 
$("document").ready(function () {
$("header").load("static/header.html");
})
</script>
<div id="loginbox">
<h3>Log In</h3>
<form action="http://127.0.0.1:8000" method="post">
<input type="text" id="uname"><br>
<input type="password" id="pass"><br>
<button id="login" type="submit">Log In</button></br>
</form>
<a href="signup.html">Sign Up</a><br>
<a href="forgotCredentials.html">Forgot Username/Password?</a>
</div>
</body>
</html>

django:

from django.shortcuts import render

# Create your views here.


from django.contrib.auth import authenticate, login

from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse

@csrf_exempt
def index(request):
    uname = request.POST.get("uname")
    passwd = request.POST.get("pass")
    print(uname + " pass " + passwd)
    user = authenticate(username=uname, password=passwd)
    if user is not None:
        login(request, user)
        return render(request, "taxnow/index.html")
    return render(request, "taxnow/Login.html")

我很确定未传输帖子数据(typeError:未支撑的操作数类型 +:'nontype'and'str'和'str'和'str'),但我不知道为什么会弄清楚为什么。 (如前所述,HTML位于Localhost上,Django在127.0.0.1:8000上。)是否有任何理由?

I am on a development server (no CSRF protection), sending over login data to Django via a web form (Django: 127.0.0.1:8000).
HTML:

<!DOCTYPE html>
<html>
<head>
<title>Log In</title>
<meta charset="utf-8">
</head>
<body>
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<header>

</header>
<script> 
$("document").ready(function () {
$("header").load("static/header.html");
})
</script>
<div id="loginbox">
<h3>Log In</h3>
<form action="http://127.0.0.1:8000" method="post">
<input type="text" id="uname"><br>
<input type="password" id="pass"><br>
<button id="login" type="submit">Log In</button></br>
</form>
<a href="signup.html">Sign Up</a><br>
<a href="forgotCredentials.html">Forgot Username/Password?</a>
</div>
</body>
</html>

Django:

from django.shortcuts import render

# Create your views here.


from django.contrib.auth import authenticate, login

from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse

@csrf_exempt
def index(request):
    uname = request.POST.get("uname")
    passwd = request.POST.get("pass")
    print(uname + " pass " + passwd)
    user = authenticate(username=uname, password=passwd)
    if user is not None:
        login(request, user)
        return render(request, "taxnow/index.html")
    return render(request, "taxnow/Login.html")

I'm pretty sure that the POST data is not being transmitted (TypeError: unsupported operand type(s) for +: 'NoneType' and 'str') in the print statement, but I can't figure out why.
(The HTML is on localhost and the django is on 127.0.0.1:8000 as mentioned previously.) Is there any reason for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世蒼涼 2025-02-19 08:11:31

request.post中,您没有来自ID属性的密钥,而是name。将其添加到输入中,您可以。

<input type="text" id="uname" name="uname"><br>
<input type="password" id="pass" name="pass"><br>

In request.POST you don't have keys from id attribute, but from name. Add it to inputs and you're fine.

<input type="text" id="uname" name="uname"><br>
<input type="password" id="pass" name="pass"><br>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文