<?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>Django foo</title>
	<atom:link href="http://www.djangofoo.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.djangofoo.com</link>
	<description>Django Tips and Tweaks</description>
	<lastBuildDate>Thu, 05 Apr 2012 14:12:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>DjangoFoo Hosting Updates</title>
		<link>http://www.djangofoo.com/352/djangofoo-hosting-updates</link>
		<comments>http://www.djangofoo.com/352/djangofoo-hosting-updates#comments</comments>
		<pubDate>Wed, 18 Aug 2010 09:38:20 +0000</pubDate>
		<dc:creator>Davo</dc:creator>
				<category><![CDATA[Django]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=352</guid>
		<description><![CDATA[Just a quick entry to let you know the following updates are now available on our DjangoFoo hosting platform Upgraded mod_wsgi to 3.3 New 750GB of backup server available, with weekly backups.]]></description>
			<content:encoded><![CDATA[<p>Just a quick entry to let you know the following updates are now available on our<br />
<strong><a href="http://hosting.djangofoo.com" target="_new">DjangoFoo hosting platform</a></strong></p>
<ul>
<li>Upgraded <a href="http://code.google.com/p/modwsgi/" target="_new">mod_wsgi</a> to 3.3</li>
<li>New <strong>750GB</strong> of <strong>backup</strong> server available, with weekly backups.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/352/djangofoo-hosting-updates/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>RequestContext direct_to_template, TEMPLATE_CONTEXT_PROCESSORS</title>
		<link>http://www.djangofoo.com/344/requestcontext-direct_to_template-template_context_processors</link>
		<comments>http://www.djangofoo.com/344/requestcontext-direct_to_template-template_context_processors#comments</comments>
		<pubDate>Thu, 05 Aug 2010 14:28:12 +0000</pubDate>
		<dc:creator>Davo</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[RequestContextdirect_to_template]]></category>
		<category><![CDATA[TEMPLATE_CONTEXT_PROCESSORS]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=344</guid>
		<description><![CDATA[If you are wondering why you can&#8217;t use {{ request }} in your templates while using direct_to_template then you probably forgot to edit your TEMPLATE_CONTEXT_PROCESSORS in settings.py The default TEMPLATE_CONTEXT_PROCESSORS is without the django.core.context_processors.request TEMPLATE_CONTEXT_PROCESSORS = ( "django.contrib.auth.context_processors.auth", "django.core.context_processors.request", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", "django.contrib.messages.context_processors.messages", ) Hope this works for you.]]></description>
			<content:encoded><![CDATA[<p>If you are wondering why you can&#8217;t use <strong>{{ request }}</strong> in your templates while using<br />
<strong>direct_to_template</strong> then you probably forgot to edit your <strong>TEMPLATE_CONTEXT_PROCESSORS</strong> in <strong>settings.py</strong> </p>
<p>The default TEMPLATE_CONTEXT_PROCESSORS is without the <strong>django.core.context_processors.request</strong></p>
<pre class="brush:python">
TEMPLATE_CONTEXT_PROCESSORS = (
      "django.contrib.auth.context_processors.auth",
      "django.core.context_processors.request",
      "django.core.context_processors.debug",
      "django.core.context_processors.i18n",
      "django.core.context_processors.media",
      "django.contrib.messages.context_processors.messages",
)
</pre>
<p>Hope this works for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/344/requestcontext-direct_to_template-template_context_processors/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Session Key Change On Login</title>
		<link>http://www.djangofoo.com/330/session-key-change-on-login</link>
		<comments>http://www.djangofoo.com/330/session-key-change-on-login#comments</comments>
		<pubDate>Sat, 03 Jul 2010 21:05:43 +0000</pubDate>
		<dc:creator>Arif Harbott</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[sessionkey]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=330</guid>
		<description><![CDATA[If you use the request.session.session_key for an anonymous user e.g. to store shopping cart information, and then use django.contrib.auth login be aware that the session key will change. This tends to catch out new Django developers.]]></description>
			<content:encoded><![CDATA[<p>If you use the <strong>request.session.session_key</strong> for an anonymous user e.g. to store shopping cart information, and then use <strong>django.contrib.auth login</strong> be aware that the session key will change. This tends to catch out new Django developers.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/330/session-key-change-on-login/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get SESSION id/key (session_key)</title>
		<link>http://www.djangofoo.com/326/get-session-idkey-session_key</link>
		<comments>http://www.djangofoo.com/326/get-session-idkey-session_key#comments</comments>
		<pubDate>Wed, 23 Jun 2010 09:50:44 +0000</pubDate>
		<dc:creator>Davo</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[sessionkey]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=326</guid>
		<description><![CDATA[You can easily grab the session key/id from the request object print request.session.session_key]]></description>
			<content:encoded><![CDATA[<p>You can easily grab the <strong>session key/id</strong> from the request object</p>
<pre class="brush:python">
     print request.session.session_key
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/326/get-session-idkey-session_key/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Autoreload/Restart modwsgi on source change</title>
		<link>http://www.djangofoo.com/320/autoreloadrestart-modwsgi-on-source-change</link>
		<comments>http://www.djangofoo.com/320/autoreloadrestart-modwsgi-on-source-change#comments</comments>
		<pubDate>Sun, 20 Jun 2010 10:50:40 +0000</pubDate>
		<dc:creator>Davo</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[autoreloadmodwsgi]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=320</guid>
		<description><![CDATA[When developing with Django&#8217;s DEV server it has a very nice feature, the auto-reload. That is, every time you make changes to a file, the server is automatically restarted. We can have the same with Apache/modwsgi, all you need to do is the following: pre requirements: Linux, python, apache, modwsgi running with WSGIDaemonProcess 1) Find [...]]]></description>
			<content:encoded><![CDATA[<p>When developing with  Django&#8217;s DEV server it has a very nice feature, the auto-reload.<br />
That is, every time you make changes to a file, the server is automatically restarted.  </p>
<p>We can have the same with Apache/modwsgi, all you need to do is the following:<br />
<strong>pre requirements</strong>: Linux, python, apache, modwsgi running with WSGIDaemonProcess</p>
<p>1) Find the <strong>site-packages</strong> of your current python </p>
<pre class="brush:python">
python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
</pre>
<p>2) <strong>monitor.py</strong> , copy this file inside your site-packages directory</p>
<pre class="brush:python">
import os
import sys
import time
import signal
import threading
import atexit
import Queue

_interval = 1.0
_times = {}
_files = []

_running = False
_queue = Queue.Queue()
_lock = threading.Lock()

def _restart(path):
    _queue.put(True)
    prefix = 'monitor (pid=%d):' % os.getpid()
    print >> sys.stderr, '%s Change detected to \'%s\'.' % (prefix, path)
    print >> sys.stderr, '%s Triggering process restart.' % prefix
    os.kill(os.getpid(), signal.SIGINT)

def _modified(path):
    try:
        # If path doesn't denote a file and were previously
        # tracking it, then it has been removed or the file type
        # has changed so force a restart. If not previously
        # tracking the file then we can ignore it as probably
        # pseudo reference such as when file extracted from a
        # collection of modules contained in a zip file.

        if not os.path.isfile(path):
            return path in _times

        # Check for when file last modified.

        mtime = os.stat(path).st_mtime
        if path not in _times:
            _times[path] = mtime

        # Force restart when modification time has changed, even
        # if time now older, as that could indicate older file
        # has been restored.

        if mtime != _times[path]:
            return True
    except:
        # If any exception occured, likely that file has been
        # been removed just before stat(), so force a restart.

        return True

    return False

def _monitor():
    while 1:
        # Check modification times on all files in sys.modules.

        for module in sys.modules.values():
            if not hasattr(module, '__file__'):
                continue
            path = getattr(module, '__file__')
            if not path:
                continue
            if os.path.splitext(path)[1] in ['.pyc', '.pyo', '.pyd']:
                path = path[:-1]
            if _modified(path):
                return _restart(path)

        # Check modification times on files which have
        # specifically been registered for monitoring.

        for path in _files:
            if _modified(path):
                return _restart(path)

        # Go to sleep for specified interval.

        try:
            return _queue.get(timeout=_interval)
        except:
            pass

_thread = threading.Thread(target=_monitor)
_thread.setDaemon(True)

def _exiting():
    try:
        _queue.put(True)
    except:
        pass
    _thread.join()

atexit.register(_exiting)

def track(path):
    if not path in _files:
        _files.append(path)

def start(interval=1.0):
    global _interval
    if interval < _interval:
        _interval = interval

    global _running
    _lock.acquire()
    if not _running:
        prefix = 'monitor (pid=%d):' % os.getpid()
        print >> sys.stderr, '%s Starting change monitor.' % prefix
        _running = True
        _thread.start()
    _lock.release()
</pre>
<p>3) Finally edit your application WSGI file and add the following <strong>before your application=django.core.handlers.wsgi.WSGIHandler()</strong>:</p>
<pre class="brush:python">
...
import monitor

monitor.start(interval=5.0) # check every 5 seconds

for root,dirs,files in os.walk('/some/folder/where/your/app/is'):
        for x in files:
            if os.path.splitext(x)[1].lower() == '.py': # only .py files please.
                monitor.track(root + x)
...
</pre>
<p>original code is at <a href="http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Monitoring_For_Code_Changes" target="_new">http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode#Monitoring_For_Code_Changes</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/320/autoreloadrestart-modwsgi-on-source-change/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Django Foo hosting now in Russian</title>
		<link>http://www.djangofoo.com/317/django-foo-hosting-now-in-russian</link>
		<comments>http://www.djangofoo.com/317/django-foo-hosting-now-in-russian#comments</comments>
		<pubDate>Sat, 19 Jun 2010 11:03:56 +0000</pubDate>
		<dc:creator>Davo</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[djangorussianhosting]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=317</guid>
		<description><![CDATA[We are pleased to anounce that our hosting platform is now available in Russian. Many thanks to Viktor &#8220;Barbuza&#8221; Kotseruba who helped with the translation.]]></description>
			<content:encoded><![CDATA[<p>We are pleased to anounce that our hosting platform is now available in Russian.<br />
Many thanks to <a href="http://twitter.com/barbuza" target="_new">Viktor &#8220;Barbuza&#8221; Kotseruba</a> who helped with the translation.</p>
<p align="center">
<a target="_new" title="Django Hosting" href="http://hosting.djangofoo.com"><img alt="Django Hosting" src="http://www.djangofoo.com/wp-content/themes/clean-home/images/django_hosting.png"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/317/django-foo-hosting-now-in-russian/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Display Django Version</title>
		<link>http://www.djangofoo.com/313/display-django-version</link>
		<comments>http://www.djangofoo.com/313/display-django-version#comments</comments>
		<pubDate>Fri, 18 Jun 2010 13:36:48 +0000</pubDate>
		<dc:creator>Davo</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[getdjangoversion]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=313</guid>
		<description><![CDATA[Ever wondered how to detect which version of Django you are actually running? Well it is as simple as this: >>> import django >>> django.VERSION (1, 2, 1, 'final', 0) >>> django.get_version() u'1.2.1 SVN-13348' >>>]]></description>
			<content:encoded><![CDATA[<p>Ever wondered how to detect which version of Django you are actually running?<br />
Well it is as simple as this:</p>
<pre class="brush:python">
>>> import django
>>> django.VERSION
(1, 2, 1, 'final', 0)
>>> django.get_version()
u'1.2.1 SVN-13348'
>>>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/313/display-django-version/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use javascript, css in admin</title>
		<link>http://www.djangofoo.com/304/use-javascript-css-in-admin</link>
		<comments>http://www.djangofoo.com/304/use-javascript-css-in-admin#comments</comments>
		<pubDate>Wed, 02 Jun 2010 14:17:44 +0000</pubDate>
		<dc:creator>Davo</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[admincss]]></category>
		<category><![CDATA[adminjavascript]]></category>
		<category><![CDATA[adminjs]]></category>
		<category><![CDATA[adminmediajs]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=304</guid>
		<description><![CDATA[Ever wanted to use your own JavaScript OR CSS inside the built in Django admin? Well&#8230; it is possible! All you need to do is the following: 1) Add a admin.py (if you don&#8217;t already have yet&#8230;) for your app from django.db import models from django.contrib import admin from myapp.hotels.models import Hotel class HotelAdmin(admin.ModelAdmin): ... [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to use your own <strong>JavaScript</strong> OR <strong>CSS</strong> inside the built in Django <strong>admin</strong>?<br />
Well&#8230; it is possible!</p>
<p>All you need to do is the following:<strong><br />
1)</strong> Add a <strong>admin.py</strong> (if you don&#8217;t already have yet&#8230;) for your app</p>
<pre class="brush:python">
from django.db import models
from django.contrib import admin

from myapp.hotels.models import Hotel

class HotelAdmin(admin.ModelAdmin):
    ...
    class Media:
        js = ("/media/javascript/yourjs.js",)

admin.site.register(Hotel, HotelAdmin)
</pre>
<p><strong>2)</strong> Create a file <strong>yourjs.js</strong> under <strong>/media/javascript</strong></p>
<pre class="brush:python">
(function($) {
    $(document).ready(function($) {
         // you can now use jquery / javascript here...
         alert('It worked.');
    });
})(django.jQuery);
</pre>
<p><strong>3)</strong> Open the admin for your application and you should see the &#8216;It worked&#8217; message.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/304/use-javascript-css-in-admin/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>AVG Decimal simplejson (Decimal is not JSON serializable)</title>
		<link>http://www.djangofoo.com/228/avg-decimal-simplejson-decimal-is-not-json-serializable</link>
		<comments>http://www.djangofoo.com/228/avg-decimal-simplejson-decimal-is-not-json-serializable#comments</comments>
		<pubDate>Wed, 26 May 2010 11:46:48 +0000</pubDate>
		<dc:creator>ekaterina</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Decimalisnotjsonseriazable]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=228</guid>
		<description><![CDATA[If you use AVG aggregation function on Integer field in MySQL, hotels = Hotel.objects.filter(city = "Paris").extra( select={ 'avg_rate': 'SELECT AVG(hotel_review.rate) FROM hotel_review WHERE hotel_review.is_active = 1 AND hotel_review.hotel_id = hotel_hotel.id', }, ) you will have in a result { &#8220;avg_rate&#8221; : Decimal(&#8217;2.6667&#8242;)} and an &#8220;Exception Value: Decimal(&#8217;2.6667&#8242;) is not JSON serializable&#8221;. In this case you [...]]]></description>
			<content:encoded><![CDATA[<p>If you use AVG aggregation function on Integer field in MySQL, </p>
<pre class="brush:python">
hotels = Hotel.objects.filter(city = "Paris").extra(
    select={
        'avg_rate': 'SELECT AVG(hotel_review.rate) FROM hotel_review WHERE hotel_review.is_active = 1 AND hotel_review.hotel_id = hotel_hotel.id',
    },
)</pre>
<p>you will have in a result { &#8220;avg_rate&#8221; : Decimal(&#8217;2.6667&#8242;)} and an <strong>&#8220;Exception Value: Decimal(&#8217;2.6667&#8242;) is not JSON serializable&#8221;.</strong><br />
In this case you can do this:</p>
<pre class="brush:python">
from django.utils import simplejson
from decimal import Decimal

class MyJSONEncoder(simplejson.JSONEncoder):
    """JSON encoder which understands decimals."""

    def default(self, obj):
        '''Convert object to JSON encodable type.'''
        if isinstance(obj, Decimal):
            return "%d" % obj
        return simplejson.JSONEncoder.default(self, obj)

hotels_json = simplejson.dumps(list(hotels.values()), cls = MyJSONEncoder)
</pre>
<p>You can read about extra search fields and JSON serialization in <a href="http://www.djangofoo.com/212/extra-select-field-in-json">our article</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/228/avg-decimal-simplejson-decimal-is-not-json-serializable/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Increment/Decrement Field value</title>
		<link>http://www.djangofoo.com/298/incrementdecrement-field-value</link>
		<comments>http://www.djangofoo.com/298/incrementdecrement-field-value#comments</comments>
		<pubDate>Wed, 26 May 2010 11:43:50 +0000</pubDate>
		<dc:creator>Davo</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[incrementdecrement]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=298</guid>
		<description><![CDATA[Incrementing or decrementing field values can be done by using the F() function. UPDATE FIELD = FIELD + 1 WHERE&#8230; ? from django.db.models import F # views.py ... offer = Offer.objects.get(...) # SQL: UPDATE field_to_increment = field_to_increment + 1 ... offer.field_to_increment = F('field_to_increment') + 1 offer.save()]]></description>
			<content:encoded><![CDATA[<p><strong>Incrementing</strong> or <strong>decrementing</strong> field values can be done by using the F() function.<br />
UPDATE FIELD = FIELD + 1 WHERE&#8230; ?  </p>
<pre class="brush:python">
from django.db.models import F

# views.py ...
offer = Offer.objects.get(...)
# SQL: UPDATE field_to_increment = field_to_increment + 1 ...
offer.field_to_increment = F('field_to_increment') + 1
offer.save()
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/298/incrementdecrement-field-value/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
