Login

Snippets by btaylordesign

Snippet List

Math Captcha Field and Widget

This math captcha field and widget was inspired by Justin Quick's django-math-captcha, but I wanted to make it into one form field and not have anything in settings.py. I removed the division and modulo operators to avoid people having to input fractions, and just randomly select the operator. It leverages Django's built-in MultiValueField to set up the hidden hashed answer to compare the user's input to instead of rendering strings for the fields like django-math-captcha does. Unit tests soon to follow, but this is used in production at: http://btaylorweb.com/. Enjoy!

  • simple
  • captcha
  • field
  • widget
  • math
  • multiwidget
  • multifield
Read More

Dynamic Regroup Template Tag

Django's built-in {% regroup %} template tag is great, but sometimes, you need to pass in the attribute you want to group on instead of declaring the attribute when you define the tag. {% dynamic_regroup %} is identical in function to {% regroup %}, except that it will attempt to resolve a context variable for the attribute you want to group by. {% dynamic regroup %} is also backward compatible, so you can also hand in the attribute literal and it will work as expected. See the end of the code for an example of usage.

  • templatetag
  • dynamic
  • regroup
Read More

Smart Spaceless

This 'smart_spaceless' template tag is a replacement for Django's built-in 'spaceless'. If settings.DEBUG = True, spaces will not be removed to make debugging your template code easier. When DEBUG = False, spaces will be removed. Happy coding!

  • template
  • spaceless
  • template tag
  • remove spaces
  • template tags
Read More

AjaxForm Base Classes

These base form classes add a method to return error messages as HTML encoded as JSON from Django, optionally passing in an argument to strip tags out. The method can be called in your view after checking that your form is valid. There is a ModelForm and Form class to use depending on your needs. The sample jQuery function will take the errors returned as json, loop over the errors and insert the error after each field. If you're using a form prefix, you'll need to add a hidden field to hold the value for the prefix. The `__all__` error(s), which are not bound to a field are appended to the end of the form, which you could easily reposition. Happy coding!

  • ajax
  • json
  • form
Read More

Feet and Inches FormField/Widget

Feet and Inches Widget/Field allows users to enter a value in feet, inches and fractional inches and have that value stored as a decimal in your database of choice. I have this code broken out into separate files, but you can always combine to suit your needs. I have designated file delineation with a single line comment and the name of the file as referenced in the import statements. The INCH_DECIMALS and INCH_FRACTION constants may be modified to allow smaller increments. My needs for this implementation only required accuracy to 1/16. Hope this helps someone learn how to use the MultiWidget and MultiValueFields! Happy Coding.

  • widget
  • multiwidget
  • multifield
  • feet
  • inches
Read More

btaylordesign has posted 6 snippets.