javascript - 在获取 POST 数据 django 中获取 MultiValueDictError
问题描述
我正在尝试获取从 HTML 表单接收的 GET 数据。
但它给了我 MultiValueDictError。这也是说
During handling of the above exception, another exception occurred:
我的 HTML 代码:
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
</head>
<body>
<form action="home_redirect/fd" id="redirect" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<input type="text" value={{user}} name="user">
<input type="submit">
</form>
<script>
document.getElementById("redirect").submit()
</script>
</body>
</html>
我的意见.py:
def home(request):
user = request.POST['user']
return render(request, 'main.html', {'login': user})
解决方案
在您的 html 中,我删除action
并编写脚本。像这样:
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
</head>
<body>
<form id="redirect" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<input type="text" value={{user}} name="user">
<input type="submit">
</form>
</body>
</html>
在这里我们检查的方法是POST
然后redirect
到你的url
.
def home(request):
user = request.POST
if request.method =="POST":
return redirect('home_redirect') # home_redirect is the redirected url
return render(request, 'afl_announcement/main.html', {'login': user})
推荐阅读
- vue.js - v-date-picker 未在所需月份打开
- c# - 不能将不可为空的对象作为接受 nullalble 的参数传递
- javascript - navigator.serviceWorker.controller.postMessage 为空,但仅在第一页加载时
- c++ - C++ 的 Web 程序集接口类型绑定?
- python - 有时找不到元素
- shell - 需要帮助根据文件夹名称对文件夹进行排序(排序音乐)
- php - 如何在 Jquery 中使用 ajax Php post 方法?
- javascript - 如何根据数组的名称和长度制作动态表单?
- istio - Istio 添加和删除标头,但不会覆盖
- reactjs - 使用 Context Api Hooks 进行异步/等待 - React