{% extends 'temp_basic.html' %}
{% block content %}
<form action='form_output'>
<input type=text name=t1>
<input type=submit value=Submit>
</form>
{% endblock %}
temp_basic.html
<html>
<head>
<title>This is basic template file </title>
</head>
<body>
{% block content %}
{% endblock %}
<br><br>
<center>Displayed by using template file temp_basic.html</center>
</body>
</html>
We will change urls.py page by adding path to handle form submitted data.
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('student/add',views.add_data,name='add_data'),
path('student/form_output',views.form_output,name='form_output')
]
We can display the form by using this URL
http://127.0.0.1:8000/student/add
As we submit the form by clicking the submit button, the last line of code inside urls.py will set the path by using the action attribute of the form to form_output function. 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. {% extends 'temp_basic.html' %}
{% block content %}
from form_output
{% endblock %}
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.
{% extends 'temp_basic.html' %}
{% block content %}
{{msg}}
{% endblock %}
Here we have displayed the welcome message by using the template and by taking the value of variable msgWelcome plus2net
Displayed by using template file temp_basic.html
{% extends 'temp_basic.html' %}
{% block content %}
<form action='form_output' method='POST'>
{% csrf_token %}
<input type=text name=t1>
<input type=submit value=Submit>
</form>
{% endblock %}
We will change form_output function to accept POST Method 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.POST['t1']
str=" Welcome " + str
return render(request,'form_output.html',{'msg':str})
Django Form class
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.