Here is input type number in template file
<form action="" method="get">
<input type="text" name="question" id="question" placeholder="Enter Something...">
<input type="number" id="typeNumber" />
<input type="submit" value="Check Now">
</form>
I want to get the number that is selected by the user.
I need to store the number variable value to custom_number variable in views.py file. How can I do this?
def your_view(request):
number = request.GET.get('number_input')
number = int(number)
custom_number = number
def home_view(request):
if request.method == "GET" and 'question' in request.GET:
question = request.GET.get('question')
data = custom_funct(question)
context = {'data': data}
else:
context = {}
return render(request, 'home.html', context)
Thanks
CodePudding user response:
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" />
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')
.