djangosnippets.org: Latest snippets posted by joshuahttps://djangosnippets.org/users/joshua/2007-03-01T15:59:50.691166-06:00joshuaTemplate Tag for Retrieving Settings
2007-03-01T15:59:50.691166-06:00joshuahttps://djangosnippets.org/snippets/67/<p>Useage:
<code>{% load setting %}</code>
<code>{% setting DEBUG %}</code>
or...
<code>{% setting MEDIA_ROOT %}</code>
</p>
<p>You get the gist.
</p>
Freely redistributableHackish Way to Execute on manage.py Action
2007-03-01T15:33:04.574598-06:00joshuahttps://djangosnippets.org/snippets/66/<p>It's cheap, hackish, and dirty, but it works. \o/</p>
<p>Lines 19 & 20 are what you should edit.</p>
Freely redistributableSQL Log Middleware
2007-02-28T23:07:47.809755-06:00joshuahttps://djangosnippets.org/snippets/61/<p>This middleware will add a log of the SQL queries executed at the bottom of every page. You can (should) use BeautifulSoup to place this in a specific location.</p>
<p>Note: If you serve non-html content, it would be wise to do a mimetype check.</p>
Freely redistributableRegular Expression Replace Template Filter
2007-02-28T22:07:59.753069-06:00joshuahttps://djangosnippets.org/snippets/60/<p>This will perform a regular expression search/replace on a string in your template.</p>
<p><code>{% load replace %}</code>
<code>{{ mystring|replace:"/l(u+)pin/m\1gen" }}</code></p>
<p>If:
<code>mystring = 'lupin, luuuuuupin, and luuuuuuuuuuuuupin are lè pwn'</code> then it will return:
<code>mugen, muuuuuugen, and muuuuuuuuuuuuugen are lè pwn</code></p>
<p>The argument is in the following format:</p>
<pre><code>[delim char]regexp …</code></pre>
Freely redistributableDynamically Import a Module (and return it)
2007-02-28T11:16:33.338249-06:00joshuahttps://djangosnippets.org/snippets/50/<p><code>foo = dynamic_import ( 'rawr.i.am.a.lion' )</code></p>
<p>Will import <code>lion</code> from <code>rawr.i.am.a</code> and return it. (This isn't really Django specific)</p>
<p>Props to Crast for the original.</p>
Freely redistributableParsing and Highlighting &lt;code&gt; Blocks
2007-02-27T17:48:26.037198-06:00joshuahttps://djangosnippets.org/snippets/39/<p>This function takes a string (most likely from a template), searches it for <code><code>[...]</code></code>, highlights it with Pygments, and returns the entire thing back, as a string. (Note: the <code><code>[...]</code></code> must have a class corresponding to the language inside. If it lacks the class, then it's silently ignored.)</p>
Freely redistributablegetattr template filter
2007-02-27T17:28:52.116275-06:00joshuahttps://djangosnippets.org/snippets/38/<p>Put inside <code>mysite/templatetags/getattr.py</code>
Add <code>mysite</code> to your <code>INSTALLED_APPS</code></p>
<p>In your template:</p>
<pre><code>{% load getattr %}
{{ myobject|getattr:"theattr,default value" }}
</code></pre>
<p>Thanks to pterk for optimizations! \o/</p>
Freely redistributableUsing Pygments with reST
2007-02-27T15:29:48.334024-06:00joshuahttps://djangosnippets.org/snippets/36/<p>UPDATED:
This now supports an argument for the initial header level.</p>
<p>This is a modified version of <code>django.contrib.markup</code> that allows you to highlight code via <a href="http://pygments.pocoo.org/">pygments</a>. The code block can be used as:</p>
<p>`Here's a paragraph, and a code example:</p>
<p>.. code:: language</p>
<pre><code>*insert code here*
</code></pre>
<p>continue with your …</p>
Freely redistributableUsing Textarea
2007-02-27T15:16:50.053860-06:00joshuahttps://djangosnippets.org/snippets/35/<p>A very common field in forms is the <code><textarea></code>, but <code>newforms</code> has no such field. Instead, you must use a dummy field (such as <code>newforms.CharField</code>) and use the <code>newforms.widgets.Textarea()</code> widget to render a textarea.</p>
Freely redistributable