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

Undefined is not a function error #44

Open
s7h opened this issue Jun 30, 2015 · 9 comments
Open

Undefined is not a function error #44

s7h opened this issue Jun 30, 2015 · 9 comments

Comments

@s7h
Copy link

s7h commented Jun 30, 2015

this is my forms.py

from bootstrap3_datetime.widgets import DateTimePicker  
from django import forms

class serviceForm(forms.ModelForm):

    serviceRequestDate = forms.DateTimeField(
        widget=DateTimePicker(options={"format": "YYYY-MM-DD",
                                       "pickTime": False}))

    class Meta:
        model = serviceBooking
        fields = ('serviceRequestDate', 'serviceRequestTime', 'serviceDetails')

this is my template;

<form method="post" action="/services/book/">{% csrf_token %}
                                <div class="col-xs-12 col-sm-6">
                                    <div class="form-group required">
                                        <label for="InputName">Date <sup>*</sup> </label>
                                        {{form.serviceRequestDate}}
                                    </div>
                                    <div class="form-group required">
                                        <label for="InputLastName">Number of Hours <sup>*</sup> </label>
                                        {{form.serviceRequestTime}}
                                    </div>

                                </div>
                                <div class="col-xs-12 col-sm-6">
                                    <div class="form-group required">
                                        <label for="InputZip">Tell us about your work? <sup>*</sup></label>
                                        {{form.serviceDetails}}
                                    </div>


                                </div>
</form>

I have included the {{form.media}} tag in my head but I'm getting undefined is not a function error in my console and the datepicker is not opening.

<script>
            $(function() {
                $("#id_serviceRequestDate_picker").datetimepicker({"pickTime": false, "language": "en-us", "format": "YYYY-MM-DD"});
            });
</script>
@n3storm
Copy link

n3storm commented Jun 30, 2015

Do you already have loaded jquery library in your page?
Are you getting any 404 errores in network pane in your browser?

@s7h
Copy link
Author

s7h commented Jun 30, 2015

Yes, I have loaded jquery in my head before including {{form.media}}
and I'm not getting any 404s

@tonjo
Copy link

tonjo commented Jun 30, 2015

Have you checked the generated html output?
Is id_serviceRequestDate_picker correct?
Given your field name, it should be id_serviceRequestDate.
From Django docs:

Its id, in turn, is generated by prepending 'id_' to the field name. 

@ghost
Copy link

ghost commented Nov 9, 2015

I'm having the same problem... It looks like adding the app to the installed apps doesn't add the javascript files... form.media doesn't either. Or there is a problem with having head in base.html and extending base.html.

@tonjo
Copy link

tonjo commented Nov 9, 2015

Maybe I'm wrong, but simply check the real datepicker id.
I guess it's id_serviceRequestDate, not id_serviceRequestDate_picker.
Check it with Firebug or Chrome developer tools.

@ghost
Copy link

ghost commented Nov 9, 2015

I figured out the issue. If you change the name of the form you send to the template genorator, say my_form instead of form, you have to use {{my_form.media}}. If you have more then one form with a datepicker it is safe to call {{my_form.media}}{{my_other_form.media}}; resources will not be included twice.

OP must be passing the form under a different name, e.g. service_form, in the return render, which means he's not loading the resources and the datetimepicker function call can't be found thus generating the error he is getting.

@ghost
Copy link

ghost commented Nov 9, 2015

Developers you should make a note about this in the readme.

@ghost
Copy link

ghost commented Nov 9, 2015

Perhaps write in an example view where you retrieve and pass the form to the return render and highlight that the name of your form in the view must be the name you use in the {{form.media}} tag.

@ghost
Copy link

ghost commented Nov 9, 2015

Wait... Maybe I'm wrong about the problem. Either way that was my issues.

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