Login

Most bookmarked snippets

Snippet List

Gravatar support in model save override

Overridden save() method that adds Gravatar support for a user with a profile photo field (and presumably an email field). Checks to see if user has provided a photo. If not, then query Gravatar for a possible photo. Finally, if Gravatar does not have an appropriate photo for this user, then use whatever default photo is available (in this case, 'users/photos/default_profile_photo.png'... change as necessary).

  • model
  • save
  • override
Read More

Template {% macro %} support, with context rendering

Reuse blocks of template code and content as macros. This is a small extension of https://gist.github.com/skyl/1715202 (which was based on http://djangosnippets.org/snippets/363/) to support rendering macro output into context variables. See comments for details.

  • template
  • tag
  • macro
Read More

Add querystring parameters to path (template tag)

`<h3>Page: {{ page.number }} of {{ page.paginator.num_pages }}</h3> {% if page.has_previous or page.has_next %} <div> {% if page.has_previous %} <a href="{% url_add_query page=page.previous_page_number %}">{% endif %}&laquo; Previous {% if page.has_previous %}</a>{% endif %} | {% if page.has_next %} <a href="{% url_add_query page=page.next_page_number %}">{% endif %} Next &raquo;{% if page.has_next %}</a>{% endif %} </div> {% endif %}`

  • template
  • templatetag
  • pagination
  • request
  • querystring
  • query-string
Read More

create_model_instances management command

This management command is run like this: `./manage.py -a someapp filename.cfg` it looks in `someapp`'s directory for a file called `/config/filename.cfg` with the format explained in the help text, and creates the model instances described in the config file. It uses the configobj module. this would be an example config file: [project.Profile] [[fields]] receive_notifications = False [[children]] [[[auth.User]]] [[[[fields]]]] username = AnnonymousUser password = ! # set unusable password. There's no way yet to hash and set a given password email = [email protected]

  • model
  • instance
  • object-creation
  • config-file
Read More

MongoDB data load

This snippet loads data from JSON files into a MongoDB database. The code is related with the other snippet [MongoDB data dump](http://djangosnippets.org/snippets/2872/). To get it working, just create a ``MONGODB_NAME`` variable in settings, holding the name of your Mongo database. This can be edited to fit more your needs. The snippet requires ``Pymongo``, since it uses its bson module and the ``MongoClient``.

  • loaddata
  • fixtures
  • mongodb
Read More

MongoDB data dump

This Django management command just dumps data from a given MongoDB collection into a JSON file. To get it working, just create a ``MONGODB_NAME`` variable in settings, holding the name of your Mongo database. This can be edited to fit more your needs. The snippet requires Pymongo, since it uses its ``bson`` module and the ``MongoClient``.

  • dump
  • fixtures
  • mongodb
Read More

Comma Seprated Character Field to store Geographic Coordinates

in models, import GeoCoordinateField: class Place(models.Model): geocoordn = GeoCoordinateField(verbose_name="geocordfield", null=True, blank = True) >>>place = Place.objects.geocoordn #gives you a Geocoordinate object >>>place.geocoordn.latitude, place.geocoordn.longitude #gives latitude and longitude of place

  • Django
  • Python
Read More

Recursive template tag for Django with arguments

This template tag was inspired by http://djangosnippets.org/snippets/592/, but with improvements in the syntax it is used with to be more function-like, and avoiding the problem of conditional recursion as noted in http://djangosnippets.org/comments/cr/15/592/#c2472. The syntax for using it can be seen in the docstring of the defrecurse() function. Additionally, a magic "level" variable is used to indicate the level of recursion, starting with 0 for the outermost level. This should theoretically allow for nested recursion, but the inner {% recurse %} call cannot call the outer {% defrecurse %} block.

  • template
  • templatetag
  • recursion
Read More

Function cache decorator

This is caching mechanism augmented to help address a number of common pitfalls in caching: cache stampeding, simultaneous expiry, cache invalidation, and the storing of None as a legitimate value. No doubt the automatic key generation could stand to be simplified, but it works.

  • cache
Read More

Git Revision Template Tag

This displays the short commit id from the current HEAD. Add this to your admin/base_site.html or any other template. {% block userlinks %} /&nbsp;HEAD: <span style="color:yellow">{% git_short_version %}</span> /&nbsp; {{ block.super }} {% endblock %}

  • git
Read More

3110 snippets posted so far.