send_html_mail
There are many versions, this is the one I like. This is quite generic, can auto generate text version of the mail if required.
- html-mail
There are many versions, this is the one I like. This is quite generic, can auto generate text version of the mail if required.
This decorator does automatic key generation and simplifies caching. Can be used with any class, not just model subclasses. Also see [Stales Cache Decorator](http://www.djangosnippets.org/snippets/1131/).
Django documentation is lacking in giving an example for sending an email with attachment. Hopefully this code helps those who are trying to send email with attachments
See also [this thread](http://groups.google.com/group/django-developers/browse_thread/thread/ead13c9424b279a2). I'm an idiot; this snippet has now been updated to show how you do this without re-inventing the wheel.
This script is an adaptation from http://www.djangosnippets.org/snippets/678/ . Here, it doesnt use the cache middleware but relies on sessions. The script set a session cookie to identify the upload and track it to make it available for a progress bar like this one : http://www.djangosnippets.org/snippets/679/ . Note the progress bar cannot work with development server as it is single-threaded. Tested with apache/mod_python and mod_wsgi. any comments appreciated ;)
A simple helper function that clears the cache for a given URL, assuming no headers. Probably best used when wired up to a model's post_save event via signals. See [message to django-users mailing list](http://groups.google.com/group/django-users/msg/b077ec2e97697601) for background.
Here is a Django template tag that allows you to create complex variables specified in JSON format within a template. It enables you to do stuff like: {% var as person %} { "firstName": "John", "lastName": "Smith", "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": 10021 }, "phoneNumbers": [ "212 555-1234", "646 555-4567" ] } {% endvar %} <p>{{person.firstName}}, </br> {{person.address.postalCode}}, </br> {{person.phoneNumbers.1}} </p> This tag also enables me to do dynamic CSS using as follows: # urlpatters urlpatterns = patterns('', (r'^css/(?P<path>.*\.css)$', 'get_css'), ) #views def get_css(request, path): return render_to_response('css/%s' % path, {}, mimetype="text/css; charset=utf-8") # dynamic css within in /path/to/app/templates/css' {% load var %} {% var as col %} { "darkbg": "#999", "lightbg": "#666" } {% endvar %} {% var as dim %} { "thinmargin": "2em", "thickmargin": "10em" } {% endvar %} body { background: {{col.darkbg}}; margin: {{dim.thinmargin}}; }
There already is a [snippet on here](http://www.djangosnippets.org/snippets/433/) that shows how to use reCAPTCHA with Django, but being the lazya-- that I am, I wanted the form to: * display the captcha when as_table is used * handle captcha validation for me So RecaptchaForm does the trick. Just subclass from it and voila, your form will include reCAPTCHA validation. Other than that, you need to * be on Django trunk version (or use clean_data instead of cleaned_data for 0.96) * set RECAPTCHA_PRIVATE_KEY and RECAPTCHA_PUBLIC_KEY in your settings.py * make sure that the [captcha module](http://pypi.python.org/pypi/recaptcha-client) is available somewhere (i.e. that "import captcha" works)
Replaces something like this: cache_key = 'game1' the_game = cache.get(cache_key) if not the_game: the_game = Game.objects.get(id=1) cache.set(cache_key, the_game, 60*24*5) With this: the_game = get_cache_or_query('game1', Game, seconds_to_cache=60*24*5, id=1)
Displays an input with an add button. Clicking the add button creates a simple form - input with a submit button. Clicking the submit button will send the output to the server and return a value which is displayed.
**Paginator TemplateTag** TemplateTag to use the new Paginator class directly from a template. The paginate template tags take the following options: 1. list or queryset to paginate 2. number of pages 3. [optionaly] name of the Paginator.Page instance; prefixed by keyword 'as' 4. [optionaly] name of the http parameter used for paging; prefixed by keyword 'using' If you want to specify the parameter name with the keyword 'using' you must use the 'as' keyword as well. The default name of the paging variable is "page" and the paginator (the class that knows about all the pages is set in the context as "page_set". This follows the naming scheme of the ORM mapper for relational objects where "_set" is appended behind the variable name. Usage, put the following in your template: {% load paginate %} {% get_blog_posts blog_category as posts %} {% paginate posts 10 as page using page %} <ul> {% for post in page.object_list %} <li>{{ post.title }}</li> {% endfor %} </ul> <div> {% if page.has_previous %} <a href="?page={{ page.previous_page_number }}">previous</a> {% endif %} <i>{{ page.number }} of {{ page_set.num_pages }}</i> {% if page.has_next %} <a href="?page={{ page.next_page_number }}">next</a> {% endif %} </div> The templatetag requires the request object to be present in the template context. This means that you need 'django.core.context_processors.request' added to settings.TEMPLATE_CONTEXT_PROCESSORS list or otherwise make sure that the templatetag can access the request object. Comments are appreciated.
____
This exception middleware abstracts the functionality of the builtin exception handling mechanisms, but makes them extensible by inheritance. Just add it (or some subclass) to the top of your active middleware list. You can use this to [make your admin emails more informative](http://www.djangosnippets.org/snippets/631/) or [log errors to a file](http://www.djangosnippets.org/snippets/639/).
Importing data from other sources than SQL can be an annoyance. This script serves as a general tool for importing data from CSV files.
cache_smart template tag is a drop in replacement for default cache tag by Django but with the added bonus to be more resistant against dog-pile/stampeding effect. This snippet uses a extra cache entry to store a stale time so we don't have to pickle/unpickle to store this extra value. If this cache entry returns None, as in expired it will reset the stale timeout 30 seconds in the future so further calls will just return the old value while this request is regenerating the new value. **warning** Don't use both cache template tags!
3110 snippets posted so far.