<?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; Django</title>
	<atom:link href="http://www.fictitiousnonsense.com/archives/tag/django/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>
		<item>
		<title>Django 1.0 is out in the wild!</title>
		<link>http://www.fictitiousnonsense.com/archives/18</link>
		<comments>http://www.fictitiousnonsense.com/archives/18#comments</comments>
		<pubDate>Thu, 04 Sep 2008 14:52:05 +0000</pubDate>
		<dc:creator>varikin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.fictitiousnonsense.com/?p=18</guid>
		<description><![CDATA[I have been closely following Django as it nears the 1.0 release.  I have been using Django for about 6 months now.  It is the first framework that I have actually grokked.  Before Django, I never understood the idea of frameworks.  I don&#8217;t know if Django is the best, but since I really like Python, [...]]]></description>
			<content:encoded><![CDATA[<p>I have been closely following <a title="Django" href="http://www.djangoproject.com">Django</a> as it nears the 1.0 release.  I have been using Django for about 6 months now.  It is the first framework that I have actually grokked.  Before Django, I never understood the idea of frameworks.  I don&#8217;t know if Django is the best, but since I really like Python, it suits me.</p>
<p>Also, my favorite changeset is <a title="Changeset #8862" href="http://code.djangoproject.com/changeset/8862">#8862</a> because I wrote the patch used and I was thanked in the commit message.  It makes me happy:)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fictitiousnonsense.com/archives/18/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging Django</title>
		<link>http://www.fictitiousnonsense.com/archives/12</link>
		<comments>http://www.fictitiousnonsense.com/archives/12#comments</comments>
		<pubDate>Sat, 02 Aug 2008 04:26:58 +0000</pubDate>
		<dc:creator>varikin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.fictitiousnonsense.com/?p=12</guid>
		<description><![CDATA[I have been wondering good ways to debug Django and stumbled across this, Debugging Django.  It is great!]]></description>
			<content:encoded><![CDATA[<p>I have been wondering good ways to debug Django and stumbled across this, <a href="http://simonwillison.net/2008/May/22/debugging/">Debugging Django</a>.  It is great!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fictitiousnonsense.com/archives/12/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title></title>
		<link>http://www.fictitiousnonsense.com/archives/6</link>
		<comments>http://www.fictitiousnonsense.com/archives/6#comments</comments>
		<pubDate>Wed, 30 Jul 2008 03:45:26 +0000</pubDate>
		<dc:creator>varikin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.fictitiousnonsense.com/?p=6</guid>
		<description><![CDATA[I have recently been working on a new website for my wife, www.jesleephotos.com.  It is currently a Joomla site, which I originally liked, but then I wanted to upgrade.  Which meant rewriting some customizations.  It was a long tedious thing.  Then I started learning Django, and decided to completely write a new site for her [...]]]></description>
			<content:encoded><![CDATA[<p>I have recently been working on a new website for my wife, <a href="http://www.jesleephotos.com" target="_self">www.jesleephotos.com</a>.  It is currently a Joomla site, which I originally liked, but then I wanted to upgrade.  Which meant rewriting some customizations.  It was a long tedious thing.  Then I started learning Django, and decided to completely write a new site for her in Django.  So far it is going great, but it is taking time.</p>
<p>The thing I like about Django is complicated piece I think I need to do turns out to be a simple bit if Django.  The ORM is great, the templates have almost everything I need, and the automatic admin site is a blessing!  Why bother writing and designing my own admin section when it can be given to me for free.  I need to override some bits of it, but the bulks done.  That way I can concentrate on the main part of the site.</p>
<p>I have fallen for Django.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fictitiousnonsense.com/archives/6/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
