Django FormView

Django FormView

Django's class-based views are designed to make common patterns easy to implement. One such pattern is handling forms. The FormView is a class-based view designed specifically for this.

Let's dive into a step-by-step tutorial on how to use Django's FormView:

1. Setting Up a Django Project

Assuming you've already set up a Django project and app. If not, you can quickly create them using:

django-admin startproject myproject cd myproject python manage.py startapp myapp 

2. Create a Form

First, we need a form. Let's create a simple contact form:

In myapp/forms.py:

from django import forms class ContactForm(forms.Form): name = forms.CharField() email = forms.EmailField() message = forms.CharField(widget=forms.Textarea) 

3. Create a FormView

In myapp/views.py:

from django.urls import reverse_lazy from django.views.generic.edit import FormView from .forms import ContactForm class ContactFormView(FormView): template_name = 'contact_form.html' form_class = ContactForm success_url = reverse_lazy('success') def form_valid(self, form): # This method is called when valid form data has been POSTed. # Here, you can handle the data (e.g., send an email, save to DB, etc.) # For simplicity, we'll just print the data. print(form.cleaned_data) return super().form_valid(form) 

In the above code:

  • template_name is the name of the template that displays the form.
  • form_class is the form class that you created earlier.
  • success_url is the URL to which the user will be redirected after a successful form submission. We use reverse_lazy to refer to the URL by its name, ensuring the URL is resolved at runtime rather than at import time.

4. Create the Template

Create a template named contact_form.html:

<form method="post"> {% csrf_token %} {{ form.as_p }} <button type="submit">Submit</button> </form> 

The form.as_p is a quick way to render form fields wrapped in <p> tags. You can customize form rendering as needed.

5. Set up URLs

In myapp/urls.py:

from django.urls import path from .views import ContactFormView urlpatterns = [ path('contact/', ContactFormView.as_view(), name='contact'), path('success/', TemplateView.as_view(template_name='success.html'), name='success'), ] 

Don't forget to import TemplateView at the top:

from django.views.generic.base import TemplateView 

Now, create a simple success.html template to display a success message:

<h1>Thank you for your message!</h1> <p>We'll be in touch soon.</p> 

6. Final Steps

  • Add 'myapp' to INSTALLED_APPS in your project's settings.py.
  • Make sure your project's urls.py includes the app's URLs.
  • Run your development server using python manage.py runserver.

Now, visit /contact/ on your development server. You should see your contact form. After submitting, you should be redirected to the success page.

Conclusion

FormView is a powerful tool in Django for handling form submissions without writing repetitive code. This guide provided a basic introduction. As you get more comfortable, you can override more methods, add more logic, and further customize the behavior of FormView to suit your needs.


More Tags

mobile-webkit android-textinputlayout django-permissions bitmask sqlsrv dylib wikipedia jquery-callback primitive-types richtextbox

More Programming Guides

Other Guides

More Programming Examples