Login

All snippets

Snippet List

Google Map Field and Widget

There is a sample in the code. You can use this snippet to allow your forms to easily edit object locations. Take care about what you should add to your templates in order to make it work.

  • newforms
  • field
  • widget
  • gmap
  • map
  • google-map
Read More

Admin change language

This snippet adds a select language drop down menu in the admin bar (just after Documentation link). For this approach it's necessary to copy from admin templates the admin/base.html to the project templates (keeping the admin directory), changing just line 25 ad shown. Then it's necessary to create specified admin/change_language.html template.

  • admin
  • language
  • change
Read More

Get child model

With the child_object function defined in the parent model, you can get it's child object.

  • model-inheritance
Read More

dynamic model graph

This view assumes you have downloaded [modelviz.py](http://django-command-extensions.googlecode.com/svn/trunk/extensions/management/modelviz.py) and placed it in a python module called utils within my_project. You also must have graphviz installed and a subprocess-capable python. From there you can feed it a URL query list of the options you want to pass to generate_dot, and it will dynamically draw png or svg images of your model relationships right in the browser. All it wants is a nice form template for graphically selecting models. Most of this code and the main idea thereof was shamelessly plagiarized from [someone else](http://gundy.org/). Examples: `http://localhost:8000/model_graph/?image_type=svg&app_labels=my_app&include_models=Polls&include_models=Choices` `http://localhost:8000/model_graph/?image_type=png&all_applications&disable_fields&group_models`

  • models
  • graph
  • png
  • database
  • graphviz
  • modelviz
  • visualization
  • svg
Read More

AlphabeticFilterSpec

**WARNING: a better version of this snippet you can see at [http://www.djangosnippets.org/snippets/1051/](http://www.djangosnippets.org/snippets/1051/)** This filter spec is util only for who uses newforms-admin branch. To use this, you need to extend the class ModelAdmin for your model class, like the MyClassAdmin class in the code, with attention to the following lines: # Appends the filter cl.filter_specs.insert(0, AlphabeticFilterSpec(cl.lookup_opts.get_field('name'),request,cl.params,self.model,self))

  • filter
  • admin
  • alphabetic
Read More

DropDownMultiple widget

Observation: depends on jQuery to works! This widget works like other multiple select widgets, but it shows a drop down field for each choice user does, and aways let a blank choice at the end where the user can choose a new, etc. Example using it: class MyForm(forms.ModelForm): categories = forms.Field(widget=DropDownMultiple) def __init__(self, *args, **kwargs): self.base_fields['categories'].widget.choices = Category.objects.values_list('id', 'name') super(MyForm, self).__init__(*args, **kwargs)

  • newforms
  • multiple
  • forms
  • jquery
  • select
  • widget
Read More

Nested set abstraction for hierarchical data

The nested set abstraction is a very nice way to store hierarchical data, for example places, in a database. It provides an easy way to say that Melbourne is within Victoria, which is within Australia, etc.</p> The calls to addChild() are expensive, so this model is slow to build, and bad if you do frequent updates. If you're building a read-only model though, it's much faster than ForeignKey('self') for determining ancestor/descendent relationships.

  • tree
  • hierarchy
  • nested-set
Read More

Generating Taconite command documents

Hello, This is a port of a php class used to generate XML taconite command documents, useful for (very) easy and powerful ajaxy stuff, if you don't know what that is just check it there in french : http://www.desfrenes.com/playground/taconite/ or there in english : http://www.malsup.com/jquery/taconite/. Basically what it does is generate an XML document that is later processed by a javascript plugin which executes a serie of DOM modifications. About the code, I'm a Django beginner as well as a Python beginner so kind advices are welcome. Cheers.

  • ajax
  • django
  • javascript
  • python
  • jquery
  • taconite
  • minidom
  • dom
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

Variable inspect filter

This module has two template filters allowing you to dump any template variable. Special handling for object instances. Pretty output. Usage: {% load dumper %} ... <pre>{{ var|rawdump }}</pre> or {% load dumper %} ... {{ var2|dump }} How to install: As usual, put into `<your-proj>/<any-app>/templatetags/dumper.py`.

  • template
  • filter
  • dump
  • variable
  • inspect
Read More

Protect anti robots template tag

This code works like that in the Google Groups you see and when try to see an e-mail and it is like this "[email protected]" with a link to write a captcha code and see the true value. You can use it for anything you want: links, blocks of texts, block of HTML, etc. To use in your template, place the a code like this (remember to load the template tags file with a {% load file_name %} before): {% protectantirobots %} <a href="mailto: {{ office.email }}">{{ office.email }}</a> {% endprotectantirobots %} You can also use **django-plus** application to do this: [http://code.google.com/p/django-plus/](http://code.google.com/p/django-plus/)

  • captcha
  • security
  • protect
  • anti
  • robots
  • safe
Read More

order_by template filter

put this code into a file in a folder "templatetags" inside some application and use it like this: {% load your_file_name %} {% for item in your_list|order_by:"field1,-field2,other_class__field_name"

  • template
  • filter
  • orderby
Read More

hide_email

This templatetag obsfucate mailto link and hide it while not happen onmouseover event. Usage: {% hide_email object.author object.author.email %} Output: <a href="mailto:[email protected]" onmouseover="var a=String.fromCharCode(79+35,14+103,66+41,30+71,49+49,16+81); var b=String.fromCharCode(14+50,9+105,61+56,81+26,92+9,2+96,20+77,13+33,75+24,32+79,31+78); this.href=['mail','to:',a,b].join('');">rukeba</a> Based on [Email Obsfucator](http://www.djangosnippets.org/snippets/536/) and [forums at Yandex.Market](http://market.yandex.ru/forums/). Example at [my guitar blog](http://rukeba.com/ra/2008/01/15/oasis-wonderwall/)

  • templatetag
  • email
  • antispam
Read More

Integrate Tenjin using a decorator

This provides basic [Tenjin](http://www.kuwata-lab.com/tenjin/) integration, using a decorator. Tenjin is a blazingly fast templating system, the fastest pure-python system available. For usage and configuration, see the example in the code. I modeled this after Robert Thomson's code for Mako integration.

  • decorator
  • tenjin
  • template-engine
Read More

get_cache_or_query - Shortcut to common cache signature

Replaces something like this: cache_key = 'game1' the_game = cache.get(cache_key) if not the_game: the_game = Game.objects.get(id=1) cache.set(cache_key, the_game, 60*24*5) With this: the_game = get_cache_or_query('game1', Game, seconds_to_cache=60*24*5, id=1)

  • cache
  • optimization
  • get_cache_or_object
Read More

3109 snippets posted so far.