Login

Top-rated snippets

Snippet List

WithTag Tag

Set a context variable with the returned value by any templatetag. Useful for example in order to use url templatetag inside blocktrans: ` {% withtag url my_app.views.my_view as my_view_url %}` ` {% blocktrans %}` ` Click <a href="{{ my_view_url }}">here</a>` ` {% endblocktrans %}` ` {% endwithtab %}` Or with include templatetag: ` {% withtag include "js_template.js" as js_template %}` ` {{ js_template }}` ` {% endwithtab %}` It works properly with your own custom templatetags.

  • templatetag
  • with
  • withtag
  • with_tag
Read More

Modify query string on a url

Modify a query string on a url. The comments in the code should explain sufficiently. String_to_dict, and string_to_list are also useful for templatetags that require variable arguments.

  • url
  • query
Read More

Model manager with row caching

RowCacheManager is a model manager that will try to fetch any 'get' (i.e., single-row) requests from the cache. ModelWithCaching is an abstract base model that does some extra work that you'll probably want if you're using the RowCacheManager. So to use this code, you just need to do two things: First, set objects=RowCacheManager() in your model definition, then inherit from ModelWithCaching if you want the invalidation for free. If you are unusually brave, use the metaclass for ModelWithCaching and you won't even need the "objects=RowCacheManager()" line.

  • get
  • model
  • manager
  • model-inheritance
  • caching
Read More

Scan uploaded file for viruses with clamav

A clean_&lt;fieldname&gt;() method in a form subclass as described [here](http://www.djangoproject.com/documentation/newforms/#custom-form-and-field-validation). Scans the field named *file* for viruses. My version of python-clamav does not support scanning of buffers. That is why I go through the hassle of saving the file to a temporary one.

  • newforms
  • clamav
  • virus
  • virus-scan
Read More

Gravatar Images

This is a simple templatetag for including [Gravatars](http://www.gravatar.com/) on your Django site. Usage is `{% gravatar some_user %}` or `{% gravatar some_user 40 %}`

  • avatar
  • gravatar
Read More

DisplayModelForm

Subclass of the ModelForm which allows to make fields 'display_only'. This means no formfield will be displayed, but a suitable representation. You can make all fields display_only or just a few (or you can use the form as a normal modelform). There are also some extra's to easily set attrs on fields or set help_texts on widgets when using this form. Why ? I made my own set of generic crud views based on newforms, but added a 'display' view to simply display objects, in the same table layout as the editing is done, but without the fields. I wanted to avoid having to redefine my forms twice and I wanted to reuse some generic templates as much as possible. Obviously this is not good for performance, but I use it for an intranet app with a lot of objects, but not that big a load. Their is definitely still a lot of room for improvement, and maybe all this could have been done much easier. How to use? See the docstring.

  • modelform
Read More

Block IP addresses

I needed a quick and dirty way to block a user from my site. Just include this middleware class under the 'MIDDLEWARE_CLASSES' variable in your settings.py file. Also include the variable BLOCKED_IPS = ('123.123.123.123',) variable, where the value is a tuple of IP addresses you want blocked from your site.

  • middleware
  • ip-address
Read More

Google Charts Templatetags (HTML)

Example usage of the GChartWrapper.charts module for creating dynamic charts with templatetags. It is an easy method of creating dynamic GoogleCharts from the [GoogleChartAPI](http://code.google.com/apis/chart/). The project is located at [google-chartwrapper](http://code.google.com/p/google-chartwrapper/). To get the most recent version: `svn checkout http://google-chartwrapper.googlecode.com/svn/trunk/` and run `python setup.py` in the downloaded trunk directory. There is an included django project there with the [ChartsExamples](http://code.google.com/p/google-chartwrapper/wiki/ChartExamples) all worked out in django templates

  • templatetags
  • google
  • chart
Read More

A dict template tag

When you need to include a specific javascript file/code snippet in your page, it's always better to do it at the bottom of your page to avoid to block the rendering too soon. This tag provide you a nice way to include and launch only what is needed: Example in an included template that need to display google maps: {% dict js_file google_api %} <script src="http://www.google.com/jsapi?key={{ MAPS_API_KEY }}" type="text/javascript" charset="utf-8"></script> <script src="{{MEDIA_URL}}js/map.display.js" type="text/javascript">...</script> {% enddict %} {% dict js_code link_map %} $('.show-map').click(function() { ... }); $('.hide-map').click(function() { ... }); {% enddict %} Finaly you just have to add this to the very bottom of your base.html file: .... </body> {% for k,v in js_file.items %} {{v}} {% endfor %} <script type="text/javascript"> /* <![CDATA[ */ {% for k,v in js_code.items %} {{v}} {% endfor %} /* ]]> */ </script> </html>

  • template
  • javascript
  • dict
  • stack
Read More

Render to file

As a demo, I was asked to write a `render_to_file()` function to load a template and render it to a file. Turns out it's amazingly easy, and I think it's a neat trick to have in your bag of tools.

  • template
  • render
  • file
Read More

Ajax progress bar

This snippet is an example of an ajax progress bar (using jquery) that you might use in conjunction with <http://www.djangosnippets.org/snippets/678/>. 1. Generates a uuid and adds X-Progress-ID to the forms action url. 2. Adds the progress bar to the page. (you'll have to add some css styling for this) 3. Makes period ajax requests to update the progress bar.

  • ajax
  • javascript
  • upload
  • file
  • progress
Read More

Newforms field for decimals with a comma

This might be handy in countries where decimals are entered with a comma separating the decimal places from the integer part (for instance in Germany). It lets user enter and displays all decimals with a comma separator. I ran into this problem and couldn't find a clean internationalized way of doing it... but newforms makes it so easy to roll your own. Hope it helps someone.

  • newforms
  • locale
  • comma
  • decimal
Read More

chdjango

This is a convenient script for those working with different branches of Django. Place all of your branches in `~/django` (e.g., `~/django/newforms-admin`, `~/django/trunk`) and you're ready to quickly change between them. For example: `chdjango.py trunk`.

  • django
  • path
  • change
  • chdjango
  • branches
  • pth
  • site-packages
Read More

FixedEmailMessage

Django EmailMessage class has no cc support and has bug in bcc support. Core developers won't add cc support (see ticket http://code.djangoproject.com/ticket/5790), and I don't want to wait half a year until they will realize they have a flaw that bcc recipients are sent to regular "to:" recipients and fix it. So, if you want to use EmailMessage class right now, you'd better use FixedEmailMessage class. Class contract is the same, except for a new cc constructor argument.

  • email
  • smtp
  • mail
  • message
  • emailmessage
  • cc
Read More

3110 snippets posted so far.