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?
Related
I am creating a website from scratch where I am planning to make it dynamically multilingual. Tools I use are:
MongoDB for my database (atlas)
pymongo to connect to DB
django as my framework env
My idea is to have a website that can have multiple languages as necessary. languages can be RTL and LTR. I can add any language in a form of json file that has an item against its translation to the desired language, for instance english-french json file would be:
{"door":"porte"}
{"direction": "LTR"}
by adding such a file my website should be able to have the right information regarding the french language for instance and everything should appear in french correctly as specified in the json file. I plan to have two types of html pages in my DB for each of my website pages; one for "RTL" and one for "LTR", I render them based on the request of the user. (language chosen in the front end). In translation process, after picking the right orientation of the language, I use django template variables in my html to place the right language using the correct json file of the language, all my json files are stored in the database, and retrieved by the desired lanaguge chosen by the user.
I have couple of questions:
Am I thinking in the right direction?! or I am re-inventing the wheel and there are other tools that does the job for me?!
Is there a best practice for international websites?!
Is there any tool that can help does that more easily?
any suggestions or comments is highly appreciated.
Thank you
You are definitely re-inventing the wheel. Django already comes with i18n support.
This works for nearly all elements.
As described here:
https://docs.djangoproject.com/en/3.1/topics/i18n/
I personally would recommend using gettext_lazy() as it makes things easier, but that's just my taste.
There is of course also a template tag called {% trans "Whatever" %}
You will end up with text files that you can simply edit and also use SaaS if you want a fancy user interface.
I am new with Django but I understand that the aim is to never reinvent the wheel. I am developing an application to supervise a factory and in all the html file I need to implement a tree representing the structure of the factory.
-> Factory
-> Production Area
-> Machine 1
-> Machine 2
etc
So I decided to put this tree structure in the base.html file and extend it on every others html files.
The problem is that I need to extract the element from my database, using the views.py files in order to call the render function to send it to the html file.
My question is how to access this data in the base.html, because he dont got any views.py to use a render function.
Thanks, Baptiste.
If you want something to show up in every template of your website (a tree in your case), you can put it in a base template and inherit from that template.
I personally (and many others as well) use a 3-level template structure. You can find an example/explanation in this GitHub repo and this post. Feel free to adapt the idea to your needs.
So in your case you could put your tree structure (the factory structure/layout) in your base template. If the data of the tree has to be fetched from the database, then you could use a custom context processor so that you don't have to repeat the tree-fetching-code in every view.
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).
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).
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.