0
0
Djangoframework~30 mins

Form fields and widgets in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Form fields and widgets
📖 Scenario: You are building a simple Django web app to collect user feedback. You want to create a form that asks for the user's name, email, and a message. You will use Django's form fields and widgets to make the form user-friendly.
🎯 Goal: Create a Django form class called FeedbackForm with three fields: name, email, and message. Use appropriate form fields and widgets to make the form easy to use.
📋 What You'll Learn
Create a Django form class named FeedbackForm
Add a name field using CharField with a maximum length of 100
Add an email field using EmailField
Add a message field using CharField with a Textarea widget
Use the forms module from Django
💡 Why This Matters
🌍 Real World
Forms are used in web apps to collect user input like feedback, registrations, or contact info.
💼 Career
Knowing how to create and customize Django forms is essential for backend web developers working with user input.
Progress0 / 4 steps
1
Create the form class and import forms
Import forms from django and create a form class called FeedbackForm that inherits from forms.Form.
Django
Need a hint?

Use from django import forms to import the forms module. Then define class FeedbackForm(forms.Form): to start your form.

2
Add the name and email fields
Inside the FeedbackForm class, add a name field using forms.CharField with max_length=100. Also add an email field using forms.EmailField.
Django
Need a hint?

Use name = forms.CharField(max_length=100) and email = forms.EmailField() inside the form class.

3
Add the message field with Textarea widget
Add a message field to FeedbackForm using forms.CharField with the widget=forms.Textarea argument.
Django
Need a hint?

Use message = forms.CharField(widget=forms.Textarea) to add a multi-line text input.

4
Add a label to the message field
Add a label argument to the message field with the value 'Your Feedback' to make the form clearer.
Django
Need a hint?

Add label='Your Feedback' inside the message field definition.