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',
),
]
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:
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:
forms.py:
views.py:
urls.py:
customer-edit.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: