Posts

Django Dynamic Formsets with Jquery

If you have an existing project, it's quite easy to add client-side support for adding and removing forms. I'll assume you've already created your formset. You can create formsets using any of the provided methods: both regular formsets (created with the ``formset_factory``) and inline formsets (created with the ``inlineformset_factory``) are supported. 1. First, copy ``jquery.formset.js`` to your ``MEDIA_ROOT``; don't    forget to include the jQuery library too! 2. Include a reference to the script in your template; again, remember    to reference the jQuery library, before including the script. 3. Render the formset as you would normally -- I usually use a table    but you can use DIVs, Ps or whatever you desire. Let's use the    example markup below:: <form id="myForm" method="post" action="">            <table border="0" cellpadding="0" cellspacing="0">  ...

Coffee Script : Overview

Image
     CoffeeScript is a little language that compiles into JavaScript . Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.     The golden rule of CoffeeScript is: "It's just JavaScript". The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa). The compiled output is readable and pretty-printed, passes through JavaScript Lint without warnings, will work in every JavaScript runtime, and tends to run as fast or faster than the equivalent handwritten JavaScript.   Latest Version: 1.6.2                sudo npm install -g coffee-script Overview CoffeeScript on the left, compiled JavaScript output on the right.   Installation...

Python Tutorial 8 : Python Utilities

In this section, we look at a few of Python's many standard utility modules to solve common problems. File System -- os, os.path, shutil The *os* and *os.path* modules include many functions to interact with the file system. The *shutil* module can copy files. os module docs filenames = os.listdir(dir) -- list of filenames in that directory path (not including . and ..). The filenames are just the names in the directory, not their absolute paths. os.path.join(dir, filename) -- given a filename from the above list, use this to put the dir and filename together to make a path os.path.abspath(path) -- given a path, return an absolute form, e.g. /home/nick/foo/bar.html os.path.dirname(path), os.path.basename(path) -- given dir/foo/bar.html, return the dirname "dir/foo" and basename "bar.html" os.path.exists(path) -- true if it exists os.mkdir(dir_path) -- makes one dir, os.makedirs(dir_path) makes all the needed ...

Python Tutorial 7 : Python Regular Expressions

Regular expressions are a powerful language for matching text patterns. This page gives a basic introduction to regular expressions themselves sufficient for our Python exercises and shows how regular expressions work in Python. The Python "re" module provides regular expression support. In Python a regular expression search is typically written as:   match = re . search ( pat , str )   The re.search() method takes a regular expression pattern and a string and searches for that pattern within the string. If the search is successful, search() returns a match object or None otherwise. Therefore, the search is usually immediately followed by an if-statement to test if the search succeeded, as shown in the following example which searches for the pattern 'word:' followed by a 3 letter word (details below): str = 'an example word:cat!!' match = re . search ( r 'word:\w\w\w' , str ) # If-statement after search() tests if it suc...

Python Tutorial 6 : Python Dict and File

Image
Dict Hash Table Python's efficient key/value hash table structure is called a "dict". The contents of a dict can be written as a series of key:value pairs within braces { }, e.g. dict = {key1:value1, key2:value2, ... }. The "empty dict" is just an empty pair of curly braces {}. Looking up or setting a value in a dict uses square brackets, e.g. dict['foo'] looks up the value under the key 'foo'. Strings, numbers, and tuples work as keys, and any type can be a value. Other types may or may not work correctly as keys (strings and tuples work cleanly since they are immutable). Looking up a value which is not in the dict throws a KeyError -- use "in" to check if the key is in the dict, or use dict.get(key) which returns the value or None if the key is not present (or get(key, not-found) allows you to specify what value to return in the not-found case).   ## Can build up a dict by starting with the the empty dict {}   ##...