# Get messages and errors in template by htmx

**URL:** https://forum.djangoproject.com/t/get-messages-and-errors-in-template-by-htmx/34549
**Category:** Templates & Frontend
**Created:** [September 6, 2024, 2:47pm UTC](https://forum.djangoproject.com/t/get-messages-and-errors-in-template-by-htmx/34549 "2024-09-06T14:47:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ArianN8610](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ariann8610/32/23073_2.png) [@ArianN8610](https://forum.djangoproject.com/u/ArianN8610)
#### Post date: [September 6, 2024, 2:47pm UTC](https://forum.djangoproject.com/t/get-messages-and-errors-in-template-by-htmx/34549/1 "2024-09-06T14:47:39Z")

</div>

I have a form on page through which the user can update profile information. Everything works fine, but I decided to make the information update without having to reload the page through a post request using htmx. I changed my form as follows:

```auto
<form hx-post="{% url 'dashboard:profile' %}" hx-target="html" hx-swap="outerHTML">
    {% csrf_token %}
    ...
</form>

```

url:

```auto
path('profile/', views.ProfileUpdateView.as_view(), name='profile')

```

view:

```auto
class ProfileUpdateView(View):
    template_name = 'dashboard/profile.html'

    def get(self, request, *args, **kwargs):
        user = request.user
        profile = user.profile
        user_form = forms.UserForm(instance=user)
        profile_form = forms.UserProfileForm(instance=profile)
        context = {'user_form': user_form, 'profile_form': profile_form}
        return render(request, self.template_name, context)

    def post(self, request, *args, **kwargs):
        user = request.user
        profile = user.profile
        user_form = forms.UserForm(request.POST, instance=user)
        profile_form = forms.UserProfileForm(request.POST, instance=profile)
        context = {'user_form': user_form, 'profile_form': profile_form}
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            messages.success(request, 'success')
        else:
            messages.error(request, 'error')
        return render(request, self.template_name, context)

```

Everything works correctly, but the messages that I send through `messages` or the errors that I receive through form validation in the template are not displayed and the page remains unchanged if it receives an error or message.  
template:

```auto
{% include 'src/alerts.html' with messages=messages %}
<form hx-post="{% url 'dashboard:profile' %}" hx-target="html" hx-swap="outerHTML">
    {% csrf_token %}
    {{ user_form }}
    {{ profile_form }}
    <button type="submit">Submit</button>
</form>

```

How can I solve this problem?

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [September 6, 2024, 3:13pm UTC](https://forum.djangoproject.com/t/get-messages-and-errors-in-template-by-htmx/34549/2 "2024-09-06T15:13:11Z")

</div>

You cannot replace the entire html element in htmx. At most, you can replace the innerHTML of the “body” element.  
(See [\</\> htmx ~ hx-swap Attribute](https://htmx.org/attributes/hx-swap/))

I would suggest changing your rendering to render the messages and form as two different divs, with each having the attribute `hx-swap-oob="True"`  
(See [\</\> htmx ~ hx-swap-oob Attribute](https://htmx.org/attributes/hx-swap-oob/))

---

<div class="post-metadata">

### Author: ![ArianN8610](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ariann8610/32/23073_2.png) [@ArianN8610](https://forum.djangoproject.com/u/ArianN8610)
#### Post date: [September 7, 2024, 5:52am UTC](https://forum.djangoproject.com/t/get-messages-and-errors-in-template-by-htmx/34549/3 "2024-09-07T05:52:50Z")

</div>

I tried to replace the body but some of `eventListeners` in my js code were broken and not working properly. Like `DOMContentLoaded`. I tried using `htmx:afterSwap` instead, but that didn’t work either

---

<div class="post-metadata">

### Author: ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)
#### Post date: [September 7, 2024, 11:26am UTC](https://forum.djangoproject.com/t/get-messages-and-errors-in-template-by-htmx/34549/4 "2024-09-07T11:26:15Z")

</div>

I’m sorry, I didn’t mean to imply that you should be replacing body.

You should be replacing _only_ the divs that need to be replaced.

If your form is one div and the messages a different div, then you’d be creating those two divs and sending that as the response.
