Help! Django media widget did not appear

scratched my head. I have been following examples of using jquery datepicker for my date field in my form. Here is my code (note: indentation is correct in my code):

from django import forms
from django.contrib.admin.widgets import FilteredSelectMultiple
from myproject.customUsers.models import SchoolClass#,Student
from django.contrib.auth.models import User
from myproject.widgets import CalendarWidget

    class ConsentFormTpl(forms.Form):
        title =  forms.CharField()
        message = forms.CharField()
        deadline = forms.DateField(widget=CalendarWidget)
        availClass  = forms.ModelChoiceField(queryset=SchoolClass.objects.all(),empty_label="None")
        students = forms.ModelMultipleChoiceField(queryset=User.objects.filter(groups__name='Students'),widget=FilteredSelectMultiple("Students",is_stacked=False))

In my widget file, I have:

class CalendarWidget(forms.DateInput):
    class Media:
        css = {
               'all':('css/ui-darkness/jquery-ui-1.8.9.custom.css',),
        }
        js = ('js/jquery-ui-1.8.9.custom.min.js',)

Finally, in my template I use {{form.media}}. Oddly enough, the css and js files for CalendarWidget did not display in my html. (there are files for FilteredSelectMultiple). I checked through a shell that seems already:

>>> from django import forms
>>> from myproject.widgets import CalendarWidget
>>> class C(forms.Form):
...    title = forms.DateField(widget=CalendarWidget)
... 
>>> c=C()
>>> print c.media
<link href="http://xyz.com/media/css/ui-darkness/jquery-ui-1.8.9.custom.css" type="text/css" media="all" rel="stylesheet" />
<script type="text/javascript" src="http://xyz.com/media/js/jquery-ui-1.8.9.custom.min.js"></script>

template:

{% extends "base.html" %}
{% block content %}
<script type="text/javascript">

        $(document).ready(function(){
                //some stuff here
                        })

            }

            //When user change classes, load new set of students
            $("select#id_availClass").change(function(){
                getStud( $(this).val() );
            })

            $("#id_deadline").datepicker();

            // Loads initial set of students when form loads
            getStud(-1);

        })
</script>
<link rel="stylesheet" type="text/css" href="http://xyz.com/media/admin/css/base.css" />
<link rel="stylesheet" type="text/css" href="http://xyz.com/media/admin/css/forms.css" />
<script type="text/javascript" src="/admin/jsi18n/" />
<script type="text/javascript" src="/media/admin/js/core.js" />
{{ form.media }}
<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" name="yes" value="Submit" />
</form> {% endblock %}

Update: I was able to print the form media before inserting it into render_to_response. There are files!

form = ConsentFormTpl()
        print form.media

    return render_to_response('consent_form/consent_form2.html',{'form':form},context_instance=RequestContext(request))

Any signposts?

+3
source share
3 answers

, @seitaridis, , . doctype, ` . , .

+1

.

from django import forms
0

A problem may arise because you specified the class CalendarWidgetinstead of the instance in the keyword argument widget?

Try changing:

deadline = forms.DateField(widget=CalendarWidget)

to

deadline = forms.DateField(widget=CalendarWidget())
-1
source

Source: https://habr.com/ru/post/1793660/


All Articles