I am trying to create a simple django site that would send information to another site with the
requests.post
function (I'm assuming that this is a correct method, but maybe there is a better way).
So far I have a simple .html file created using bootstrap
<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>
and my 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', {})
I want the site to do following: render itself allowing the user to input something in both fields, and after pressing the button to be redirected to another site - in this case 'home'
and for the input to get sent to example.com
. With this moment the page renders correctly but after pressing the button nothing happens and external site receives no data.
There are many typos in your code. You are confusing
requests
withrequest
. You are importing requests module and passing it tomy_view()
which is incorrect.my_view()
is a view function which takes arequest
object, notrequests
library.And also you should be using request.POST to get payload. request.GET returns query params and not payload.
Here's the updated code