jrief / django-formset

The missing widgets and form manipulation library for Django
https://django-formset.fly.dev/
MIT License
318 stars 30 forks source link

Buttons are completely unresponsive. A simple model utilizing the example from Chapter 9 of the docs produces a dead add button. #65

Closed omarcoming closed 1 year ago

omarcoming commented 1 year ago

I can't get even the simplest model/form button to work using the example in Chapter 9 of the documentation. The following example doesn't throw any errors in the browser console.

models.py:

from django.db import models

class Customer(models.Model):
    first_name = models.CharField('First Name', max_length=55, blank=True)

forms.py:

from django import forms
from .models import *

class CustomerForm(forms.ModelForm):
    class Meta:
        model = Customer
        fields = '__all__'

views.py:

from django.views.generic import UpdateView, ListView
from django.utils.encoding import force_str
from django.http.response import JsonResponse
from django.urls import reverse_lazy

from formset.views import EditCollectionView, FormCollectionView, FormView, FileUploadMixin, FormViewMixin

from .forms import *
from .models import *

class CustomerEditView(FormViewMixin, UpdateView):
    model = Customer
    template_name = 'testing/customer-edit.html'
    form_class = CustomerForm
    success_url = reverse_lazy('customer-list')
    extra_context = None

    def get_object(self, queryset=None):
        if self.extra_context['add'] is False:
            return super().get_object(queryset)

    def form_valid(self, form):
        if extra_data := self.get_extra_data():
            if extra_data.get('delete') is True:
                self.object.delete()
                success_url = self.get_success_url()
                response_data = {'success_url': force_str(success_url)} if success_url else {}
                return JsonResponse(response_data)
        return super().form_valid(form)

class CustomerListView(ListView):
    model = Customer

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        return context

urls.py:

from django.urls import path, re_path

from .views import *

urlpatterns = [
    path('', CustomerListView.as_view(), name='customer-list'),  # list view not handled here
    path('add/', CustomerEditView.as_view(extra_context={'add': True}),
         name='customer-add',
         ),
    path('<int:pk>/', CustomerEditView.as_view(extra_context={'add': False}),
         name='customer-edit',
         ),
]

customer-edit.html:

{% load i18n %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

{% load render_form from formsetify %}

<django-formset endpoint="{{ request.path }}" csrf-token="{{ csrf_token }}">
  {% render_form form %}
  {% if add %}
    <button type="button" click="submit({add: true}) -> proceed">{% trans "Add" %}</button>
  {% else %}
    <button type="button" click="submit({update: true}) -> proceed">{% trans "Update" %}</button>
    <button type="button" click="submit({delete: true}) -> proceed">{% trans "Delete" %}</button>
  {% endif %}

</django-formset>
</body>
</html>

I came across this issue while I was testing/debugging for my project. I thought it maybe had something to do with not loading the typescript implementation of 'click' based on the error I got but this simple example presents the same problem, although without this error:

Uncaught DOMException: Failed to read the 'cssRules' property from 'CSSStyleSheet': Cannot access rules at Object.i [as convertPseudoClasses] (http://127.0.0.1:8001/static/formset/js/chunk-KFPGMBXM.js:1:354) at http://127.0.0.1:8001/static/formset/js/django-formset.js:25:29909

omarcoming commented 1 year ago

Sorry I'm going to move this to discussion