Login

Snippets by lawgon

Snippet List

language switcher in admin

you have a multilingual site and need to change languages in admin. Previously this was easy in the site itself and more difficult in admin. Now it is dead easy. Set up your languages in settings.py. Make a directory called 'admin' in your templates directory, copy ~/django/contrib/admin/templates/base.html to that directory. Add the following code (below breadcrumbs is a good place) this will give you a switcher for all installed languages. You would need to refresh the browser on changing the language.

  • admin
  • i18n
Read More

django on tornado

there have been many posts on running django on tornado with static media served by nginx. But for dumb people like me, the whole thing needs to be spelt out. So here is how I succeeded in serving django from a virtual host using nginx and tornado. The key thing to note is that 'root' refers to the **parent** directory of the root and not the full path. Also remember to put in ':' as a line end. Procedure - start the tornado server with the python script on localhost:8888, start nginx. Relax and enjoy your django at the speed of light. Nginx can be got by apt-get or yum, but you need the latest git clone of Tornado - the default tarball does not support django. btw, this install is for FC11 on my laptop - I have done it in production on lenny.

  • nginx
  • tornado
Read More

automating twitter

Assume you have a model called Delegate and you want to tweet whenever a new Delegate registers, the code above will do this. You need to install python-twitter.

  • twitter
Read More

simple jquery example

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.

  • ajax
  • jquery
Read More

ManyToMany field with newforms

In editing a ManyToMany field in a form, it is necessary to clear the entries in the bridge table before adding new entries. So first save the new entries, remove the old entries in the multiple choice field and then add the new entries. It is also necessary to add the commit_on_success decorator to make sure that the whole process is in one transaction.

  • newforms
  • multiplechoicefield
  • manytomany
Read More

multiple model form with image

You have two models, one of which also has an image field. You want to get the data into a form and edit it. This also requires showing the url of the image, if there is one, on the form. This is a complete working example. No big deal, but for newbies, a complete working example could be valuable.

  • newforms
  • imagefield
Read More

Changing field type in production

Assume a model called 'Child' with a field called 'schoolstd' whic h is of integer type and not null. You need to change it to char type and not null and the same time preserve the data. The above snippet does this in postgresql.

  • schema_evolution
Read More

lawgon has posted 7 snippets.