Login

Top-rated snippets

Snippet List

Use class name in templates

For use in templates: {% if object|classname:"entry" %} ... class="{{ object|classname }}" ... I couldn't find simpler solution for checking weather specific object belongs to specific class name, or just to output object's class name.

  • filter
  • classname
Read More

Random Quotes

Display a random quote. Just add some quotes to the app (you may want to add the administration interface options) and then load the template tag: `{% load random_quote %}` and then call the tag to display a random quote from the app `{% random_quote %}` feel free to improve it/whatever.. :)

  • templatetag
  • random
  • quotes
Read More

Sending html emails with images using Django templates

I have not extensively test this yet. But working for templates with embedded images. If you want to use Django template system use `msg` and optionally `textmsg` as template context (dict) and define `template` and optionally `texttemplate` variables. Otherwise msg and textmsg variables are used as html and text message sources. If you want to use images in html message, define physical paths and ids in tuples. (image paths are relative to MEDIA_ROOT) example: images=(('email_images/logo.gif','img1'),('email_images/footer.gif','img2')) use them in html like this: `<img src="cid:img1"><br><img src="cid:img2">` stripogram and feedparser modules are used for extract plain text from html message source. If you are going to define text partition explicitly, than you can comment out line 10,11 and 48.

  • template
  • email
  • mail
  • html
Read More

script for run a django task

when running a console script to access django or your models, you need to setup django environment properly and that is very annoying if you have quite a few scripts. this script can be used as a wrapper just like the manage.py. put it in your project root and type this: python run.py myproj.appname.filename.functionname [arguments] and define your function as def functionname(argv[]): ...

  • task
  • console
  • script
Read More

Changing the look of newforms as_table with a custom BaseForm

I wanted to mark rows with an erroneous input with 'class="error"' and move the errorlist below the input tag so I wrote a NormalRowFormatter which behaves like a format string by overwriting \_\_mod\_\_. Examples: class MyForm(PrettyBaseForm, forms.Form): # add your fields here SomethingForm = form_for_model(Something, form=PrettyBaseForm)

  • newforms
  • error
  • baseform
  • as_table
  • pretty
  • prettybaseform
  • _html_output
  • style
Read More

Simple Contact Form

Simple contact form, using a javascript form checker which you can use any you want to you can just modify the form in the way you want to. The form checker I use I include in the 'base.html' page before the </head> section end. You can use it freely. http://media.xorl.de/form.js . A lot of this code is comprised of other peoples forms that didn't suit the simple purpose of a *really* basic contact form which I wanted, so I rebuilt it up from bits and pieces to make a simple basic form.

  • python
  • form
  • contact
Read More

run "silent" dev server

This will hide the scrolling output from the development server so you can ssh and run the server in peace, for those of us with only one SSH session active developing on a remote server.

  • bash
  • runserver
  • daemon
Read More

Tablify templatetag

`{% tablify questions name response 8 %}` Will generate a table structure of a list, ensuring that a set number of columns is not exceeded. It takes 4 arguments: *Context Variable *Cell Header Attribute *Cell Data Attribute *Max Number of Columns The code will loop through the given variable, creating th and td elements for each item. It will create a new tr when the max number of columns is exceeded.

  • templatetag
  • table
Read More

Last pages the user visited

This middleware remembers the last URLs that the user visited on your Django-site and saves them into the `request.session`. The fields are `currently_visiting` for the URL that is opened by the user and `last_visited` which is the URL before. Most of the time, you'll need only `last_visited`, as `currently_visiting` is just an implementation detail. For what is this good for? Imagine, you have to implement something like JavaScripts `history.back()` or `history.forward(-1)` but without JavaScript. Things start to get difficult when using JavaScript is not possible, for whatever reason. This snippet was created because I needed to redirect the user to the page he's been watching before clicking on the "translate" link. One other alternative would be adding the URL the user was visiting as a GET field to the "translate" link, but I hoped to find a possibility to avoid GET. This snippet works quite well as a proof of concept, the only known wart is when the user uses tabs or multible windows on the site, things get messed up. This cannot be solved, it's a restriction imposed by the design of HTTP.

  • middleware
  • session
  • user
Read More

Getting the global error of a form

You may want to access to "global" errors in your template, typically when you have a custom `clean()` method which checks several fields. `{{ form.errors }}` gives an < ul >, which is often not what you want, and `{{ form.errors|join:", " }}` iterates in the keys of `errors` (which are the names of fields, uninteresting). The global `errors`'s key is "__all__", and Django doesn't allow us to do `{{ form.errors.__all__ }}`. This method returns an array, like a classic `{{ form.field.errors }}`, and can be used with a join : `{{ form.get_global_errors|join:", " }}`.

  • newforms
  • forms
Read More

custom sql without table names

Keeps database table names out of custom SQL code, but still allows for correct parameter passing in the execute function. (psycopg doesn't substitute table or field names, only data, in the execute function)

  • sql
  • postgres
Read More

custom css classes for newforms

This isn't really any trick, its just that I didn't find any documentation of this or any references in searching. There are a few changes proposed for css classes which might make this redundant, but for now this works! If you want to add attributes for any fields, just include them in the widget constructor. They get written out in key value pairs when the input field is being created. e.g. in the example above, this will come out like: ` '<input type="text" name="start_date" id="id_start_date" class="vDateField required" size="10"/>' `

  • newforms
  • css
Read More

3110 snippets posted so far.