<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Fictitious Nonsense &#187; markitup</title>
	<atom:link href="http://www.fictitiousnonsense.com/archives/tag/markitup/feed" rel="self" type="application/rss+xml" />
	<link>http://www.fictitiousnonsense.com</link>
	<description>&#38; Wasted Ink</description>
	<lastBuildDate>Thu, 12 Nov 2009 05:47:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Overriding a Widget in the Django Admin Site</title>
		<link>http://www.fictitiousnonsense.com/archives/22</link>
		<comments>http://www.fictitiousnonsense.com/archives/22#comments</comments>
		<pubDate>Mon, 06 Oct 2008 04:49:24 +0000</pubDate>
		<dc:creator>varikin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[markitup]]></category>

		<guid isPermaLink="false">http://www.fictitiousnonsense.com/?p=22</guid>
		<description><![CDATA[In an app I am creating in Django, I decided I want to be able to format some text. There are several ways I could do this: allow all HTML, some HTML, or some other markup syntax. I decided I don&#8217;t want HTML for security reasons, either all or just a subset. So I decided [...]]]></description>
			<content:encoded><![CDATA[<p>In an app I am creating in <a title="Django" href="http://www.djangoproject.com/">Django</a>, I decided I want to be able to format some text. There are several ways I could do this: allow all HTML, some HTML, or some other markup syntax. I decided I don&#8217;t want HTML for <a title="Security reasons" href="http://www.codinghorror.com/blog/archives/001167.html">security</a> reasons, either all or just a subset. So I decided to go with another markup syntax<span class="__mozilla-findbar-search" style="black;">. </span>I am looking at both <a title="Markdown" href="http://daringfireball.net/projects/markdown/">Markdown</a> and <a title="Textile" href="http://hobix.com/textile/">Textile</a>. To make this work, I want an &#8220;editor&#8221; because I will never remember all the syntax.</p>
<p>The big part is then getting the editor incorporated into the Django admin site<span class="__mozilla-findbar-search" style="black;">. </span>It took some work, several tries, and some pure luck but in the end I am happy with the results.</p>
<p>First, I chose the <a title="markItUp!" href="http://markitup.jaysalvat.com/home/">markItUp!</a> editor because it looks nice, it is a jQuery plugin (I am alreay using jQuery), and it is not tied to any markup syntax. To use this editor, I needed to configure Django to use a different widget than the normal textarea widget for my model<span class="__mozilla-findbar-search" style="black;">. </span>I found this <a title="Override a widget in new forms admin" href="http://www.kryogenix.org/days/2008/03/28/overriding-a-single-field-in-the-django-admin-using-newforms-admin">post</a> and followed its directions. I worked great, but just didn&#8217;t feel right<span class="__mozilla-findbar-search" style="black;">. </span>Then by pure chance I found that you can specify which form to use for the ModelAdmin. To do this, I created a widget, MarkItUpWidget:</p>
<pre style="30px;">class MarkItUpWidget(forms.Textarea):
    class Media:
        js = (
            'js/jquery.js',
            'js/markitup/jquery.markitup.js',
            'js/markitup/sets/markdown/set.js',
            'js/markItUp_init.js',
        )
        css = {
            'screen': (
                'js/markitup/skins/simple/style.css',
                'js/markitup/sets/markdown/style.css',
            )
        }</pre>
<p>The widget subclasses Textarea and includes the JavaScript and CSS files needed for MarkItUp plus a JavaScript file, markItUp_init.js, that initilizes MarkItUp:</p>
<pre style="30px;">$(document).ready(function()    {
    $('textarea').markItUp(mySettings);
});</pre>
<p>Then a ModelForm, PostAdminForm is needed that uses the MarkItUpWidget:</p>
<pre style="30px;">class PostAdminForm(forms.ModelForm):
    raw_body = forms.CharField(widget=MarkItUpWidget())

    class Meta:
        model = Post</pre>
<p>PostAdminForm uses the Post model and specifies the MarkItUpWidget for raw_body, a model attribute on the Post model which will holds the unprocessed markup text. The last thing is setting the form for the PostAdmin and registering it.</p>
<pre style="30px;">class PostAdmin(admin.ModelAdmin):
	form = PostAdminForm

admin.site.register(Post, PostAdmin)</pre>
<p>After all this, here is what the admin site looks like for the Post.</p>
<dl>
<dt><a href="http://flickr.com/photos/15952500@N04/2916433037/"><img class="aligncenter" src="http://farm4.static.flickr.com/3043/2916433037_42bc54c5dc.jpg?v=0" alt="MarkItUp in Django Admin" width="495" height="378" /></a></dt>
</dl>
<p>The top text area is for the input<span class="__mozilla-findbar-search" style="black;">. </span>The toolbar is added automatically by MarkItUp. The check mark on the toolbar generates a preview of the marked up text which MarkItUp automatically places in the box below.</p>
<p>Then the plain text (top area) is send to Django when saved<span class="__mozilla-findbar-search" style="black;">. </span>I have some server side hooks to process the save and preview, but I will get to that in another post.</p>
<p>There are still some issues being worked out, like the fact that the links are not working (notice the non-blue and non-underlined word &#8220;link&#8221; in the preview), but that is probably something with my server side processing.</p>
<p>The Django documentation tells how to do each bit of this, but doesn&#8217;t show to how put it all together, so it took a while and luck to randomly find the right parts (like setting <em>form </em>in PostAdmin). I am just happy there such a nice simple way to accomplish this. Also, any custom widget could be added to the admin change/add page like this, not just the markItUp! widget for text areas.  The main thing getting the custom widget class setup the way you need it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fictitiousnonsense.com/archives/22/feed</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>
