Code

Ticket #11865: form-media-raw-javascript.patch

File form-media-raw-javascript.patch, 1.5 KB (added by tarequeh, 5 years ago)

Suggested changes to django/forms/widget.py

  • django/forms/widgets.py

     
    2929    'SplitDateTimeWidget', 
    3030) 
    3131 
    32 MEDIA_TYPES = ('css','js') 
     32MEDIA_TYPES = ('css', 'js', 'raw_js') 
    3333 
    3434class Media(StrAndUnicode): 
    3535    def __init__(self, media=None, **kwargs): 
     
    4040 
    4141        self._css = {} 
    4242        self._js = [] 
     43        self._raw_js = [] 
    4344 
    4445        for name in MEDIA_TYPES: 
    4546            getattr(self, 'add_' + name)(media_attrs.get(name, None)) 
     
    5758    def render_js(self): 
    5859        return [u'<script type="text/javascript" src="%s"></script>' % self.absolute_path(path) for path in self._js] 
    5960 
     61    def render_raw_js(self): 
     62        if self._raw_js: 
     63            return [u'<script type="text/javascript">\n%s\n</script>' % u'\n'.join(self._raw_js)] 
     64        else: 
     65            return [] 
     66 
    6067    def render_css(self): 
    6168        # To keep rendering order consistent, we can't just iterate over items(). 
    6269        # We need to sort the keys, and iterate over the sorted list. 
     
    8289        if data: 
    8390            self._js.extend([path for path in data if path not in self._js]) 
    8491 
     92    def add_raw_js(self, data): 
     93        if data: 
     94            self._raw_js.extend([script for script in data if script and script not in self._raw_js]) 
     95 
    8596    def add_css(self, data): 
    8697        if data: 
    8798            for medium, paths in data.items():