Changes between Version 3 and Version 4 of CustomWidgetsTinyMCE
- Timestamp:
- Nov 16, 2006, 3:27:51 AM (18 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
CustomWidgetsTinyMCE
v3 v4 5 5 To start the custom widget, create a custom_widgets.py file in your project folder 6 6 7 for the TinyMCE widget i usedthe code below7 for the TinyMCE widget utilise the code below 8 8 9 9 {{{ … … 11 11 from django.newforms import * 12 12 from django.newforms.widgets import flatatt 13 from django.newforms.util import smart_unicode 13 14 from django.utils.html import escape 14 15 from django.utils.simplejson import * … … 17 18 """ 18 19 TinyMCE widget. requires you include tiny_mce_src.js in your template 19 you can customize the mce_settings by overwriting the instance20 mce_settings, or add extra options using update20 you can customize the mce_settings by overwriting instance mce_settings, 21 or add extra options using update_settings 21 22 """ 23 22 24 mce_settings = dict( 23 25 mode = "exact", … … 27 29 ) 28 30 29 def update (self, custom):31 def update_settings(self, custom): 30 32 return_dict = self.mce_settings.copy() 31 33 return_dict.update(custom) … … 34 36 def render(self, name, value, attrs=None): 35 37 if value is None: value = '' 36 final_attrs = dict(self.attrs, name=name, id=name) 37 if attrs: 38 final_attrs.update(attrs) 39 40 self.mce_settings['elements'] = name 38 value = smart_unicode(value) 39 final_attrs = self.build_attrs(attrs, name=name) 40 41 self.mce_settings['elements'] = "id_%s" % name 41 42 mce_json = JSONEncoder().encode(self.mce_settings) 42 43 43 return u'<textarea %s >%s</textarea> <script type="text/javascript">\44 44 return u'<textarea%s>%s</textarea> <script type="text/javascript">\ 45 tinyMCE.init(%s)</script>' % (flatatt(final_attrs), escape(value), mce_json) 45 46 }}} 46 47 47 48 48 in the views.py file, replace myproject with your project name49 in the views.py file, import the custom_widget.py file (replace myproject below) and create a form which will use the new TinyMCE widget. 49 50 50 51 … … 57 58 from django.shortcuts import render_to_response 58 59 59 modified_settings = { "auto_resize" : True } 60 cust = dict( auto_resize = True ) 60 61 61 62 class TestForm(Form): 62 63 #test with custom settings 63 64 test_mce = CharField(widget=TinyMCE()) 64 test_mce.widget.mce_settings = test_mce.widget.update(modified_settings) 65 66 #test with no custom settings 65 test_mce.widget.mce_settings = test_mce.widget.update_settings(cust) 66 #test with no custom settings 67 67 another_test = CharField(widget=TinyMCE()) 68 69 68 #plain old textarea 70 69 plain_textarea = CharField(widget=Textarea) 71 70 72 71 def index(request): 73 test_form = TestForm( )74 return render_to_response(' template.html', {'form': test_form })72 test_form = TestForm(auto_id='id_%s') 73 return render_to_response('index.html', {'test_form': test_form }) 75 74 }}} 76 75 77 in your template file simply add the following, replace http://localhost/ with the location of the tinyMCE source served obviouslyon your media server.76 In your template file simply add the following, replace http://localhost/ with the location of the tinyMCE source served on your media server. 78 77 79 78 … … 87 86 <body> 88 87 89 {{ form }} 88 <table> 89 {{ test_form.as_table }} 90 </table> 90 91 91 92 </body> … … 95 96 96 97 97 This implementation isn't perfect by all means 98 1. It requires the template author to include the javascript file. 99 1. tinyMCE.init() is called each time the widget is used, this gives the benefit that you can have multiple tinyMCE text boxes with different settings. 100 101 102 This is as far as I've progressed I haven't yet tested with a Model, validation etc. - if you do please add your experience here. 103 104 Any Feedback/Comments please don't hesitate to email me at john.dagostino AT gmail DOT com 98 This implementation has the following flaw 99 1. It requires the template author to include the javascript source in every template which displays a TinyMCE widget.