Login

Most bookmarked snippets

Snippet List

LinkSleeve comment moderation

This is an extension to ubernostrum's [comment-utils](http://code.google.com/p/django-comment-utils/) that performs comment moderation based on LinkSleeve check result. It requires original comment-utils package.

  • akismet
  • comment
  • moderation
  • linksleeve
Read More

PHP's number_format like template filter

This filter allows you to format numbers like PHP's [number_format](http://php.net/number_format) function. If `var` equals 1234.567`{{ var|number_format:2 }}` produces 1,234.56. Because Django's template filters support just 1 argument you'll have to adjust the argument's default values by hand until [#1199](http://code.djangoproject.com/ticket/1199) is fixed.

  • filter
  • number_format
Read More

HTTP method_required Decorator

Edit: As James pointed out, `django.views.decorators.http` already provides stuff for this. Use that instead. Old description: Should be pretty straightforward; you give it the method you can accept, it returns 405's for other methods. EG, `@method_required("POST")` at the top of your view.

  • http
  • decorator
Read More

CustomChoiceField, Selectable label field version of ModelChoiceField

**The problem** ModelChoiceField always uses __unicode__ or __str__ to fill the labels. I needed to dynamically select which field to use for the labels. **The solution** My approach copies a lot from [this blog](http://oebfare.com/blog/2008/feb/23/overriding-modelchoicefield-labels/) with some modifications to make it more dynamic. There are some proposals to fix on this [Ticket #4620]( http://code.djangoproject.com/ticket/4620) **How to use** Include the code on your forms.py (or whatever you are using) and use the CustomChoiceField with the extra argument label_field instead of ModelChoiceField. Hope it helps someone.

  • newforms
  • forms
  • modelchoicefield
  • modelform
Read More

escapejs block tag

Block tag version of [escapejs](http://www.djangoproject.com/documentation/templates/#escapejs). Handy when using inclusion tags to generate AJAX responses.

  • tag
  • javascript
  • escapejs
Read More

Reshape list for table, flatten index in nested loops

Sometimes we want to render items as cells in table with fixed row-count and computable col-count shape and vice versa. It's not difficult to do it with compute odd and even row, col index, but more common way is to use some reshape function. Theare are simple TAG for it: "table", with use reshape function and FILTER "flatindex" wich can use to get flat index in nested loops (may use with table like in this example). Example of usage: `{# List filials must exists in context! #} {% load table %} <table border="0" width="100%"> {% table filials "3x?" %} {% for row in table_obj %} <tr> {% for record in row %} {% if record %} <td class="mf_table_cell" onclick="selcell('filial_{{forloop|flatindex}}')"> <img src="/art/filial.gif" style="margin-bottom: 4px;"/><br/> <span id="filial_{{forloop|flatindex}}" class="mf_table_unselcell">{{ record }}</span> </td> {% else %} <td class="mf_table_cell"> &nbsp; </td> {% endif %} {% endfor %} </tr> {% endfor %} {% endtable %} </table>` /best regards Yosifov Pavel

  • table
  • flatindex
Read More

Zope testing django layer

[zope.testing](http://pypi.python.org/pypi/zope.testing/) is a test framework and test runner, similar to the django test runner and nose. This snippet is a [Layer](http://pypi.python.org/pypi/zope.testing/3.5.1#layers) class which you can assign as a layer attribute of your test suite to initialise and clean up the django test environment appropriately and to reset the test database between tests. for example: tests/suite.py import unittest from zope.testing import doctest def test_suite(): suite = doctest.DocFileSuite("models.txt") suite.layer = DjangoLayer return suite runtests.py import os from zope.testing import testrunner os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings' defaults = [ '--path', 'tests', '--tests-pattern', '^suite$', '-c' ] testrunner.run(defaults)

  • testing
  • tests
  • test
  • zope.testing
Read More

really spaceless (trim spaces at line start)

This tag is meant to override the current implementation of '{% spaceless %}' tag and remove spaces at the beginning of a line too. I.e. a template like this: <div> <div>useless space up front</div> </div> will become this `<div>` `<div>useless space up front</div>` `</div>` All the other behaviour of spaceless stays the same! Put this in your app/name/templatetags/tags.py And if you want it to override the default "spaceless"-tag to the following from django.template import add_to_builtins add_to_builtins('app.name.templatetags.tags')

  • template
  • tag
  • trim
  • spaceless
Read More

Debug-only static serving

A simple way to enable static serving while in development stage still - on release, simply set up the web server to serve the static content instead, and adjust `MEDIA_URL` accordingly.

  • static-serve
Read More

Easier tags with parameters as dictionary values

This is just an example, **NOT any particular tag**. I was just tiered in examining every bits in list. I converted list to dictionary for easier manipulation of parameters. You can use this keeping in mind syntax: {% tag_name 1_key 1_value 2_key 2_value %} and so on...

  • tag
Read More

blocksetter

Sometimes you need to set a little bit more complex variable in the template (e.g. Title), that is being used more than once. this simple tag defines "blockset" tag. {% blockset variable-name %}{%endblockset} everything inside body (between blockset/endblockset) is being assigned to the variable "variable-name". I have found this quite useful with translations, or setting the title, where you out several variables into one sentence. Drop me an email if you will find this useful.

  • templatetag
  • block
  • set
Read More

Filter for adding quote marks

A template filter for adding curly quotes around a string. The filter understands enough HTML to put the quotes inside an initial paragraph begin and ending paragraph end, if they exist. Put the code inside a file in a templatetags subdir in your app, add a {% load myfile %} statement and you're ready to go with {{somebodysays|addquotes}}. Of course, beware the script kiddies, be careful with escaping.

  • templatetag
  • quotes
Read More

CleanCharField

I was about to start an online community but every time you allow people to post something as a comment you never know what they come up to, especially regarding profanities. So I come up with this idea, I put together some code from the old style form validators and the new newform style, plus some code to sanitize HTML from snippet number [169](http://www.djangosnippets.org/snippets/169/), and the final result is a CharField that only accept values without swear words, profanities, curses and bad html. Cheers.

  • validator
  • newforms
  • forms
  • html
  • sanitize
  • profanities
Read More
Author: DvD
  • -2
  • 0

Sumar dias habiles / Working days

Dada una fecha_origen, incrementa N dias a partir de esa fecha ignorando sábados y domingos. Increments a date by n days without counting weekends. Just working days.

  • date
  • python
  • dias_habiles
  • working_days
Read More

3110 snippets posted so far.