Any drawbacks or gotchas to using Jinja2 templates in Django? - django

After reading the Jinja2 documentation, I'm interested in employing it in future Django projects. However, I'm wondering if anyone has encountered any drawbacks or gotchas when using Jinja2 templates with Django? If so, how did you work around them?
I wouldn't mind hearing about positive experiences either, just to get a good cross section of the best and worst of Jinja2.

I use Jinja2 in some of my projects and love the extra expressiveness it gives me. I can keep my presentation logic and application logic separate, but I don't have to bend over backwards to call into a function/method I've designed specifically for my presentation layer.
In addition to what's already been listed by other posters, here are some things that I've found:
The Admin app is tightly coupled to Django templates
The default views and decorators that come with the Auth app (and elsewhere) are coupled to Django templates, so you may have to duplicate the effort if you want to use your Jinja2 templates for login/logout/etc
Behaviorally, Django templates will escape its output by default whereas Jinja2 will not. I think either approach has its own merits, but you have to keep this in mind if you are switching between the two.

I've documented several of the syntax, config, filter, and interoperability considerations for Django -> Jinja2 on my wiki

I haven't used Jinja2 with an actual Django site yet, but I did convert an application using Django templates in standalone mode over to Jinja2 templates. The only (very minor) problem I encountered was lack of the {% spaceless %} template tag.

Extending Jinja2 is much harder than Django template system (I'm talking about templatetags). While most of inclusion tags functionality can be achieved using macros in Jinja (they even seem to be more appropriate), writing bit more complicated tags is really hard in Jinja (see the docs for yourself).
Other than that, the only obstacle are Django-based habits... ;)

There's been some new code added in the Django trunk that lets you write TemplateLoaders and Template classes that can be used to work with different template languages. Docs have been added for it at http://docs.djangoproject.com/en/dev/ref/templates/api/#using-an-alternative-template-language, and it will be in the 1.2 release. This should cut out most of the gotchas with things like using custom templates for login, logout, admin etc.
An alternate solution is to use a layer on top of Django, like Chouwa or Djinja2. You will have issues getting Django's builtin views to use your templates, but it works if you don't want to use Django trunk.
Once you've done either of those, the only really major issue is that most of the stuff Django exposes to templates (especially for the comments framework) is exposed in custom tags, which don't translate to Jinja2. Sadly, backwards-compatibility concerns don't see this changing anytime soon.

For me, the most annoying thing from using Jinja2 in Django is that you won't be able to use some Django apps when they come with their own templates or template tags (e.g. django-uni-forms).
This can be frustrating some times, when you find a great app that solves your problems but you can't use it because it's not compatible with Jinja2.
BTW, it seems that Armin Ronacher (the author of Jinja2) will be working on a new template engine backend that will sit behind both Jinja2 and Django, replacing the current infrastructure but preserving backwards-compatibility. https://www.djangoproject.com/weblog/2011/apr/25/gsoc/

re: the lack of {% spaceless %} in jinja2, check out the jinja2htmlcompress module:
# In shell:
fetch -o myapp/jinja2htmlcompress.py https://raw.github.com/mitsuhiko/jinja2-htmlcompress/master/jinja2htmlcompress.py
# In your app:
app = Flask(__name__, static_path='/static')
app.config.from_object('myapp.default_settings')
app.jinja_env.add_extension('myapp.jinja2htmlcompress.HTMLCompress')

As April 2015, Django 1.8 supports rendering templates with multiple engines within the same project, and has built-in support for Jinja2. So it doesn't have to be an all-or-nothing decision anymore.
(While this isn't directly answering the question, since this was previously the case I thought it merited more than just a comment).

I had some issues getting crispy-forms to work with Jinja2. There is a pretty easy way to solve this however.
django crispy forms with jinja2
I think in general downside will most likely be similar often used Django packages that just don't play with Jinja2

Related

Django using another templating engine (Chameleon)

I wonder if i can use another templating engine besides django's on django project, to be exact, it's chameleon, and there are any performance issues? Just curious, because i'm more familiar with chameleon
The easier part to replace in Django is the template engine, and it is not famous for the astonishing performance.
I'm pleased with the results from mixing Django and Jinja2, unfortunately I can't comment on chamaleon.

What would be an appropriate equivalent of Jinja's macros in Django's templating system?

Let's just say that I need to render a certain amount of HTML over and over in a page, for example, for a user's profile information. Jinja's macros seem like they're absolutely fit for such usage. However, Django doesn't have macros.
Right now, I'm using a custom filter for the same purpose - is this the best way to go about it, or am I missing something?
Thanks.
ps. I'm migrating an app that ran on GAE with webapp2 & Jinja to Django.
After some more mucking with the documentation, I finally found the "correct" way to do this: custom inclusion tags, which let you create a tag out of a template.
There are two other approaches that you could take: blocks and includes. With blocks you would have to include it all the way through the inheritance strand. With includes, you just load as necessary but you have to make sure that you're passing the appropriate context variables in from your view. However, your current approach is probably the most useful in terms of DRY.
Django doesn't have macros, so it's either custom filters, or separating your repeating code in its own template, and including it over and over with different arguments passed through "with", which will probably work slower than macros. But you can actually use jinja2 templating system with django perfectly fine.
Just for the record, I actually spent a fair bit of time adapting a pre-existing snippet to do macros in django in a fairly robust way, if you really want macros please check it out.
As advice, most of the time what you want to do with a macro, you should really do with an include tag, as mentioned; however, sometimes you just really want a macro and for that you have to use a template tag library like mine (which is one of a few that do this for django).

View centric design with Django

I'm relatively new to Django and I'm designing a website that primarily needs usability experience, speaking of optimized CSS, HTML5 and UI stuff.
It's very easy to use Django for data/Model centric design. Just designing a couple of Python classes and ./manage.py syncdb - there's your Model.
But I'm dealing with a significant amount of View centric challenges. (Different user classes, different tasks, different design challenges.)
The official Django tutorial cursorily goes through using a "Template".
Is there any Design centric guide for Django, or a set of Templates that are ready and useable? I don't want to start from scratch using JS, HTML5, Ajax and everything. From the Model layer perspective Django is very rapid and delivering a working base system. I wonder whether there's something like that for the Views.
Probably django-blocks (http://code.google.com/p/django-blocks/) is aiming a bit into this direction! But otherwise I guess your only choice is to assort to some other 3rdparty html/css + js/aja frameworks depending on the functionality you need! There are also some snippets out, that implement templatetags that output common used html, but nothing big in general!
Is there any Design centric guide for Django, or a set of Templates that are ready and useable?
Django templates alone aren't that much reusable, since their are tied to specific views and variables defined in that views. In my personal experience, I found templating still rapid with an average size of my templates around 50 lines. Of course, this application wasn't heavy on the UI.
Inheritance and fragments will save you from repetition.
Inheritance:
{% extends base.html %}
Fragments:
{% for location in locations %}
{% include "_location_item.html" %}
{% endfor %}
I don't want to start from scratch using JS, HTML5, Ajax and everything.
Well, at a point, you'll start from scratch.
Version-control your project, so you could extract intermediate states for the coming project, if appropriate,
use efficient libraries like jQuery to write less, and do more,
use some CSS scaffolding tool like blueprintcss.
I agree that there is an overhead at the beginning of a project. My justification of that overhead so far has been the fact, that the UI wasn't a modified template (like modified typo3 or so templates, hence more tailored to the applications needs), that it was fast and looked good in the end, too.

Django grappelli

Does anyone here use django-grappelli here ?
I would like to read some experience of developers or users, if there are common mistake to avoid or why you use or do not use grappelli.
Thanks for sharing
I'm using grappelli as well. I'd be content with django's normal admin, but if you need to present the backend to someone else grappelli is much more appealing.
The current grappelli version 2.1 is working quite good with django 1.2. The only problems you(sometimes) run into is that 3rd party apps have sometimes some problems, which you can solve in most cases quite easily with changing the custom templates they are coming with (in most cases it's just other names for css classes etc, sometimes there are some js incompatibilities as well, which you can solve easily!
The actual version of grappelli also has a nicer html/css framework which enables you to easily use their styles/ui elements in your own templates. Have a look at http://grappelliproject.com/ for that (it's not totally valid for the actual version but gives you a small impression on what to expect!)
According to the developers, grappelli should soon work together with the marvellous django-admin-tools,which offer you drop down menus within the admin and a customizable index dashboard! Installation is not quite complicated, just do not forget to pass the adminmedia folder to manage.py! All in all I think it's quite recommendable (also check out django-filebrowser and medman if you dont know them yet, which come from the same developers and work very well together with grappelli)!
I have used Grappelli several places and suggest using it, if you are using admin for anything more than a "database debugging" purpose.
From django 1.1.1 onwards, you can create multiple instances of admin, so generally, I keep the main admin to examine the data as it is, and another instance of it for using purpose, with grappelli.
A minor issue is that,
On the dev server, it is hard to ask django to use a different admin media folder, and the easiest way is to use the command line parameter, as it is documented.
I started using django-grappelli recently and will like to point out 2 cases that newbies like me have a high chance of wasting time on:
For Django==1.6, use grappelli==2.5 and not the current latest 2.6. Also for Django==1.5/1.4 use grappelli==2.4.The official docs clearly mention it but many may overlook it and later wonder why it is not working.
In INSTALLED_APPS setting, if grappelli is not placed above django default apps it will not work. I used to think that order is not of importance for INSTALLED_APPS but this made me change my mind.

Beginner: Do I have to use templates when implementing my own designs using CMS such as Joomla?

Brand new to CMS:
With Joomla or other CMS, If I want to use my own design, do I have to make it a template first using proprietary code of some sort = ie using some Joomla templating language - or is using my own design with Joomla simple and independent?
I am not sure the role "templates" play with Joomla and other CMS. I gather they can be used as a complete "pre-made" design where you just tweak it, but are they necessary as a base of some sort to using my own designs? I am proficient with HTML and CSS. I also have been playing around with various Jquery Plugin's for my UI.
Ideally I would like to create any design I can think of with any layout I create and use CMS simply for its database and plugins etc such as a blog or calender back end. I might use it for more features as I learn about them. Do I need to use a "template" or create a "template" to do this?
Thanks!
{edit}
You have to create a template (or change an existing one), how else will the CMS know where to add the information into as the template is a HTML, CSS and Javascript Page group devoid of information and some HTML Tags replaced with CMS Specific tags, which tells the CMS where to add the information. It also include certain files telling the CMS the Name, and othe information of the template.
The First step in creating a template is to have a design.
Do your design in HTML and CSS. (Preferably using the CMS Template folder/directory structure.)
For Joomla use the information in this post: How should I go about writing a Joomla! template?
In Joomla jQuery have to be used in compatibility mode.
The CMS will supply the information, you supply the design and tell the CMS
{/edit}
The Template is the design container, whether you create one from scratch or reuse a current template. I initially used one of the pre-installed templates (together with the documentation) to learn some of the complexities of designing the template. (ex. structure, and what is possible.)
Please also look at my answer to this question How should I go about writing a Joomla! template? as it is a pretty good beginners guideline for template designers in Joomla (Even if I have to say it myself) with a couple of other resources linked in as well.
A Joomla template is nothing but HTML layout stylised using CSS. So to create your own templates you need a good grasp on HTML and CSS.
Joomla, by default provides a couple of templates to start with.
If you are just beginning, creating your own template is not where you want to start...learn more about setting up Joomla. You have enough problems there :)
Joomla! 1.5: A User's Guide is a great beginner book. It takes you through learning Joomla, what templates are, how to make them, and even walks you through setting up WAMP so you can experiment on your local machine.
The templates only affect the styling of your site. If you are starting out, I would work on getting your content in place first and wait on templates until later on.
My experience has been revamping my two websites to use WordPress, then moving to ModX, then moving back to HTML. I believe that you don't need a template but it will make some things easier as it bundles a lot of stuff you'd otherwise have to do manually. --At least for WordPress. I'm not even sure we found a good ModX template but adapted [poorly] a WordPress one. ModX is a good but little known CMS.
With one of my sites I found that it was impossible for WordPress to handle the images, thus I began working with a plug-in developer to create a custom plug-in. He took about a year and it still wasn't useable. Also I hired some consultants along the way. Their biggest contribution was "to complexify things" so I couldn't make any changes myself without going back to them & paying hundreds of $ to change a phone # for example.
I chucked all that and redid everything in HTML5 with Javascript, css and a little php. People here on S.Overflow actually helped me with the coding. I have a 3rd simple site that I may go back to WordPress for but only to use some particular functions which might be a pain to program otherwise.
Here is one of my 2 HTML sites:
coinsandhistory.com