Django + jQuery: Sometimes AJAX, but always DRY?
- by Justin Myles Holmes
Let's say I have an app (in Django) for which I want to sometimes (but not always) load content via ajax.
An easy example is logging in. When the user logs in, I don't want to refresh the page, just change things around.
Yet, if they are already logged in, and then arrive at (or refresh) the same page, I want it to show the same content.
So, in the first case, obviously I do some sort of ajax login and load changes to the page accordingly. Easy enough.
But what about in the second case? Do I go back through and add {% if user.authenticated %} all over the place? This seems cold, dark, and WET.
On the other hand, I can just wrap all the ajaxy stuff in a javascript function, called loggedIn(), and run that if the user is authenticated. But then I'm faced with two http requests instead of one. Also undesirable.
So what's the standard solution here?