https://htmx.org logo
Hey guys, I need help with getting a class-based v...
# 🔥-django-htmx
a
Hey guys, I need help with getting a class-based view to properly redirect to
success_url
. I have a contact form with a confirmation view.
Copy code
python
class ContactPageView(FormView):
    template_name = 'contact_page.html'
    base_template = 'contact_form_partial.html'
    extra_context = {'base_template': base_template}
    form_class = ContactForm
    success_url = '/confirm/'


class ConfirmationView(FormView):
    template_name = 'confirmation_page.html'
    base_template = 'contact_form_partial.html'
    extra_context = {'base_template': base_template}
    form_class = ContactForm
    submit = False
    success_url = '/success/'

    def post(self, request, *args, **kwargs):
        if self.submit is True:
            form_class = self.get_form_class()
            form = self.get_form(form_class)
            if form.is_valid():
                self.form_valid(form)
        return render(self.request, self.template_name, self.get_context_data())

    def form_valid(self, form):
        form.send()
        return super().form_valid(form)


class SuccessView(TemplateView):
    template_name = 'success_page.html'
    base_template = 'contact_form_partial.html'
    extra_context = {'base_template': base_template}
My template with HTMX looks like this:
Copy code
html
<form>
        {% for field in form %}
            <div>
            {{ field.as_hidden }}
            {{ field.label }}:
            {{ field.value }}
            </div>
        {% endfor %}
        <button hx-post="send/"
                hx-target="#contact-form"
                hx-swap="outerHTML"
                type="submit">
            Send
        </button>
    </form>
The form will successfully send an email, but it won't redirect to the
success_url
(ie the
#contact-form
element isn't replaced with the
success_page.html
template)