bhch / django-jsonform

A better, user-friendly JSON editing form field for Django admin. Also supports Postgres ArrayField.
https://django-jsonform.rtfd.io
BSD 3-Clause "New" or "Revised" License
307 stars 31 forks source link

Form dynamics not working after reloading django-jsonform with HTMX #143

Open ErikKoinberg opened 5 months ago

ErikKoinberg commented 5 months ago

I am using django-jsonform together with HTMX .

I have created a model that looks like this:

`class GroceryStore(models.Model):

PRODUCTS_SCHEMA = {
    "type": "array", 
    "items": {
        "type": "object", 
        "properties": {
            "name": {"type": "string"}, 
            "price": {"type": "number"}, 
            "inventory_size": {"type": "integer"}
        },
        "required": ["name", "price"],
    }
}

creator = models.OneToOneField(User, on_delete = models.CASCADE)

name = models.CharField(max_length = 200)

products = JSONField(schema = PRODUCTS_SCHEMA, null = True, blank = True, default = None)`

That I am rendering using crispy-forms:

Form: `class GroceryStoreForm(ModelForm):

def __init__(self, *args, **kwargs):

    super().__init__(*args, **kwargs)
    self.helper = FormHelper(self)

    self.helper.form_id = 'grocery-store-form'

    self.helper.attrs = {
        "hx-post": reverse_lazy('testing_json_fields'), 
        "hx-target": "#grocery-store-form", 
        "hx-swap": "outerHTML", 
        "autocomplete": "off"
    }

    self.helper.add_input(Submit('submit', 'Save Changes'))

class Meta: 
    model = GroceryStore
    exclude = ["creator"]`

Template: {% load static %} {% load crispy_forms_tags %} {% crispy grocery_form %}

The POST request is successfully sent to the server and a form element is sent back to replace the original using render_crispy_form() and HttpResponse(). However, after the replacement, the interactive features of django-jsonform stop working. What could be the problem?

ErikKoinberg commented 5 months ago

I understand now that I should use the built-in validation for this system, however, I am still curious how to make HTMX work with this. Even when using return render() instead of return HttpResponse() am I not getting it to work.

bhch commented 4 months ago

I don't use HTMX or crispy forms, so I'm going to need a bit of help in understanding the issue better.

Could you upload a minimal sample project on github so I can see first hand what's going on?

abe-101 commented 1 month ago

@ErikKoinberg, I think the issue might be due to the JavaScript being initialized only once on the original form. Since HTMX swaps out the form dynamically, the JavaScript needs to be reinitialized to apply to the new form content.

Since I am new to this library, I may not fully understand its inner workings. However, you can look into the HTMX documentation for ways to trigger JavaScript code after a swap. This might help you reinitialize the necessary components. Here are some relevant links from the HTMX documentation that could be useful: