Fix amount displayed at page load

This commit is contained in:
Gabriel Augendre 2023-03-27 18:15:47 +02:00
parent ee57139597
commit 1dec9c0bb6

View file

@ -1,3 +1,4 @@
from crispy_forms import layout
from crispy_forms.bootstrap import InlineRadios from crispy_forms.bootstrap import InlineRadios
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from crispy_forms.layout import Div, Layout, Submit from crispy_forms.layout import Div, Layout, Submit
@ -28,7 +29,7 @@ class BasketForm(forms.ModelForm):
self.helper.add_input(Submit("submit", _("Save"))) self.helper.add_input(Submit("submit", _("Save")))
self.helper.attrs = { self.helper.attrs = {
"hx_post": reverse("purchase:price_preview"), "hx_post": reverse("purchase:price_preview"),
"hx_trigger": "keyup delay:500ms,change delay:500ms,load", "hx_trigger": "keyup delay:500ms,change delay:500ms",
"hx_target": "#price_preview", "hx_target": "#price_preview",
"hx_swap": "innerHTML", "hx_swap": "innerHTML",
} }
@ -51,6 +52,9 @@ class BasketForm(forms.ModelForm):
}, },
) )
fields.append(BasketItemField(field_name, product=product)) fields.append(BasketItemField(field_name, product=product))
total = 0
if basket:
total = basket.price / 100
self.helper.layout = Layout( self.helper.layout = Layout(
Div( Div(
*fields, *fields,
@ -58,7 +62,11 @@ class BasketForm(forms.ModelForm):
css_id="products", css_id="products",
), ),
InlineRadios("payment_method"), InlineRadios("payment_method"),
Div(css_id="price_preview", css_class="mb-2"), Div(
layout.HTML(f"Montant total : {total:.2f}"),
css_id="price_preview",
css_class="mb-2",
),
) )
def save(self): def save(self):