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

Django-formset - i am facing the issue - Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON #28

Closed sasikumar1978k closed 1 year ago

sasikumar1978k commented 1 year ago

i am facing the following error in my modelchoice control change event

Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON

MY CODE

my model class Places(models.Model): placename = models.CharField(max_length=50,unique=True) blockname = models.CharField(max_length=50) pincode = models.CharField(max_length=6) TALUKNAME = models.CharField(max_length=50) rev_dist = models.ForeignKey(REVDISTDET, on_delete=models.CASCADE, verbose_name='REVENUE DISTRICT',default='',null=True, ) edn_dist = models.ForeignKey(EDNDISTDET, on_delete=models.CASCADE, verbose_name='REVENUE DISTRICT',default='',null=True, )

def __str__(self):
    return self.placename

class Meta:
    ordering = ('placename',)

class Officedet(models.Model):

PlaceArea=[('1','Rural'),('2','Urban'),('3', 'NOT APPLICABLE'), ]
YesNo=[('Y','YES'),('N','NO'),('NA', 'NOT APPLICABLE'),]
office_type_choice = [
    ('SCHOOL','SCHOOL'),
    ('ADMIN','ADMIN'),

]
office_code = models.CharField(max_length=15,unique=True)
office_name = models.CharField(max_length=150)
office_address = models.CharField(max_length=250, verbose_name='Address')
office_area = models.CharField(max_length=10,choices = PlaceArea,verbose_name='Area Rural/Urban')
phone_regex = RegexValidator(regex=r'^[789]\d{9}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
office_phone = models.CharField(max_length=10,validators=[phone_regex])
office_head_name = models.CharField(max_length=50,verbose_name='Head Name')
office_head_phone1 = models.CharField(max_length=10,validators=[phone_regex],verbose_name='Office Head Contact Number')
office_head_phone2 = models.CharField(max_length=10,validators=[phone_regex],verbose_name='Office Head Whatsapp Number')
office_mail = models.EmailField(max_length=50,unique=True)
office_place = models.ForeignKey('Places', on_delete=models.CASCADE)

my form class OfficedetForm(forms.ModelForm):

office_logo = fields.FileField(
    label="Office Logo",
    widget=UploadedFileInput,
    required=True,
)

office_gender_type = forms.ModelChoiceField(
    queryset=OFFICE_GENDER_TYPE_CHOICE.objects.all(),
    widget=Selectize(

        placeholder="Choose Gender",
    ),
)

office_mgnt_type = forms.ModelChoiceField(
    queryset=OFFICE_MGNT_TYPE_CHOICE.objects.all(),
    widget=Selectize(

        placeholder="Choose Management",
    ),
)

office_class_level = forms.ModelChoiceField(
    queryset=OfficeClassLevel.objects.all(),
    widget=Selectize(

        placeholder="Choose Class Level",
    ),
)

office_place = forms.ModelChoiceField(
    queryset=Places.objects.all(),
    widget=Selectize(
        search_lookup='placename__icontains',
        placeholder="Choose Place",

    ),
)

office_type = forms.ChoiceField(
    choices=[ ('SCHOOL','SCHOOL'),
    ('ADMIN','ADMIN'),],
    widget=Selectize,
)

default_renderer = FormRenderer(
    form_css_classes='row',
    field_css_classes={
        '*': 'mb-2 col-12', 
        "office_code": 'mb-2 col-2', 
        "office_name": 'mb-2 col-4',
        "office_mail": 'mb-2 col-3',
        "office_place": 'mb-2 col-3',
         '*': 'mb-2 col-12', 
        "office_gender_type": 'mb-2 col-3', 
        "office_mgnt_type": 'mb-2 col-3',
        "office_class_level": 'mb-2 col-3',
        "office_type": 'mb-2 col-3',

         '*': 'mb-2 col-12', 
        "office_tanno": 'mb-2 col-4', 
        "office_udisecode": 'mb-2 col-4', 
        "office_logo": 'mb-2 col-4', 

        }
        )
class Meta:
    model = Officedet
    fields = ["office_code",

"office_name", "office_mail", "office_place", "office_gender_type", "office_mgnt_type", "office_class_level", "office_type", 'office_tanno', 'office_udisecode', "office_logo", ]

My view class OfficeDetailCreateView(AdminUserTypeAuthMixin,FileUploadMixin, FormViewMixin, LoginRequiredMixin, CreateView): model = Officedet template_name = 'home/crud/crud_template.html' form_class = OfficedetForm success_url = reverse_lazy('office_list_view') # or whatever makes sense extra_context = { 'pagetitle':'Office Creation' } my template {% extends "layouts/masterpage-formset.html" %} {% block pagecontent %} {% load render_form from formsetify %}

{% render_form form %}

{% endblock pagecontent %}

jrief commented 1 year ago

seems that you're interpreting HTML as JSON.

A bug report like this, without any context is quite useless for me.

codematsing commented 1 year ago

I'm facing the same issue.

version: 1.0.1

# models
class Donor(models.Model):
    name = models.CharField(max_length=32)

class FundAccount(models.Model):
    donor = models.ForeignKey(Donor)
    name = models.CharField(max_length=32)

class Scholarship(models.Model):
    fund_account = models.ForeignKey(FundAccount)
    name = models.CharField(max_length=32)

# forms.py
class ScholarshipForm(forms.ModelForm):
    donor = forms.ModelChoiceField(
        queryset=Donor.objects.all(),
        )
    fund_account = forms.ModelChoiceField(
        queryset=FundAccount.objects.all(),
        widget=Selectize(
            filter_by={'donor':'donor__id'},
            )
        )
    class Meta:
        model = Scholarship
        fields = ['name']

Error when placing input in donor field in form will cause this issue: image

image

Seems that the js script is reading the whole html document rather than it's capturing json data

Note that this only occurs if I use the filter_by parameter in selectize. If I do not use filter_by, I don't get the issue

jrief commented 1 year ago

Why didn't you use widget Selectize for field donor?

Anyway, that doesn't seem to be the problem, I just tested with the example https://github.com/jrief/django-formset/blob/main/testapp/forms/state.py and everything works fine for me.

codematsing commented 1 year ago

I didn't use Selectize for donor field to isolate the problem that uses filter_by. Lol

I found the issue here to be from my source code

If ever someone would encounter this problem, please ensure that the view you are inheriting also inherits IncompleteSelectResponseMixin in formset.views

since in my sourcecode I did this implementation:

from formset.views import FormViewMixin
from .forms import ScholarshipForm
class ScholarshipCreateView(..., FormViewMixin, CreateView):
    form_class = ScholarshipForm

It was not able to properly process the form using the IncompleteSelectResponseMixin.

A question for @jrief, is it possible for FormViewMixin to inherit IncompleteSelectResponseMixin by default? This could potentially avoid the same case for other developers. I'm just not sure if there will be conflicts or implications for doing so.

Thanks you still for your assistance! Will continue to support your library and try to report or debug any vulnerabilities as I hope for further development of this library :)

jrief commented 1 year ago

A question for @jrief, is it possible for FormViewMixin to inherit IncompleteSelectResponseMixin by default? This could potentially avoid the same case for other developers. I'm just not sure if there will be conflicts or implications for doing so.

IncompleteSelectResponseMixin is only required, if you're using the Selectize and DualSelector widget with asynchronous loading. Not everyone needs that and therefore I prefer to not add another moving part.