# ajax-form The `ajax-form` is a custom element which wraps around a regular HTML `form` element, but submits the form data via AJAX. This allows the form to be submitted without a page reload, similar to using [HTMX](https://htmx.org/) and the `hx-swap` functionality. ```{note} Make sure to [install `dj_angles` middleware](../installation.md#middleware) to use `is_post` and `is_ajax`. The `dj_angles` scripts must also be [included in the HTML template](../installation.md#scripts). ``` ## Example ```python # views.py from django.shortcuts import render from book.models import Book def book_detail(request, book_id): book = Book.objects.filter(id=book_id).first() if request.is_post: book.title = request.POST.get('title') book.save() if not request.is_ajax: return redirect('book', id=book.id) return render(request, 'book.html', {'book': book}) ``` ```html
{% csrf_token %}
``` ### `form` tag There is also a shortcut [`form` tag element](../tag-elements.md#form) when using the `dj-angles` template loader. ```html ``` ## Attributes ### `swap` Defines how the form should be replaced after submission. Valid values are `outerHTML` and `innerHTML`. Defaults to `outerHTML`. ```html
{% csrf_token %}
``` ### `delay` Defines the delay in milliseconds before the form is submitted. Defaults to 0. ```html
{% csrf_token %}
``` ## Why not HTMX? [`HTMX`](https://htmx.org/) is a helpful, general solution for this use case as well. ### HTMX Example ```html
{% csrf_token %}