问题描述
我正在尝试创建一个简单的Django网站,该网站将通过
requests.post
函数(我假设这是一种正确的方法,但也许有更好的方法)。
<div class="form-group">
<label for="exampleFormControlInput1">text_thing</label>
<input type="text" class="form-control" placeholder="text_thing" name="text_thing">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">number_thing</label>
<input type="number" class="form-control" placeholder="number_thing" name="number_thing">
</div>
<button type="submit" class="btn btn-secondary">Send data</button>
</form>
和我的views.py
from django.shortcuts import render,redirect
import requests
def my_view(requests):
if requests.method == "POST":
text_thing= request.GET.get('text_thing')
number_thing= request.GET.get('number_thing')
post_data = {'text_thing','number_thing'}
if text_thing is not None:
response = requests.post('https://example.com',data=post_data)
content = reponse.content
return redirect('home')
else:
return redirect('home')
else:
return render(requests,'my_view.html',{})
我希望该网站执行以下操作:呈现自身,允许用户在两个字段中输入内容,并在按下按钮后将其重定向到另一个网站-在这种情况下为'home'
,并发送输入到example.com
。此时,页面可以正确呈现,但是在按下按钮之后,什么也没有发生,并且外部站点没有接收到数据。
解决方法
您的代码中存在几个错误,需要首先修复
...
def my_view(requests):
if requests.method == "POST":
# Here is 1,use POST instead
# text_thing= request.GET.get('text_thing')
# number_thing= request.GET.get('number_thing')
# https://docs.djangoproject.com/en/3.1/ref/request-response/#django.http.HttpRequest.POST
text_thing = request.POST.get('text_thing',None)
number_thing = request.POST.get('number_thing',None)
# Here is 2,you are not creating a dict this way,# You are creating a set,put the variables with the keys.
post_data = {'text_thing': text_thing,'number_thing': number_thing}
....
如果您的网站未收到任何数据,请检查该其他网站的响应, 以确保您正在调用正确的API。
我希望这对您有帮助
,您的代码中有很多错别字。您将requests
与request
混淆了。
您正在导入请求模块并将其传递给不正确的my_view()
。
my_view()
是一个视图函数,它使用request
对象,而不是requests
库。
而且您还应该使用request.POST来获取有效负载。 request.GET返回查询参数,而不是有效载荷。
这是更新的代码
from django.shortcuts import render,redirect
import requests
def my_view(request):
if request.method == "POST":
text_thing= request.POST.get('text_thing')
number_thing= request.POST.get('number_thing')
post_data = {'text_thing','number_thing'}
if text_thing is not None:
response = requests.post('https://example.com',data=post_data)
content = reponse.content
return redirect('home')
else:
return redirect('home')
else:
return render(request,'my_view.html',{})
,
感谢@Radwan Abu-Odeh和@Underoos,我设法让它正常工作。非常感谢您的帮助。
工作示例: .html代码
<form method="POST">
<div class="form-group">
<label for="exampleFormControlInput1">text_thing</label>
<input type="text" class="form-control" placeholder="text_thing" name="text_thing">
</div>
<div class="form-group">
<label for="exampleFormControlInput1">number_thing</label>
<input type="number" class="form-control" placeholder="number_thing" name="number_thing">
</div>
<button type="submit" class="btn btn-secondary">Send data</button>
</form>
views.py
def my_view(request):
if request.method == "POST":
text_thing= request.POST.get('text_thing',None)
number_thing= request.POST.get('number_thing',None)
post_data = {'text_thing': text_thing,'number_thing': number_thing}
if text_thing is not None:
response = requests.post('example.com',data=post_data)
return redirect('home')
else:
return redirect('home')
else:
return render(request,{})