Skip to main content

Posts

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...

Timezones. New in Django 1.4.

What and Why? Under Django version 1.4 all dates stored and displayed for a single timezone. The one that is specified in project.settings.TIME_ZONE . Now you gain ability to store dates in UTC and render it with timezone correction. Problems with localtime  bypass is an additional plus. They can happen once a year. For e.g. 31 of November 2012 in Russia. Time from 2:00am to 3:00am in fact goes by twice. It may not be a problem for 99% of users. But it can become a nightmare for billing systems. So it's better to store time in UTC and display with user Time Zone correction. So "02:15am 31 November 2012" will become " 2012-10-30T22:15:00+04:00" and "2010-10-30T23:15:00+03:00" that is so handy for programmers ;). Concepts datetime objects in Python support timezones with attribute tzinfo . If this attribute is filled out it is called "timezone-aware", otherwise it's "naive" date. Django uses timezone-aware date...

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: {% ...

Delete all '.pyc' files recursively OS X

We often get strange errors with python. When you changing the source and cannot figure out why files are used from past. So if you ever get this dejavu feeling, try using this command: find . -type f -name '*.pyc' -exec rm {} \; It will delete all precompiled python sources. (Because with python you have ability to distribute your files in binaries in fact.) Those errors usually appear if you move/delete *.py files with version control. You usually exclude *.pyc files from list. Old links used this way. Debugger does not suggest wrong imports because your files are in fact there, just precompiled. Anyway I've just put this command to my blog for memorizing. It often helps me. Maybe it can for you.

Python / Django UnicodeEncodeError hacks.

Python plays with unicode nicely nowadays. Bt what if you must deal with old time formats conversion, or ASCII files exporting for e.g. You may also use software that is out of date but is too long to rewrite...  Here often errors occur. I have received mine at copy-pasting from MS Word into Django admin UI  by stupid users. Most of the website played nicely with this fancy characters, but exporting to CSV failed due to non ASCII characters support. Google said nothing special. Python docs about unicode usage briefly cover this type of events. So here is the result of some hours of experiments. I've decided to rewrite some of the python functionality to create decode function with behavior for my needs. Hopefully they will shorten you some time with those collisions you may get in your Django apps... Anyway I've started to receive errors like: Exception Type: UnicodeEncodeError Exception Value: 'asc...

Installing CouchDB for Mac OS X. Nuances.

Our project will have a new architecture changes in future. We've chosen from different tech. But now we 're experimenting with CouchDB a bit. It may become a part of our new architectural Changes. I'll try to cover most of newbies observations with CouchDB for Python(Django) programmer, planning to use it in your project. First article will be, unsurprisingly, installing CouchDB under Mac OS X. It, sure, has some inches that you need to scratch. So let's roll... CouchDB is not a simple SQL database like you used to install before. It has own server to accept connections. And, in fact, you can work with database from your browser GUI (without other server proxy, like Django or ...). Anyway it will not be our target. Building JS UI is quite easier with CouchDB and changes your logic dramatically. To build UI with Python we need some proxy parts. Anyway, again, CouchDB is a standalone server, that runs on your server's port and talks to your APP/Browser/wha...

Establishing Dev environment with PyCharm + Virtualenv for Django development

I'm a fan of IDE's. Also I'm a fan of GUI's. I also use console where needed, but why waist time typing commands when you can just point and click. I've been using Eclipse + PyDev for almost a year and recently switched to PyCharm. Why I prefer PyCharm: - Template Debugging. (you can set a normal breakpoints in templates) - Easy Virtualenv connection. (Set an interpreter from your virtualenv and you're ready to roll) - Has excellent set of most common CVS integrations (Git, SVN, Redmine, etc...) - Has looots of tiny tasty things for coding and proper code highlighting over JS and HTML out of the box) - Has proper and shiny themes out of the box. So I'm a blind minded blond, it seems now. But design of this IDE is really attractive IMHO :) - OH and it's quite quick and has ALL the functions I need. Here is a brief instruction to setup PyCharm IDE to work with my typical Django project. 1.  First thing you need is a PyCharm installation. Y...