Login

All snippets

Snippet List

Declaring django views like web.py views

I work a little with [web.py framework](http://webpy.org/) and I like a lot the view definition. For each view you define a class and in that class you can define two method, GET and POST. If the http request is a GET request the GET method will be called and if http request is a POST request the POST method is called. Then you can define common stuff in another method that could be called inside each method, and you have a class for each view.

  • views
  • class
  • web.py
Read More

Foreign Key list_filter wthout custom FilterSpec

This is some (probably) pretty dodgy code that allows for foreign keys in the admin's list_filter without using a custom FilterSpec. It overrides the django.db.models.options.Options get_field class to handle spanned relationships, i.e. list_filter=('house__room__town',) as seen in http://code.djangoproject.com/ticket/3400 I have only tested this with a double foreign key relationship('house__room'), and it worked. It hasn't been tested in production, and the troubling part of this code is probably to use of 'deepcopy'. I do add those copies back to the options instance, so there shouldn't be too much runaway copying.

  • foreign-key
  • filterspec
  • list-filter
  • 3400
Read More

Django Admin inline preview

Extend this ModelAdmin to get dynamic inline previews in the list admin also lives here: [http://github.com/broderboy/django-admin-preview](http://github.com/broderboy/django-admin-preview)

  • admin
  • jquery
Read More

View decorator to automate templates.

Place the above code in your view, and then you can use it to specify what template to use. Set elements of the context as a dictionary and that gets passed to the template as well. For example: In view.py: #################################### @with_template('friends/index.html') def friends(request, context, username): context['user'] = User.objects.get(username = username) in friends/index.html: {% extends "base.html" %} {% block content %} <h1>{{ user.username }}'s Friends</h1>

  • template
  • view
  • decorator
Read More

Flatpage Suggester Template tag for 404 templates

This template tag finds FlatPages with urls 'similar' to the given request_path. It takes the request_path from the page_not_found view (django.views.defaults), picks it apart, and attempts to match existing FlatPages that have a 'similar' URL. For example, if the URL that resulted in a 404 was: /foo/bar/baz/whatever/ This tag would look for FlatPages whose URL starts with the following: /foo/bar/baz/whatever/ /foo/bar/baz/ /foo/bar/ /foo/

  • template-tag
  • flatpage
  • 404
  • suggestion
Read More

Template Tag to protect the E-mail address

This is a Django Template Tag to protect the E-mail address you publish on your website against bots or spiders that index or harvest E-mail. It uses a substitution cipher with a different key for every page load. It basically produce a equivalent Javascript code for an email address. This code when executed by browser make it mailto link. **Usage** `{% encrypt_email user.email %}` **More Info ** [Template Tag to protect the E-mail address](http://www.nitinh.com/2010/02/django-template-tag-to-protect-the-e-mail-address/)

  • template
  • django
  • javascript
  • email
  • template-tag
  • obfuscate
  • bots
  • spider
Read More

SelfForeignKey to prevent hierarchical loops

When you have a model containing a field that is a foreign key back to the same model, you could find yourself with a hierarchy with an infinite loop: #Data modelling Back to the Future > grandfather > father > son > father > ... Using this field instead of the standard ForeignKey will ensure that no model instance is saved that has itself as an ancestor, breaking the relationship if it does. (Enhancements: I am sure one would want to better enhance this with appropriate error handling instead of silently disconnecting the relationship. And the relevant forms ought not show ancestors in the field's widget to reduce the chances of this happening in the first place.)

  • pre_save
  • recursion
  • loop
  • foreign
Read More

Remaining character count in django admin

Want to display the remaining characters on a text field in admin? (based off of maxlength or an override) also lives here: [http://github.com/broderboy/django-admin-remainingcharacters](http://github.com/broderboy/django-admin-remainingcharacters)

  • admin
  • jqeury
  • charactercount
Read More

Testrunner with testmodels

I miss the ability to use testmodels in app-tests. Using this testrunner you can include a test "app" in the app's tests module. Say if you have a module test_app.models within the tests module you could use it in the test like this: from django.test import TestCase from some_django_app.tests.test_app import models as testingmodels TEST_APPS = 'test_app' class ATestCase(TestCase): def test_no_url_model_signals(self): thing = testingmodels.ThingModel(name=u'a small thing') `TEST_APPS` can either be a string, a list or a tuple. This testrunner works with Django 1.1. It is based on the simple testrunner included with Django.

  • models
  • testrunner
Read More
Author: nfg
  • 0
  • 0

admin.py generator from a model

Generates admin.py file for a models.py file with the basic structure that could be edited. Usage: python gen_admin_py.py path/to/models.py creates a file admin.py in the same directory as models.py. import your models as you edit the admin.py

  • admin
Read More

Template range loop

This tag is meant to mimic the python use of range in a for-loop: 'for i in range(start, end, step)'. It is implemented like a loop and it takes both variable names from the context and constant integers as arguments. Syntax: {% range end as i %} {{ i }} {% endrange %} {% range start:end as i %} {{ i }} {% endrange %} {% range start:step:end as i %} {{ i }} {% endrange %}

  • template
  • range
  • loop
Read More
Author: nfg
  • 0
  • 0

Gallerific template for django-photologue

Template to make a galleriffic gallery for django photologue. Uses the settings from this demo http://www.twospy.com/galleriffic/example-2.html This is now the default template for the django-cms photologue plugin http://www.django-cms.org/en/extensions/cmsplugin-photologue/detail/

  • photologue
  • photo-gallery
  • django-cms
Read More

3109 snippets posted so far.