Login

All snippets

Snippet List

Add a print link to the admin to print individual record

This is a view that can be used to add a print button (link) in the admin change form for an individual record. Pretty simple to use. A nice enhancement to it is to be able to pull the model field name tie with the field value, something like making {{ object.as_dl }} available to the template. PS: The code is a modification from the django.views.generic.list_detail.object_detail

  • print
  • admin
  • record
Read More

Child aware model inheritance

Base models aren't aware of its inherited models, here is a quick solution to access child models from the base model.

  • model
  • inheritance
  • child
  • content-type
Read More
Author: rix
  • 2
  • 4

Test Integration for mako templates

This is a small addition to the mako template processing plugin for django that allows you to use the unit test framework with mako templates. To install, put the code into a file on your python path, and add the python path to your settings.py file. For example, if you install the code at /usr/lib/python2.5/site-packages/mako_django/test_integration.py you would add the following line to settings.py: TEST_RUNNER="mako_django.test_integration.run_mako_tests" This code will still call all of the normal test code, it just adds the mako template handler onto the list of things that are monitored.

  • mako
  • testing
  • test
  • mako-django
Read More

ShowOnly widget for froms

This form widget allows you to just display data in a rendered form, not giving the user the opportunity to change it. The initial data will just be carried through the form and showed to the user. In combination with snipped [1184](http://www.djangosnippets.org/snippets/1184/) you can make this even tamper safe. ;-)

  • newforms
  • forms
  • display
  • content
Read More

Tamper safe HiddenFields

This snippet prevents people from tampering with the data in hidden form fields. This is something you usually want unless you have some Javascript Vodoo going on on the browser side. For the people scratching their heads: This form class will dynamically create a clean function for every passed additional hidden field, which just returns the original value of the hidden field. So the data in the hidden field posted gets actually ignored when calling the (overwritten) clean_{field name} function. This class is just an example using the protected hidden field feature for all passed field variables, which is probably not what you want. You have to add the editable fields the end of the __init__ function in the class. Example: `self.fields['bestbeer'] = forms.CharField(max_length=23)`

  • newforms
  • field
  • hidden
  • froms
Read More

Hierarchical Flatpage Tag

Two template tags (I keep them in an app called "utils") handy for building menus out of flatpages. I only have two levels of hierarchy, and the frontpage is dynamic, so that's what it does. There is some flexibility, however, in that you can change the regex that defines a "root" page. You can pass it the flatpage object that is normally given to a flatpage template, or you can pass it a string (for any other page). Deliberatley does not create any markup -- flexibility is key, so it adds the list of root urls or child urls as a name in the current context. You get to pick the name. Please visit the [GitHub archive](http://wiki.github.com/0sn/nameremoved/flatpages) where i keep this up to date, there's a better explanation and you can see it in use.

  • tag
  • flatpage
  • hierarchy
Read More
Author: 0sn
  • 1
  • 6

Django Paypal

This is a snippet to use Paypal with python. This modifies Mike Atlas's geocept Paypal library. (Which was not working, for me at least) and adds recurring payments. I would try to write an article explaining how to use this and update the snippet.

  • paypal
  • payments
  • payment-processors
Read More

SuperChoices

Seeing [snippet 1178](http://www.djangosnippets.org/snippets/1178/) reminded me that I also had a go at writing a Choices class at some point. I'm content with the result, but I doubt xgettext will discover your translation strings, which will no doubt be inconvenient. Here it is anyway, in all its overly-complicated glory :-) The following demo was pulled from the function's docstring tests. >>> simple = Choices("one", "two", "three") >>> simple Choices(one=0, two=1, three=2) >>> tuple(simple) ((0, u'ein'), (1, u'zwei'), (2, u'drei')) >>> (0, _('one')) in simple True >>> simple.ONE 0 >>> hasattr(simple, 'FOUR') False Ordering just follows the order that positional arguments were given. Keyword arguments are ordered by their value at appear after positional arguments. >>> [ key for key, val in simple ] [0, 1, 2] >>> Choices(one=1, two=2, three=3) Choices(one=1, two=2, three=3) A Mix of keyword and non-keyword arguments >>> Choices("one", two=2, three=3) Choices(one=0, two=2, three=3) Automatically generated values (for "one" below) should not clash. >>> Choices("one", none=0, three=1, four=2) Choices(one=3, none=0, three=1, four=2) Here is an example of combined usage, using different object types. >>> combined = Choices(one=1, two="two", three=None, four=False) >>> len(combined) 4 >>> (1, _('one')) in combined True >>> ('two', _('two')) in combined True >>> (None, _('three')) in combined True >>> (False, _('four')) in combined True And here is an empty choices set. Not sure why you would want this.... >>> empty = Choices() >>> empty Choices()

  • models
  • choices
Read More

Login Required Middleware

Sometimes it's a real pain to use the `@login_required` decorator all over the views of a complicated site. This middleware requires login on every page by default and supports a list of regular expression to figure out the exceptions. This way you don't have to worry about forgetting to decorate a view. This snippet requires `LOGIN_URL` to be set in settings.py, and optionally allows you fill out `LOGIN_EXEMPT_URLS`, a tuple of regular expressions (similar to urls.py) that lists your exceptions. Example: `` LOGIN_EXEMPT_URLS = ( r'^about\.html$', r'^legal/', # allow the entire /legal/* subsection ) ``

  • middleware
  • authentication
Read More

Choices class

Yet another class to simplify field choices creation. Keeps order, allows i18n. Before: ONLINE = 0 OFFLINE = 1 STATES = ( (ONLINE, _('online')), (OFFLINE, _('offline')) ) state = models.IntegerField(choices=STATES, default=OFFLINE) After: STATES = Choices( ('ONLINE', _('online')), ('OFFLINE', _('offline')) ) state = models.IntegerField(choices=STATES, default=STATES.OFFLINE)

  • models
  • choices
Read More
Author: dc
  • 7
  • 8

Address model

This is standart address model. Must match US / GB / French address and other... Specifics locales fields are notice by comments.

  • model
  • address
Read More

truncatehtml_at_word

Much stolen from base `truncate_html_words`. The difference is that this filter takes a number of characters as its argument and truncates to the nearest word boundary less than that count, rather than specifying a number of words.

  • filter
  • html
Read More

Url persistance of GET variables

I'm using Django 0.96 for a project, and the url tag does not have all the capabilities I need. I want a way to persist all or some of the GET parameters between requests. I also want a way to add to the current url a list of extra parameters, or nullify certain parameters. In this snippet I am defining 2 tags ... link_persist and link_add. It only works with Django 0.96, as the API changed ... but maybe a good soul can fix it for 1.0, as I haven't had any time available. A tip for usage: if you specify a parameter as being the empty string, or None, the parameter will be removed from the link. When you specify a parameter already available in GET, it will replace it.

  • get
  • url
  • link
  • persistance
Read More

Securely Signed S3 Links With Expiration

I couldn't find a Python implementation of this, so I threw this class together real quick. This will let you share "private" files on S3 via a signed request. It will also have an expiration on the link, so it is only valid until a certain time period. Example Usage: s3 = SecureS3('AWS_ACCESS_KEY', 'AWS_SECRET_ACCESS_KEY') s3.get_auth_link('your_bucket', 'your_file') That would return your secure link. eg, http://your_bucket.s3.amazonaws.com/your_file?AWSAccessKeyId=AWS_ACCESS_KEY&Expires=1226198694&Signature=IC5ifWgiuOZ1IcWXRltHoETYP1A%3D

  • s3
  • secure
Read More
Author: pjs
  • 1
  • 4

3110 snippets posted so far.