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:
JavaScript
x
29
29
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<title>Log In</title>
5
<meta charset="utf-8">
6
</head>
7
<body>
8
<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
9
<header>
10
11
</header>
12
<script>
13
$("document").ready(function () {
14
$("header").load("static/header.html");
15
})
16
</script>
17
<div id="loginbox">
18
<h3>Log In</h3>
19
<form action="http://127.0.0.1:8000" method="post">
20
<input type="text" id="uname"><br>
21
<input type="password" id="pass"><br>
22
<button id="login" type="submit">Log In</button></br>
23
</form>
24
<a href="signup.html">Sign Up</a><br>
25
<a href="forgotCredentials.html">Forgot Username/Password?</a>
26
</div>
27
</body>
28
</html>
29
Django:
JavaScript
1
21
21
1
from django.shortcuts import render
2
3
# Create your views here.
4
5
6
from django.contrib.auth import authenticate, login
7
8
from django.views.decorators.csrf import csrf_exempt
9
from django.http import HttpResponse
10
11
@csrf_exempt
12
def index(request):
13
uname = request.POST.get("uname")
14
passwd = request.POST.get("pass")
15
print(uname + " pass " + passwd)
16
user = authenticate(username=uname, password=passwd)
17
if user is not None:
18
login(request, user)
19
return render(request, "taxnow/index.html")
20
return render(request, "taxnow/Login.html")
21
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?
Advertisement
Answer
In request.POST
you don’t have keys from id
attribute, but from name
. Add it to inputs and you’re fine.
JavaScript
1
3
1
<input type="text" id="uname" name="uname"><br>
2
<input type="password" id="pass" name="pass"><br>
3