Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

process the media class of a model form in django to a template

Tags:

django

I have a form looking like this:

class MarketingActionForm(forms.ModelForm):

    contact = ManyToManyByLetter(Contact, field_name="first_name")
    #contact = AjaxManyToManyField(Contact, DICT_LOOKUP)

    class Meta:
        model = MarketingAction
        exclude = ('created_by',)

    class Media:
            js = (
                settings.ADMIN_MEDIA_PREFIX + "js/SelectBox.js",
                settings.ADMIN_MEDIA_PREFIX + "js/SelectFilter2.js",
                settings.MEDIA_URL + "js/jquery.js",
                settings.MEDIA_URL + "js/ajax_filtered_fields.js",
            )

I process this form with a view to the template. Now I`m wondering why the Media class is not automatically processed in the template, at least it does not show up in the .html output.

Therefore i want to ask what i have to do in order that the media definitions will show up in the .html output.

I did not find it in the django .docs. Therefore i thought it will be processed automatically.

like image 971
Thomas Kremmel Avatar asked Dec 29 '09 16:12

Thomas Kremmel


People also ask

How do forms get rendered using django templates?

Working with form templates. All you need to do to get your form into a template is to place the form instance into the template context. So if your form is called form in the context, {{ form }} will render its <label> and <input> elements appropriately.

What is form media in django?

Rendering an attractive and easy-to-use web form requires more than just HTML - it also requires CSS stylesheets, and if you want to use fancy widgets, you may also need to include some JavaScript on each page.

What is forms ModelForm in django?

Django Model Form It is a class which is used to create an HTML form by using the Model. It is an efficient way to create a form without writing HTML code. Django automatically does it for us to reduce the application development time.


1 Answers

You'll need to add {{form.media}} in the template yourself. References to form media are not inserted automatically.

It would be very hard to do since entire html document including the <head> section is to be typed by the template designer and django would have to guess where to insert the links if it were to attempt to do it automatically (it would be especially hard to guess correctly for the javascript media - if there are dependencies between scripts)

like image 132
Evgeny Avatar answered Oct 05 '22 14:10

Evgeny