We will learn how to take user input by using a web form and then collect the data and process the same by using Python. To keep it simple, we will ask the user to enter his name, after collecting the user name we will display one welcome message using the user input data.
Let us start by creating one simple html page form.html with the basic form input.
We will add the function form_output inside views.py file.
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return render(request,'form.html')
def form_output(request) :
return render(request,'form_output.html')
Above code will display form_output.html page when form is submitted. Now we will not get any form input data here. We need to change our views.py page to accept html form data and then store it in a variable and display. This we will do in our next step.
To display the content of form_output.html we will use the temp_basic.html. Here is the code for form_ouput.html
Above code will display the string saying from form_output. However we want to display dynamic content that is the input data with welcome message. For this we have to modify our form_output function inside views.py.
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return render(request,'form.html')
def form_output(request) :
str=request.GET['t1']
str=" Welcome " + str
return render(request,'form_output.html',{'msg':str})
We need to change form_ouput.html file to use the template temp_basic.html and to display welcome message.