我是 Django 和 python 的新手,我从 HTML 页面发出了 POST 请求。但是,当我尝试 request.method === "GET" 来检索数据时,它不起作用。 谢谢
HTML `
<form id="form" action="fd.py" method="post" name="form" enctype="multipart/form-data">
<div class="input-control">
<div class="username">
<label for="username" class="label">Username</label>
<input id="usernameId" name="username" type="text" placeholder="username"
autocomplete="username" maxlength="64">
<div class="error"></div>
</div>
<div class="email">
<label class="lemail" for="email">Email</label>
<input id="emailId" name="email" type="text" placeholder="email">
<div class="error"></div>
</div>
<div class="password">
<label for="password" class="label">Password</label>
<input id="passwordId" name="password" type="password" placeholder="password">
<div class="error"></div>
</div>
<div class="repassword">
<label for="password" class="label">Password</label>
<input id="repasswordId" name="repassword" type="password" placeholder="re-
confirm password"
autocomplete="current-password">
<div class="error"></div>
</div>
<button id="nextButton" class="nextButton" type="submit"></button>
</div>
</form>`
PY
`
from django.http import HttpRequest
from django.contrib.auth import login, logout
def FindRqst(request):
if request.method == "GET":
print("POST Request Found")
else:
print("POST Request Not Found :(")
`
如果我没有导入正确的模块或做错了什么,请告诉我。
在html文件中添加
csrf_Token
登录.html
<form id="form" action="fd.py" method="post" name="form" enctype="multipart/form-data">
{% csrf_token %}
<div class="input-control">
<div class="username">
<label for="username" class="label">Username</label>
<input id="usernameId" name="username" type="text" placeholder="username"
autocomplete="username" maxlength="64">
<div class="error"></div>
</div>
<div class="email">
<label class="lemail" for="email">Email</label>
<input id="emailId" name="email" type="text" placeholder="email">
<div class="error"></div>
</div>
<div class="password">
<label for="password" class="label">Password</label>
<input id="passwordId" name="password" type="password" placeholder="password">
<div class="error"></div>
</div>
<div class="repassword">
<label for="password" class="label">Password</label>
<input id="repasswordId" name="repassword" type="password" placeholder="re-
confirm password"
autocomplete="current-password">
<div class="error"></div>
</div>
<button id="nextButton" class="nextButton" type="submit"></button>
</div>
</form>
使用post方法而不是get这些
FindRqst functions
views.py
from django.http import HttpRequest
from django.contrib.auth import login, logout
def FindRqst(request):
if request.method == "POST":
username = request.POST["username"]
email = request.POST["email"]
password = request.POST["password"]
password = request.POST["password"]
return render(request, "login.html")