Skip to main content

Posts

Showing posts with the label snippet

Django: adding additional Admin static and writing JS snippets for Django Admin

It often gets that you need it because you invent something that is meant to be displayed for admin. E.g. some nasty form field or something. Here is my recipe to add some additional Django static to your admin. Rather than writing a widget , that is more proper way here... I was using a widget already (from another package). So I could not use it properly, at least without a ton of overrides and complexity it leads to. SO I've decided to add the JS functionality to override a lack of backend. First of all we should add the javascript/css like so: # admin.py class MyModelAdmin (admin.ModelAdmin): # admin additions class Media : css = { "all" : ( "css/my_style.css" ,) } js = ( "js/my_script.js" ,) This add should list your files, loaded in the admin . So you can access them and see in the Django admin frontend itself. Like so: Now that your script and style are loaded you could jus...

Django 1.5 migrating to new url tag syntax

Django 1.5 changed. The url tag syntax from {% url  my_page %} into {% url "my_page" %} . Now all my templates should be changed accordingly to migrate to 1.5. You could do it with executing this command in your bash shell. Assuming you are in the project folder. Assuming you have version control, or do know how to backup your work ;). Some people claim it is unsafe. But you may want to make it safer with adding '{%' to the beginning of the string. To make sure only templates arre changed. Or either execute this only in your templates folder to be sure. $ find . - type f - print0 | xargs - 0 sed - i 's/ url \([^" >][^ >]*\)/ url "\1"/g' And one person did a django snippet for this operation. You may also find it useful.  http://djangosnippets.org/snippets/2905/