About Lesson
Handling Form Submission
When a user submits a form, the view will process the data. If the form is valid, you can save it or perform any required action. If not, you can display errors.
Example:
from django.shortcuts import render
from .forms import ContactForm
def contact_view(request):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
# Process form data (e.g., send email, save data)
return render(request, 'success.html')
else:
return render(request, 'contact.html', {'form': form})
else:
form = ContactForm()
return render(request, 'contact.html', {'form': form})
Using POST
and GET
Methods
- GET: Render the empty form.
- POST: Process the submitted form.
Join the conversation