How to add the feedback form using Django and HTMx
11.04.2025
15.04.2025
6 minutes
48
0
0
0
0
Introduction
In this article, I will describe the way to add on your Django website feedback form using only HTMx and a little bit of DaisyUI as a UI library. Everything is done by using my upcoming website (there is no link to it yet). But, hey, there are some images here ٩(◕‿◕。)۶.
Let's get rid of HTML and templates first
As a starting point, we need to choose a location to put the feedback form. It can be a special page, designed only for a form, a contact's page, or the footer of the website. Specifically, I am going to place it in the footer because I do what I want to do. This is how it's going to look like:

This form is uploaded into the page via an AJAX request that is dynamically loaded during the time when the main page is loading. Via HTMx, you could accomplish this like that:
Here, you can see how I did the request to the server(hx-get) by feedback/ address when this element first appeared in the DOM tree (hx-trigger). The response from the server will be placed in the div element with id="feedback-form-container". Also want to mention, I did use a simple, built-in loading indicator with the help of the hx-indicator attribute, with ID equal to "#simple_indicator".
After the request to feedback/ address, the server will respond with the rendered-ready template, feedback-form.html.
The form will be returned via a context variable, and you can access that variable via the form keyword. BTW, we can iterate through her and get all the form's fields and also errors if there are some. We insert a submit button separate from the rest of the form. And because we want to replace a whole form, we should change the policy of swapping via the hx-swap attribute, and it must be equal to "outerHTML"
Besides the feedback form, in the request, we will receive a result message too. This message will be sent into another template. This is how it is connected with the feedback template:

This is how a feedback form with errors will look like.

This is how a feedback form without errors will look like.
The feedback message template (toast-message.html) is structured like this:
To be able to work correctly, that template must be given two context variables - the message and the status variable. What is more interesting in this template is how and when he sends the requests.
Let me explain. To send the request, the user must click on it or wait 5 seconds after inserting it in the DOM tree (hx-trigger="click, load delay:5s"). The request is sent to api/raw_delete/, and the server will always respond with an empty string and a 200 response code. You could get more about triggers in HTMx here, but about the response codes here.
Here is a view function, which responds only to the GET requests by an api/raw_delete/ address.
It is not required, for sure, to use the GET request; it's just the simplest and fastest to develop. To know more about Python's decorators in Django views, see the link.
In the end, the feedback's message is removed, and the user once again could send the feedback request.
Let's develop the feedback form
After the form templates and message popups from the server have been written, you can and should write a form class that determines which fields should be present in it, which are mandatory, and which are not.
In order to customize a form field for yourself, you need to specify a special widget for it. In widgets, you can specify the values of any attributes (attrs) and also specify an error message (error_messages).
That's all for the form. When we finish creating the feedback form, we can always add or delete unnecessary fields. And we will not have to do anything else because everything will already be done and pre-configured.
Let's develop and connect the FeedbackView class-based view
All that's left is to process the incoming form and return it. Plus a message about the form status. The presentation of the form is very simple, although not without nuances. Here's its code right away:
Let's start in order. If you do everything according to the official documentation, then in addition to specifying such class members as template_name and form_class, you will also need to specify success_url.
And in some cases this will be appropriate, but not in mine. At least I did not find the redirect necessary for me. Therefore, having peeked a little into the source code of the parent class (FormMixin), I overrode the form_valid method and simply returned the form with a message about the result to the client.
And of course, we will connect the created presentation class in urls.py:
Conclusion
I get it; the feedback form works only in case of sending messages back to senders. But it is not always the case. We, for example, could collect those messages in the table (Django model) of the database. And later make use of them: sorting, removing, etc.
About to connect the Django website to email providers I will discuss somewhere later. Till then, we have one of the many ways to add the feedback form to the Django website.
And it is so easy with HTMx, not a single line of JS code, only HTML and Python. It is nice, though.
Comments
(0)
Send
It's empty now. Be the first (o゚v゚)ノ
Other
Similar articles
Used termins
- Django template ⟶ This is a text document marked up with a special syntax for inserting new code.
- HTML (Hyper Text Markup Language) ⟶ HTML, or Hyper Text Markup Language, is the standard markup language used to create and design documents on the World Wide Web. It is not a programming language, but a markup language that structures content on the web. HTML uses various elements and tags to define the different parts of a webpage.
- Website ⟶ Is defined as a collection of related web pages that are typically identified by a common domain name and published on at least one web server. Websites can serve various purposes and can include anything from personal blogs to business sites, e-commerce platforms, or informational resources.
- jQuery ⟶ Is a fast, small, and feature-rich JavaScript library that simplifies the process of handling HTML document traversal and manipulation, event handling, animation, and Ajax interactions for rapid web development. It provides an easy-to-use API that works across a variety of browsers, enabling developers to create dynamic and interactive web applications with
Related questions
- How do I extract content from dynamic web pages? A dynamic website would update the data frequently. For example, there are always new posts on Twitter. To scrape from such a website, it is the same process as scraping other websites, but it would allow the scraper to access the website with some frequency to get the continuously updated data.
- How did email begin to be widely used In 1971 Ray Tomlinson sent the first mail message between two computers on the ARPANET, introducing the now-familiar address syntax with the '@' symbol designating the user's system address. But in parallel with ARPANET, other companies were also developing email systems with similar functionality.