Skip to main content

Posts

Showing posts with the label js

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