Django + jQuery: Sometimes AJAX, but always DRY?

Posted by Justin Myles Holmes on Stack Overflow See other posts from Stack Overflow or by Justin Myles Holmes
Published on 2010-12-30T22:13:59Z Indexed on 2010/12/30 23:54 UTC
Read the original article Hit count: 317

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about AJAX