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:
<form hx-post="{% url 'dashboard:profile' %}" hx-target="html" hx-swap="outerHTML">
{% csrf_token %}
...
</form>
url:
path('profile/', views.ProfileUpdateView.as_view(), name='profile')
view:
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:
{% 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?