Skip to main content

Posts

Showing posts from October, 2011

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

Django: 'ManyRelatedManager' object is not iterable

Upon development proces you often meet ManyToMany relations that throw errors in templates. I prefer to feed everything to context and play with it there. This kind of error appears when you iterate through a model's ManyToMany field. To fix it just add a related 'all' manager through dot syntax. Like so: Model: class BannerImage ( models . Model ) : image = models . ImageField ( upload_to = "somedir" ) def get_image_url ( self ) : return 'Image url here' class Banner ( models . Model ) : name = models . CharField ( max_length = 250 , null = True ) images = models . ManyToManyField ( BannerImage ) View/Tag or else, that creates context: banner = get_object_or_404 ( Banner , pk = 1 ) return { 'banner' : banner , } Template causing the error: {% for image in banner . images %} < img src = "{{ image.get_image_url }}" /> {% endfor %} Template without the error: {% ...