CSS Templating system for Django / Python? - python

I'm wondering if there is anything like Django's HTML templating system, for for CSS.. my searches on this aren't turning up anything of use. I am aware of things like SASS and CleverCSS but, as far as I can tell, these still don't solve my issue as I want to dynamically generate a CSS file based on certain conditions, so that a different CSS file would be served based on a specific user session...
I want to minimize the use of javascript / AJAX for some things (since its for a legacy system running in some hospital where they're still using IE 6 ), also I have an interest in possibly minimizing javascript for other projects as well... so it would be where there is 1 CSS file, but that it may need to be changed based on the situation (which would be done with CleverCSS), however the problem is that if I just write the changes to 1 file, then this would be served to everyone, even though they may have a different "state" of the CSS file depending on their use of the application, so I want to remove the physical association of a CSS file and rather have it dynamically generated each time (so that its unique to a specific user's session), the way that Django's HTML templating system works..

The Django templating system can be used for any text you like. It's used for HTML most of the time, but it could also be used to create CSS. The CSS reference in your HTML can be to a dynamic URL instead of to a static file, and the view function can create whatever context you like, then a .css template file can create your CSS.
If you have only a few different CSS possibilities, then you may be better served by creating them as static files, and using the HTML template to select the CSS file you want by writing a different CSS reference depending you your conditions.

Related

Pelican: customizing index.html by reading metadata from YAML

I am looking to build a personal CV site using Pelican. Since I am not really targeting a blog site, I want a somewhat different index.html and it should be easy to maintain. I know I can directly make the template the way I want at the very least, but then it involves directly modifying html which defeats the purpose of using a static content generator.
I am thinking to represent the data using a YAML file, and pass in extra data to jinja2 only when index.html is being generated. So my question is, how should I configure a plugin so that it only applies to some specific files and alters the output?

Is it possible to generate static html from Django Cms?

Hi I'm working on a project based on Django Cms
DJango Cms
Most of the templates ara generated via an APi call in ajax.. I wondered if is possible to generate an HTML static file from the original template file in order to avoid dynamic calls.
Short answer: Yes.
Long answer: It depends.
There's little stopping you from just creating static HTML pages (you could for example just use wget to crawl you website. However note that this only works if your content is not dynamic, as in, it doesn't depend on whether a user is logged in or not etc. If you only use plugins that always have the same output, regardless of the request, then it'll work.
Since Django CMS gives you a lot of power to write highly dynamic plugins, there's no built-in way of generating these static pages (the chances of someone using it without realizing the drawbacks are high).

Django - Managing page content in Django Admin

I'm new to working with Django and am developing for a client who wants to be able to change page content in the Django Admin. They need to be able to change the html of the index page without editing the files on the server.
I know about flatfiles but I'm not sure that's completely what I'm after as I can't display stuff such as Django forms for example.
EDIT: Kind of like how a CMS works but without the users/group stuff and be able to use Django View modules such as forms.
Any advice?
Thanks
Honestly, the scope of what you're looking for is too huge to cover in this format. There's a number of ways this could be done, but they're all going to require some work and customization based on the client's needs.
Flatpages could work if you allow HTML content and make sure the content is rendered as "safe" in the template. This really only covers the "content" area of the site, though. It wouldn't be wise to use flatpages for an entire site template, including header, sidebar, footer, etc.
You could create editable areas. So, you actually create models for things like headers, sidebars, footers, and modules within those areas, and then just pull them into the template as needed. Then, the client is only editing pieces of the template instead of responsible for the whole HTML document.
Forms are going to be a challenge, because they require backend-processing that requires a connected view. The client won't be able to just arbitrarily drop in some form code and have a form. But, you could use a third-party service form forms and just embed them in the available content regions. Or, there's a couple of django apps that try to implement a type of "form builder" in the admin. That might somehow let the client add a form via something like the shortcodes used in Wordpress, but you'd likely have to lay down some infrastructure to make that work.
At a certain point, stuff like this reaches a point of diminishing returns, though. The only way to allow total customization of the template is to drop down into the actual physical file and make changes there. You can make certain things easier for the client, but ultimately, they either need to scale back their customization needs or deal with the fact that they'll have to work with the filesystem.
I don't believe that is possible at this time. Of course you can edit your models but templates, I think not.
I would find out how much they need to change? If they plan a complete redesign every week then you're still looking for an answer. If they just need a dynamic front page then you can split it up into variables and let them edit sections of html. Much safer and less prone to breaking the html.

Why should JavaScript files be localised differently in Django?

When localising Django application the makemessages command simply parses all the TXT, HTML and PY files and generates PO files for them but when localising JS files, you need to run the djangojs command. I haven't delved into the Django source to figure out why this done differently. Could someone explain?
I've read that in production environments, Apache is used to serve the application files while a simple proxy like Nginx is used to serve static files as this greatly reduces the load on the application server. With this scenario, I guess it works like this: when rendering a template, Django checks the requested locale, loads the appropriate localisation file and serves the template but JS on the other hand being served as static media doesn't get parsed by Django. Is this it?
(Its my first foray in to the world of localisation with Django and I'm packed full of question, many of who's answers I can't seem to find and therefore this post.)
Thanks
The reason why it's handled differently is in the docs.
Adding translations to JavaScript poses some problems:
JavaScript code doesn't have access to a gettext implementation.
JavaScript code doesn't have access to .po or .mo files; they need to be delivered by the server.
The translation catalogs for JavaScript should be kept as small as possible.
So essentially, the internal Python translation is done on the server. But for JS, there's another file served by the server, which contains all the required translations for user's language. And the translation is done on the user's side. So as you can see, it's a completely different strategy. Django helps by adding similar interface for JS files, even though they're handled in a completely different way.
I guess it works like this: when rendering a template, Django checks
the requested locale, loads the appropriate localisation file and
serves the template but JS on the other hand being served as static
media doesn't get parsed by Django. Is this it?
You are right in the first part, about handling templates. Handling JS works as I've explained above.
Note that Django JS translation mechanism, doesn't treat JS translations as static files. It uses a Django view to generate the JS file everytime (javascript_catalog mentioned in the docs linked in the first line).
That is one of the problems I've encountered. Such files don't need to be generated on every request. There are some projects that actually let you pack those JS translations as static files and enable you to cache them properly (like django-mediagenerator).

Can a python view template be made to be 'safe/secure' if I make it user editable?

Say I need to have a templating system where a user can edit it online using an online editor.
So they can put if tags, looping tags etc., but ONLY for specific objects that I want to inject into the template.
Can this be made to be safe from security issues?
i.e. them somehow outputing sql connection string information or scripting things outside of the allowable tags and injected objects.
Yes, use a template engine that has sandboxing features, like jinja2

Categories