Note that there are some explanatory texts on larger screens.

plurals
  1. POcalling function from jQuery with Django not working correctly
    text
    copied!<p>Im trying to make a page that searched for users based on a users criteria. It then loads the results via AJAX response into a different DIV:</p> <pre><code>function search_friends() { $("#search-results").show(); $("#loading1").show(); var q = $("#id_q").val(); var type = $("#id_search_type").val(); $("#loading1").hide(); $("#results").load("/search/friends/?ajax&amp;q="+encodeURIComponent(q)+"&amp;search_ty­pe="+encodeURIComponent(type)); return false; } </code></pre> <p>Inside this new results DIV, I have links for each user to be able to add them as a friend. Each link in this results DIV has a ID of each users userID and a class of user_link. When the logged in user clicks on the link I want it to pop up a confirmation box then send the request via AJAX. However, I cannot get the links to submit via AJAX like I want. The code I have is below:</p> <pre><code>{% for u in users %} &lt;div id="results"&gt; &lt;img src="{{ u.profile_pic }}" class="xsmall-pic" /&gt; &lt;a href="/{{ u.username }}/"&gt;{{ u.username }}&lt;/a&gt;&lt;br /&gt; &lt;span class="small-date"&gt;{{ u.get_full_name }}&lt;/span&gt; &lt;span class="floatR" id="user_{{ u.id }}_link"&gt;&lt;a href="#" id="{{ u.id }}" class="user_link"&gt;Add as friend&lt;/a&gt;&lt;/span&gt; &lt;/div&gt;{% endfor %} &lt;script type="text/javascript" language="javscript"&gt; $(document).ready(function() { var csrf_token = "{{ csrf_token }}"; $(".user_link").bind('click',function() { request_friend($(this).id,csrf_token) }); $("#search-friends-form").submit(search_friends); }); &lt;/script&gt; </code></pre> <p>In an external JavaScript file I have the following:</p> <pre><code>function confirm_request() return confirm("Are you sure you want to request this user as a friend?"); } function request_friend(id,token) if (confirm_request()) { var data1 = {to_friend: id,csrfmiddlewaretoken: token}; $.post('/users/requests/friends/'+id+'/?ajax', data1, function(json) { $("#user_"+id+"_link").html(json.status); }, "json"); return false; } else { return; } } </code></pre> <p>Thanks for any help as Im not all that great with Javascript. </p> <p><em><strong>EDIT</em></strong> Python function called via AJAX</p> <pre><code>def search_for_friends(request): users = False friends = False return_page = 'users/friend_search.html' ajax = 'ajax' in request.GET try: if 'q' in request.GET: form = FriendSearchForm(request.GET) if form.is_valid(): if form.cleaned_data['search_type'] == 'username': users = CustomUser.objects.exclude(pk=request.user.id).filter(username__icontains=form.cleaned_data['q']) elif form.cleaned_data['search_type'] == 'name': users = CustomUser.objects.filter(Q(first_name__icontains=form.cleaned_data['q']) | Q(last_name__icontains=form.cleaned_data['q'])) elif form.cleaned_data['search_type'] == "email": users = CustomUser.objects.filter(email__icontains=form.cleaned_data['q']) else: pass else: pass else: form = FriendSearchForm() if users != False: users = users error = False if users == "": users = "" error = "No users match the search term provided" if ajax: show_results = True context = RequestContext(request,{'users':users,'form':form,'show_results':show_results}) return render_to_response('users/friend_search_results.html',context_instance=context) context = RequestContext(request,{'users':users,'form':form,'error':error}) return render_to_response(return_page,context_instance=context) except: form = FriendSearchForm() context = RequestContext(request,{'form':form}) return render_to_response(return_page,context_instance=context) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload