Skip to main content

Posts

Showing posts with the label templates

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/

Templates. New in Django 1.4

Continuing those series of tasty features in Django 1.4. It's templates and newly added templatetags this time. Many things improved in template logic now. Lets move on point by point... Tag  {% elif %} For most of us it is almost killer feature of new Django 1.4. You will have simpler logic in your templates, if you have advanced logic there, of course. ;) Key-value arguments for user tags You can use key-value for tags along with old, positional, arguments, e.g.: { % custom_tag 123 "test" thing . var text = msg | upper username = user . get_full_name % } All of this implemented using common "args" and "kwargs": @register . simple_tag def custom_tag ( a , b , * args , * * kwargs ) : warning = kwargs [ 'text' ] profile = kwargs [ 'username' ] . . . return . . . assignment_tag Some tags are not built to output something in template, like simple_tag. They only have to change something in c...

Django template tags to find out field type

Sometimes you have the task to decorate django form fields. In our case it was need to add different widget to form field depending on field type. You can know your field type by refering to it's python class name, e.g.: field . __class__ . __name__ You will get wrong symbols error if you try to refer this python variable in templates directly. So you need a custom template tag to serve here. Decision was not to return variable for tag but to add context variable, cause I had need to perform exact actions on certain field types. So tag example: @register . simple_tag ( takes_context = True ) def set_this_field_type ( context , field ) : """     Adds to context given field type variable     variable named "this_field_type"     """ context [ "this_field_type" ] = field . field . __class__ . __name__ return '' Now you can add this variable to your if statements. E.g. usage: {% ...

Django: compressing CSS/JS files with django-compressor

There are 2 main usual tasks with web project's deployment and about .css and .js files. First one - size minimization. But there are lot's of utilities helping you to compress CSS files. Delete unused spaces, comments and so on... Second one - Version control. For e. g. When you've updated the script on your deployment server, but user's browser uses old one until user manually hits 'Refresh'. That's where Static files compressor comes in to mind. Upon selecting among available one's found top "google" results: - django-compress - django-compressor - webassets Project uses 'django.contrib.staticfiles', so django-compress was not compatible... It does not support Django's static files gently. Webassets s a good lib. but project has a huge amount of different static. Maybe it's ok for e small project, but when you need to specify/change a 100's javascript in python module for certain templates... Nothing good comes ...