Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Let me resume what you are doing:</p> <pre><code>$.ajax({ data:{ /* ... */ }, type:'POST', url: '/report/save_reporter/', success: function() { return true; } }); </code></pre> <p>Here you setup an Ajax asynchronous query to post data to the server. When the query reaches the server, if it doesn't crash, the <code>success:</code> callback is called and javascript does nothing (<code>return true;</code>).</p> <pre><code>$('#authorisedreporter').show(); </code></pre> <p>Here, you show an HTML node <strong>before the asynchronous Ajax query ends</strong> (succeeds or fails). To show the element after the Ajax query is done, put this code inside the <code>success:</code> or <code>error:</code> callback.</p> <p>Finally, if you count your <code>(</code>, <code>)</code>, <code>{</code> and <code>}</code> you will see than <code>$('#authorisedreporter').show();</code> is outside of the click callback. Therefore, if it is out of the document ready callback, it will have no effect.</p> <p>So the right Javascript code should be (I think):</p> <pre><code>$('.openDiv').click(function (e) { e.preventDefault(); var id = $(this).attr('id'); var firstname = $("#"+id).find('#id_first_name').val(); var phonemobile = $("#"+id).find('id_phone_mobile').val(); $.ajax({ data:{ csrfmiddlewaretoken: csrf_token, edit_reporter:true, id:id, first_name:firstname, phone_mobile:phonemobile, }, type:'POST', url: '/report/save_reporter/', success: function() { $('#authorisedreporter').show(); } }); }); </code></pre> <p><strong>EDIT:</strong></p> <p>About your view.py, you save a UserProfile but do not return anything to the client browser. Nothing.</p> <pre><code>def save_reporter(request): user=User.objects.get(user=user) # you should use django.shortcuts.get_object_or_404 userprofile = Userprofile.objects.get(user=user) # same comment if request.method == 'POST': registerform = UserRegisterForm(request.POST,instance=user) createprofileform = UserCreateProfileForm(request.POST,instance=userprofile) # you create 2 forms above, but don't use them. Is it normal ?! # you should do loops "if registerform .valid(): ... else: ..." and use it. Same for the 2nd form if 'edit_reporter' in request.POST: first_name=request.POST.get('first_name') # can be None phone_mobile = request.POST.get('phone_mobile') # can be None user = User.objects.get(pk=user) user.first_name = first_name user.save() # put it inside a try/except statment userprofile = UserProfile.objects.get(pk=user) # use get_or_404 userprofile.phone_mobile = phone_mobile userprofile.save() # put it inside a try/except statment return HttpResponse() # this returns an empty html page, do you want to return the form here ? </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