django-components/docs/examples/form_submission
2025-10-21 15:30:08 +02:00
..
images docs: Add "scenarios" code examples (#1445) 2025-10-08 00:17:31 +02:00
component.py refactor: make it optional having to specify parent class of Args, Kwargs, Slots, etc (#1466) 2025-10-21 15:30:08 +02:00
page.py docs: Add "scenarios" code examples (#1445) 2025-10-08 00:17:31 +02:00
README.md docs: Add "scenarios" code examples (#1445) 2025-10-08 00:17:31 +02:00
test_example_form_submission.py docs: Add "scenarios" code examples (#1445) 2025-10-08 00:17:31 +02:00

Form Submission

Handle the entire form submission flow in a single file. From UI definition to server-side handler, without Django's Form class and without modifying urlpatterns.

  1. Define the form to submit in the HTML as a <form>.

  2. Add a View.post() method on the same component that defines the <form>, to define how to process the form data and return a partial HTML response.

  3. Obtain the URL to submit the form to and set it as the action attribute of the <form>. You don't need to go to your urlpatterns. The submission URL is dynamically generated using get_component_url().

The ContactFormComponent renders a simple form. After submission, it receives a partial HTML response and appends a "thank you" message below the form.

Form Submission example

Definition

--8<-- "docs/examples/form_submission/component.py"

Example

To see the component in action, you can set up a view and a URL pattern as shown below.

views.py

--8<-- "docs/examples/form_submission/page.py"

urls.py

from django.urls import path

from examples.pages.form_submission import FormSubmissionPage

urlpatterns = [
    path("examples/form_submission", FormSubmissionPage.as_view(), name="form_submission"),
]