๐ Introduction
In many websites, a feedback form is used to collect user opinions, suggestions, and comments.
In this program, we will:
- create a feedback form using
forms.py - accept user input
- submit data using POST method
- display the feedback summary on the same page
This program is useful for understanding:
- Django forms
- text input fields
- textarea input
- data display after form submission
๐ฏ Program Statement
๐ Create a feedback form and display submitted feedback summary.
๐ง Concept
This program uses:
forms.pyto define the feedback formviews.pyto process submitted valuestemplateto display the form and summary
โ๏ธ Step 1: Create Form
๐ File: forms.py
๐น Path:
myproject/myapp/forms.py
๐น Code:
from django import forms
class FeedbackForm(forms.Form):
name = forms.CharField(label='Enter Your Name', max_length=100)
email = forms.EmailField(label='Enter Your Email')
message = forms.CharField(
label='Enter Feedback',
widget=forms.Textarea(attrs={'rows': 4, 'cols': 40})
)
๐ง Explanation
CharFieldis used for nameEmailFieldis used for emailTextareais used for feedback message so the user can enter a longer comment
โ๏ธ Step 2: Create View
๐ File: views.py
๐น Path:
myproject/myapp/views.py
๐น Code:
from django.shortcuts import render
from .forms import FeedbackForm
def feedback_form(request):
submitted_data = None
if request.method == 'POST':
form = FeedbackForm(request.POST)
if form.is_valid():
submitted_data = {
'name': form.cleaned_data['name'],
'email': form.cleaned_data['email'],
'message': form.cleaned_data['message']
}
else:
form = FeedbackForm()
return render(request, 'feedback.html', {
'form': form,
'data': submitted_data
})
๐ง Explanation
- Blank form is shown when the page opens
- After submission, Django validates the form
- Submitted values are stored in a dictionary
- That dictionary is sent to the template as
data
โ๏ธ Step 3: URL Mapping
๐ File: urls.py
๐น Path:
myproject/myproject/urls.py
๐น Code:
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('feedback/', views.feedback_form, name='feedback_form'),
]
โ๏ธ Step 4: Create Template
๐ File: feedback.html
๐น Path:
myproject/templates/feedback.html
๐น Code:
<!DOCTYPE html>
<html>
<head>
<title>Feedback Form</title>
</head>
<body>
<h1>Feedback Form</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit Feedback</button>
</form>
<hr>
{% if data %}
<h2>Submitted Feedback Summary</h2>
<p><strong>Name:</strong> {{ data.name }}</p>
<p><strong>Email:</strong> {{ data.email }}</p>
<p><strong>Feedback:</strong> {{ data.message }}</p>
{% endif %}
</body>
</html>
๐ง Explanation
{{ form.as_p }}displays the form fields{% csrf_token %}is required for POST method- After submission, the feedback summary is displayed below the form
โ๏ธ Step 5: Run Server
python manage.py runserver
๐ Step 6: Output
Open:
๐ http://127.0.0.1:8000/feedback/
โ Example Input:
- Name: Kavita
- Email: kavita@example.com
- Feedback: This website is very useful for students.
โ Output:
Submitted Feedback Summary
Name: Kavita
Email: kavita@example.com
Feedback: This website is very useful for students.
๐ง How It Works
- User opens the feedback form page
- Enters name, email, and feedback
- Form is submitted using POST
- Django validates the form
- View extracts values using
cleaned_data - Template displays submitted summary
๐ฅ Key Concepts
Django Form
class FeedbackForm(forms.Form):
Defines the feedback form structure.
EmailField
forms.EmailField()
Used to accept valid email input.
Textarea Widget
widget=forms.Textarea(...)
Used for multi-line text input.
Submitted Summary
submitted_data = {
'name': ...,
'email': ...,
'message': ...
}
Stores values for display in template.
โ ๏ธ Common Errors
โ Forgot {% csrf_token %}
POST form submission will fail.
โ Used CharField instead of EmailField for email
Then email format will not be checked properly.
โ Forgot to import form
Use:
from .forms import FeedbackForm
โ Summary not displaying
Check:
{% if data %}
and ensure data is passed from the view.
โ Textarea not appearing
Make sure the widget is defined properly:
widget=forms.Textarea(attrs={'rows': 4, 'cols': 40})
๐งช Practice Questions
- Add a subject field in feedback form
- Add phone number field
- Display feedback in table format
- Clear form after submission
- Add rating field from 1 to 5
๐ค Viva Questions & Answers
1. Why is EmailField used in this program?
EmailField is used to validate that the entered email follows a proper email format.
2. What is the use of Textarea widget?
The Textarea widget is used to accept long text input over multiple lines. It is suitable for comments and feedback.
3. Why do we use forms.py here?
forms.py helps define the form fields in a structured and reusable way. It also supports validation.
4. Why is POST method used?
POST method is used because the user is submitting data to the server. It is more secure than GET for forms.
5. What is cleaned_data?
cleaned_data contains validated and sanitized values from the submitted form.
6. Why do we use {% csrf_token %}?
It protects the form from CSRF attacks and is required by Django for POST submissions.
7. Can this feedback be saved in database?
Yes, instead of only displaying the summary, the data can also be saved using Django models.
๐ Navigation
๐ Next Post: Create a Feedback Form with Validations such as Required Fields and Email Format
๐ Back to List: Django Programs (60 Questions with Solutions)
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
