Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let me analyze your JS code a bit, since I can see several errors/mistakes there:</p> <pre><code>$('.openDiv').click(function (e) { e.preventDefault(); // where is following data taken from? At the point you click the .openDiv link, the form doesn't have any data yet so all of them will be empty string '' var csrf_token = $("#csrf_token").val(); var id = $(this).closest('td').attr('id'); var firstname = $("#"+id).find('#id_first_name').val(); var lastname = $("#"+id).find('#id_last_name').val(); var phonedaytime = $("#"+id).find('#id_phone_daytime').val(); var phonemobile = $("#"+id).find('#id_phone_mobile').val(); var email = $("#"+id).find('#id_email').val(); // do you use AJAX to get the form or use it to save the form? $.ajax({ data: $(this).serialize(), // this is wrong, $(this) is the link object, not a form type:'POST', url: '/setting/save-reporter/', success: function(data) { $('#authorisedreporter').html(data); $('#authorisedreporter').show(); } }); }); </code></pre> <p>Ok as far as I understand, after clicking the link, you are using AJAX to send request to Django view to fetch back the correct instantiated form. So you should:</p> <p>First, simplify your JS code:</p> <pre><code>$('.openDiv').click(function (e) { e.preventDefault(); var this_id = $(this).closest('td').attr('id'); // get the user ID, since that's all you need console.log(this_id); // making sure that you get the right ID $.ajax({ data: { id: this_id }, type: 'POST', url: '/setting/fetch-reporter-form/', success: function(data) { $('#authorisedreporter').html(data); $('#authorisedreporter').show(); } }); }); </code></pre> <p>Next, split your old view to several views to focus on what it needs to do (note: you can leave your <code>index</code> view as it is now):</p> <pre><code>def fetch_reporter_form(request): ''' Change your save_reporter view name to this view ''' registerform = UserRegisterForm() if request.method == 'POST': id = request.POST.get('id', None) if id: user = get_object_or_404(pk=user.id) userprofile = UserProfile.objects.get(user=user) registerform = UserRegisterForm(request.POST, instance=user) return render(request, 'setting/register_form.html', { 'user_id': id 'registerform':registerform }) else: return HttpResponse('Request does not contain any ID') else: return HttpResponse('Request is not POST') def update_reporter(request): ''' This function is for update the reporter ''' registerform = UserRegisterForm() if request.method == 'POST': id = request.POST.get('id', None) if id: user = get_object_or_404(pk=user.id) userprofile = UserProfile.objects.get(user=user) registerform = UserRegisterForm(request.POST, instance=user) if registerform.is_valid(): result = registerform.save(commit=False) # saving code here ... return HttpResponse('Success') else: return HttpResponse('Request does not contain any ID') else: return HttpResponse('Request is not POST') </code></pre> <p>You can see here there are 2 functions: 1 for fetching the right form from AJAX, the other for saving the data via normal form submit. Of course you should make the <code>urls.py</code> accordingly, something like:</p> <pre><code>urlpatterns = patterns('', # ... your code ... url(r'^setting/fetch-reporter-form/$', 'yourapp.views.fetch_reporter_form'), url(r'^setting/update-reporter/$', 'yourapp.views.update_reporter'), ) </code></pre> <p>You might also notice that you should make a new template <code>setting/register_form.html</code> that include your registration form HTML only (note: you need a hidden <code>id</code> field that was returned by <code>fetch_reporter_form</code> view above to identify the form):</p> <pre><code>&lt;form method="post" action="/setting/update-reporter" id="reporter-form"&gt; {% csrf_token %} &lt;input type="hidden" name="id" value="{{ user_id }}" /&gt; &lt;!-- your code here --&gt; &lt;/form&gt; </code></pre> <p>So the flow is:</p> <ol> <li>You go to <code>index</code> view. There is several forms to save new reporter, etc. as normal.</li> <li>You click on the <code>.openDiv</code> button. It will send the AJAX request above to <code>fetch_reporter_form</code> to get the correct form. (Your code is working fine at this point)</li> <li>You click Save button on that form, it will submit the updated data (via POST) to <code>update_report</code> view and update the reporter.</li> </ol> <p>I'm just trying to give you the basic idea. The rest is quite straightforward so I guess you can continue easily. Hope it helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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