I am working on a media server, where each video will have a small star. A JQuery event is connected to the handler button, so clicking on that button will send an AJAX post request to the server's favorite page, send it with the unique ID of the video for the favorite. After that, the JQuery will toggle the image (if not liked, with one click the star will become "brightness", if liked, a click will make the star transparent again).
If the user is not logged in, they can not explicitly favor the video. I want the user to click on the favorite button, then they redirect to the login page. However, AJAX response means that clicking on a favorite button will be toggling the favorite button image to the favorite button, which should not be so. The problem is, I do not know how I can short-circuit the JQuery function and stop toggles based on the user's login status.
Below is my favorite page, where the post request has been submitted.
@login_required def Favorite (requested): If request.POST.has_key ('id'): id = request.POST ['id'] id = id.rsplit ('_') Id = int (id [1]) m = Media.objects.get (pk = id) m.favorited_by.add (request.user.get_profile ()) m.save () if request.META.has_key ('HTTP_REFERRER' ): Returns HttpResponseRirectirect (request.META ['HTTP_REFERRER']) HttpResponseRedirect ('/') and JQuery
var imgSRC = {' Src1 ':' / static / images / star_off.png ',' src2 ':' / static / images / star_on.png '}; $ ('Favorites'). Live ('click', function () {var img = $ (this); id = $ (this) .Parent () .parent () .ttr ('id'); console log (id); $ .post ( '/ Favorite /', {"id": id}, function (data) {img.attr ('src', imgSRC.src2); img.attr ('class', 'favorite_o');})}} ; Edit:
I prefer JavaScript to be in my own file, rather than embedding JavaScript into the Django templates. As such, I will not be able to use the template tag for user authentication. I thought that a solution was searching for a unique developer layer, when the user is present when he is logged (the user's name appears in the header after login), but it is still my problem to do so Does not solve correctly, keeping my script separate from my template.
I will use the method in your template:
$ ( '. the favorite'). Live ('click', function () {{% if request.user.is_authenticated%} // user is logged in, go! Var img = $ (this); id = $ (this) .Parent () parent () Attr ('id'); console.log (id); $ .post ('/ favorite /', {"id": id}, function (data) {img. Attr ('src', imgSRC.src2) ; Img.attr ('class', 'favorites_on');}); {% else%} // login, redirect to login page! Window.location = "{% url your_login_url%}" // // Url name has its login URL named ur.py.py {% endif%}}); update
You can send your user's status to an external JS file:
at the top of your JS file puts these lines:
USER_STATUS = ''; LOGIN_URL = ''; // When you change, you will never know;) then
$ ('favorites'). Live ('click', function) {if (USER_STATUS == true) {// user is logged in, go! Var img = $ (this); id = $ (this) .Parent () .Parent (). Attr ('id'); Console.log (id); $ .post ('/ favorite /', {"id": id}, function (data) {img.attr ('src', imgSRC Src2); img.attr ('class', 'favorite_on');});} other {// not logged in, redirect to login page! Window.location = LOGIN_URL;}}); I tested the JavaScript part, but without the demo but I think it will start you.
Update 2
I call it my & lt; Script & gt; {% if request.user.is_authenticated%} USER_STATUS = 'true'; {% Else%} USER_STATUS = 'false'; {% Endif%} alert (USER_STATUS); and it was alerted to true .
No comments:
Post a Comment