Django

Create a Feedback Form and Display Submitted Feedback Summary

๐Ÿ“Œ 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.py to define the feedback form
  • views.py to process submitted values
  • template to 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

  • CharField is used for name
  • EmailField is used for email
  • Textarea is 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:

โœ… Output:

Submitted Feedback Summary
Name: Kavita
Email: kavita@example.com
Feedback: This website is very useful for students.

๐Ÿง  How It Works

  1. User opens the feedback form page
  2. Enters name, email, and feedback
  3. Form is submitted using POST
  4. Django validates the form
  5. View extracts values using cleaned_data
  6. 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

  1. Add a subject field in feedback form
  2. Add phone number field
  3. Display feedback in table format
  4. Clear form after submission
  5. 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

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *