Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ErikKoinberg opened this issue Feb 4, 2024 · 3 comments
Open

Comments

@ErikKoinberg
Copy link

ErikKoinberg commented Feb 4, 2024

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 ErikKoinberg changed the title From dynamics not working after reloading it with HTMX Form dynamics not working after reloading django-jsonform with HTMX Feb 4, 2024
@ErikKoinberg
Copy link
Author

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
Copy link
Owner

bhch commented Feb 7, 2024

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
Copy link

abe-101 commented May 14, 2024

@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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants