<?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 &#187; Django</title>
	<atom:link href="http://www.djangofoo.com/tag/django/feed" rel="self" type="application/rss+xml" />
	<link>http://www.djangofoo.com</link>
	<description>Django Tips and Tweaks</description>
	<lastBuildDate>Wed, 18 Aug 2010 09:38:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Connect/Disconnect django signals</title>
		<link>http://www.djangofoo.com/85/signal-connect-disconnect-django-signals</link>
		<comments>http://www.djangofoo.com/85/signal-connect-disconnect-django-signals#comments</comments>
		<pubDate>Wed, 24 Feb 2010 21:17:17 +0000</pubDate>
		<dc:creator>Davo</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[connect disconnect]]></category>
		<category><![CDATA[post_delete]]></category>
		<category><![CDATA[post_save]]></category>
		<category><![CDATA[pre_delete]]></category>
		<category><![CDATA[pre_save]]></category>
		<category><![CDATA[signals]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=85</guid>
		<description><![CDATA[Django signals allow to bind a function to a specific event. There are 4 major signals available: pre_save() ( before saving ) post_save() ( after saving ) pre_delete() ( before deleting ) post_delete() ( after deleting ) A simple example of connecting a signal: # in models.py from django.db.models.signals import post_save from myapp.models import MyModel [...]]]></description>
			<content:encoded><![CDATA[<p>Django <a href="http://docs.djangoproject.com/en/dev/topics/signals/" target="_new"><strong>signals</strong></a> allow to bind a function to a specific event.</p>
<p>There are 4 major signals available:</p>
<ul>
<li><strong>pre_save()</strong> ( before saving )</li>
<li><strong>post_save()</strong> ( after saving )</li>
<li><strong>pre_delete()</strong> ( before deleting )</li>
<li><strong>post_delete()</strong> ( after deleting )</li>
</ul>
<p>A simple example of <strong>connecting</strong> a signal:</p>
<pre class="brush:python">
# in models.py
from django.db.models.signals import post_save
from myapp.models import MyModel

def do_something(sender, **kwargs):
    # the object which is saved can be accessed via kwargs 'instance' key.
    obj = kwargs['instance']
    print 'the object is now saved.'
    # ...do something else...

# here we connect a post_save signal for MyModel
# in other terms whenever an instance of MyModel is saved
# the 'do_something' function will be called.
post_save.connect(do_something, sender=MyModel)
</pre>
<p>If you need to <strong>disconnect/detach</strong> a signal:</p>
<pre class="brush: python">
# instead of connect() we call disconnect()
post_save.disconnect(do_something, sender=MyModel)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/85/signal-connect-disconnect-django-signals/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get/List model fields</title>
		<link>http://www.djangofoo.com/80/get-list-model-fields</link>
		<comments>http://www.djangofoo.com/80/get-list-model-fields#comments</comments>
		<pubDate>Mon, 22 Feb 2010 14:39:11 +0000</pubDate>
		<dc:creator>Davo</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[fields]]></category>
		<category><![CDATA[get fields]]></category>
		<category><![CDATA[list fields]]></category>
		<category><![CDATA[meta fields]]></category>
		<category><![CDATA[model]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=80</guid>
		<description><![CDATA[It is possible to get/list all of the fields for the given model, to do so you need to access the _meta attribute of the Model. The following example demonstrates how to do it. # in the models.py class Car(models.Model) year = models.IntegerField() name = models.CharField(max_length=50) ... def get_fields(self): # make a list of field/values. [...]]]></description>
			<content:encoded><![CDATA[<p>It is possible to get/list all of the fields for the given model, to do so you need to access the <strong>_meta</strong> attribute of the Model.<br />
The following example demonstrates how to do it.</p>
<pre class="brush: python">

# in the models.py
class Car(models.Model)
    year = models.IntegerField()
    name = models.CharField(max_length=50)
    ...

    def get_fields(self):
        # make a list of field/values.
        return [(field, field.value_to_string(self)) for field in Car._meta.fields]
</pre>
<p>Now that we implemented the get_fields function for our Car model, we can use this in the template&#8230;</p>
<pre class="brush: python">
# your template file...
# I assume that the variable 'car' is an instance of Car object.
{% for field, value in car.get_fields %}
    {{ field }} : {{ value }}
{% endfor %}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/80/get-list-model-fields/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Session array/list append does not work</title>
		<link>http://www.djangofoo.com/57/session-arraylist-append-does-not-work</link>
		<comments>http://www.djangofoo.com/57/session-arraylist-append-does-not-work#comments</comments>
		<pubDate>Mon, 01 Feb 2010 20:19:52 +0000</pubDate>
		<dc:creator>Davo</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[append]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[session]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=57</guid>
		<description><![CDATA[I was trying to save and update some arrays inside the Django session object but it was not working at all&#8230; The following snippet DOES NOT WORK request.session['array'] = [] # let's create an empty array # try to append some integers... request.session['array'].append(1) request.session['array'].append(2) request.session['array'].append(3) ... In order to solve this we need to store [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying to save and update some arrays inside the Django <strong>session</strong> object but it was not working at all&#8230;</p>
<p>The following snippet <strong>DOES NOT WORK</strong></p>
<pre class="brush: python">
request.session['array'] = [] # let's create an empty array
# try to append some integers...
request.session['array'].append(1)
request.session['array'].append(2)
request.session['array'].append(3)
...
</pre>
<p>In order to solve this we need to store the array in a temporary object first.<br />
Quick fix:</p>
<pre class="brush: python">
request.session['array'] = []
tmp = request.session['array']
tmp.append(1)
tmp.append(2)
tmp.append(3)
request.session['array'] = tmp # this works now.
...
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/57/session-arraylist-append-does-not-work/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Django and mod_wsgi deploy example</title>
		<link>http://www.djangofoo.com/17/django-mod_wsgi-deploy-exampl</link>
		<comments>http://www.djangofoo.com/17/django-mod_wsgi-deploy-exampl#comments</comments>
		<pubDate>Wed, 13 Jan 2010 14:22:39 +0000</pubDate>
		<dc:creator>Davo</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[httpd]]></category>
		<category><![CDATA[mod_wsgi]]></category>
		<category><![CDATA[wsgi]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=17</guid>
		<description><![CDATA[So what do we do next when we finished to develop(debug, debug&#8230; debug&#8230;!) our application? We go and deploy it of course! Well, a common choice of deploying Django application is to use Apache httpd and mod_wsgi. Let&#8217;s suppose the following scenario: our application name is marina our application is placed inside /home/ directory our [...]]]></description>
			<content:encoded><![CDATA[<p>So what do we do next when we finished to develop(debug, debug&#8230; debug&#8230;!) our application? We go and deploy it of course!<br />
Well, a common choice of deploying Django application is to use <a href="http://httpd.apache.org/" target="_new" title="Apache Http Server">Apache httpd</a> and <a href="http://code.google.com/p/modwsgi/"  target="_new" title="Mod wsgi for Apache">mod_wsgi</a>.</p>
<p>Let&#8217;s suppose the following scenario:</p>
<ul>
<li>our application name is <strong>marina</strong></li>
<li>our application is placed inside <strong>/home/ </strong>directory</li>
<li>our static files (javascript, images, css etc&#8230;) are inside <strong>/home/marina/media/</strong></li>
</ul>
<p>The first step is to create the WSGI file and place it inside <strong>/home/marina/</strong><br />
With the following content:</p>
<pre class="brush: python">#!/usr/local/bin/python
import os, sys
sys.path.append('/home/')
sys.path.append('/home/marina/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'  # this is your settings.py file
os.environ['PYTHON_EGG_CACHE'] = '/tmp'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()
</pre>
<p>The second step is to edit your <strong>httpd.conf</strong> and add the following lines.</p>
<pre class="brush: python">

&lt;Directory "/home/marina/media"&gt;
   Order deny,allow
   Allow from all
&lt;/Directory&gt;

&lt;Directory "/home/marina"&gt;
    AllowOverride All
    Order deny,allow
    Allow from all
&lt;/Directory&gt;

Alias /media/ /home/marina/media/
ServerAdmin admin@djangofoo.com
ErrorLog "logs/marina.com-error_log"
CustomLog "logs/marina.com-access_log" common
# mod_wsgi configuration is here
# we are running as user/group 'deamon', if you don't have those you need to change or create.
WSGIDaemonProcess marina user=daemon group=daemon processes=2 threads=25
WSGIProcessGroup marina
# this is our WSGI file.
WSGIScriptAlias / /home/marina/marina.wsgi
</pre>
<p>We need to be sure that our folder is readable by the &#8216;<strong>daemon</strong>&#8216; user</p>
<pre class="brush: python">chown -R daemon /home/marina
</pre>
<p>NOTE: if you are using <strong>/media/</strong> to store all the static files be sure you have this inside your settings.py</p>
<pre class="brush: python">ADMIN_MEDIA_PREFIX = '/admin_media/' # by default this is /media/ which conflicts with our /media/ !!
</pre>
<p>And finally don&#8217;t forget to <strong>restart your apache</strong>.</p>
<p>If you still have issues to run a Django application its worth checking the <strong>error logs</strong>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/17/django-mod_wsgi-deploy-exampl/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Display asterisks for required fields in template</title>
		<link>http://www.djangofoo.com/1/asterisks-for-required-fields-in-template</link>
		<comments>http://www.djangofoo.com/1/asterisks-for-required-fields-in-template#comments</comments>
		<pubDate>Mon, 11 Jan 2010 15:44:51 +0000</pubDate>
		<dc:creator>Davo</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[asterix]]></category>
		<category><![CDATA[field]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[required]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=1</guid>
		<description><![CDATA[If you want to display simple asterisks (*) for required fields in Django then the following code is for you. The trick is to access the field properties inside the template and make a conditional statement. Here is a simple example of view and template: # this is inside your views.py def do_something(request): """Display simple [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to display simple asterisks (<strong>*</strong>) for required fields in Django then the following code is for you.<br />
The trick is to access the field properties inside the template and make a conditional statement.<br />
Here is a simple example of <strong>view</strong> and <strong>template</strong>:</p>
<pre class="brush: python">
# this is inside your views.py
def do_something(request):
      """Display simple form"""
      form = MyForm() # this will be used in the template.
      return render_to_response('template.html', locals())

# this is inside your template.html
<form action="somewhere" method="post">
   {% for field in form %}
      {{ field }} {% if field.field.required %}(*){% endif %}
   {% endfor %}
</form>
</pre>
<p>The <a href="http://docs.python.org/library/functions.html#locals" target="_new">locals()</a> function is a built in python function</p>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/1/asterisks-for-required-fields-in-template/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

