Passing user input to external link in django

286 views Asked by At

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.

3

There are 3 answers

0
Underoos On

There are many typos in your code. You are confusing requests with request. You are importing requests module and passing it to my_view() which is incorrect.

my_view() is a view function which takes a request object, not requests 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

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', {})
0
A67John On

Thanks to @Radwan Abu-Odeh and @Underoos I managed to get it working. Thank you very much for your help.

Working example: .html code

<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, 'my_view.html', {})
2
Radwan Abu-Odeh On

You have a couple of errors in your code that needs to be fixed first

...

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}
        ....

if your site is not receiving any data, check the response from that other site, to make sure you are calling the right API.

I hope this helped