mysterious-toddler-20573
08/17/2022, 4:07 PMbumpy-zoo-26881
08/21/2022, 11:05 AMearly-camera-41285
08/21/2022, 6:14 PMhx-vals
attribute on the previous and next button to send along the extra value. https://htmx.org/attributes/hx-vals/
html
<button hx-vals="{goTo: 'next'}">Next</button>
<button hx-vals="{goTo: 'prev'}">Previous</button>
or use hx-include
on each button with a hidden input for each
html
<input id="next-input type="hidden" name="goTo" value="next">
<input id="previous-input" type="hidden" name="goTo" value="prev">
<button hx-include="#next-input">Next</button>
<button hx-include="#previous-input">Previous</button>
early-camera-41285
08/21/2022, 6:18 PMhx-post="/next"
and the other would have hx-post="/prev"
. Then you can handle the logic server sideearly-camera-41285
08/21/2022, 6:30 PMhx-post
attribute is on the form
element; I have made the post request from the button and then used hx-include
to include all form values plus the button value.bumpy-zoo-26881
08/21/2022, 7:14 PMcalm-queen-64495
08/22/2022, 2:06 AMcalm-queen-64495
08/22/2022, 2:07 AMclass RolesList(ListView):
model = Role
template_name = 'workers-roles.html'
# context_object_name = 'roles'
def get_template_names(self):
if self.request.htmx:
return 'partials/roles_list.html'
return self.template_name
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of all the Roles
context['roles'] = Role.objects.all().order_by('name')
return context
rapid-lunch-78326
08/22/2022, 9:46 AMif self.request.htmx and not self.request.htmx.history_restore_request:
calm-queen-64495
08/22/2022, 11:54 AMaloof-crayon-56651
08/23/2022, 2:21 AMsuccess_url
.
I have a contact form with a confirmation view.
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:
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)aloof-crayon-56651
08/23/2022, 2:21 AMaloof-crayon-56651
08/23/2022, 2:24 AMpython
class ConfirmationView(FormView):
success_url = '/success/'
def form_valid(self, form):
form.send()
# I feel like I'm missing something here.
return super().form_valid(form)
My expectation is that return super().form_valid(form)
should redirect to the success_url
specified in ConfirmationView
.aloof-crayon-56651
08/23/2022, 2:57 AMpython
def form_valid(self, form):
...
return render(...)
and
python
def form_valid(self, form):
...
return redirect(self.success_url)
aloof-crayon-56651
08/23/2022, 3:56 AMpython
class ConfirmationView(FormView):
...
def post(self, request, *args, **kwargs):
...
if form.is_valid():
# Add return
return self.form_valid(form)
return render(self.request, self.template_name, self.get_context_data())
strong-addition-37567
08/23/2022, 8:19 PMlogin_required
with my own logic that adds a HX-Redirect=/login
to the response in case we need to re login
2) have the login view return a HX-Retarget=body
to have the content be swapped by the whole page.
3) any other ideas?bitter-monkey-50309
08/24/2022, 5:34 PMHX-Redirect
?strong-addition-37567
08/24/2022, 5:37 PMstrong-addition-37567
08/24/2022, 5:37 PMsquare-manchester-95442
08/25/2022, 9:30 PMsalmon-xylophone-28580
08/26/2022, 1:40 PMmysterious-honey-84546
08/30/2022, 12:45 AMmysterious-honey-84546
08/30/2022, 12:45 AMhundreds-camera-24900
08/30/2022, 1:05 AMrapid-lunch-78326
08/30/2022, 12:33 PM