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

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

Closed
sasikumar1978k opened this issue Feb 10, 2023 · 5 comments

Comments

@sasikumar1978k
Copy link

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 %}

Submit Reset Back to list
{% endblock pagecontent %}
@jrief
Copy link
Owner

jrief commented Feb 11, 2023

seems that you're interpreting HTML as JSON.

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

@jrief jrief closed this as completed Feb 11, 2023
@codematsing
Copy link

codematsing commented Aug 12, 2023

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

jrief commented Aug 13, 2023

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

codematsing commented Aug 15, 2023

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

jrief commented Aug 15, 2023

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.

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