[Answered ]-How to send data from HTML form input type number to a variable in app views.py Django

1πŸ‘

βœ…

An input without a name will not send anything back to the server, so that is step 1:

<input name="number_input" type="number" id="typeNumber" class="form-control m-3 w-50 mx-auto" />

Step two is to extract it by using request.GET.get():

# views.py

def home_view(request):
    if request.method == "GET" and 'question' in request.GET:
        question = request.GET.get('question')
        number = int(request.GET.get('number_input'))
        data = custom_funct(question)
        context = {'data': data}
    else:
        context = {}
    return render(request, 'home.html', context) 

NOTE: your custom_number = number will not work since it is outside of a view. I was using your_view as an example only, since I did not know what the name of your view was. You should include my suggestion within your home_view, just like you already have done with question = request.GET('question').

πŸ‘€raphael

Leave a comment