views.py
def index(request): """""""""""""" registerform = UserRegisterForm(request.POST) createprofileform = UserCreateProfileForm(request.POST) if registerform.is_valid() and createprofileform.is_valid(): result = registerform.save(commit=False) result.set_password(request.POST['password'])
index.html
{% block main-content %} <table width="98%" border="0" style="margin-left:0.7%;" cellpadding="0" cellspacing="0" id="rounded_table"> <tr > <td width="50%">Main Account Holder</td><td width="50%">Authorised Reporters</td> </tr> <tr id="main_account"> <td width="50%">All data related to main account holder comes here</td> </tr> <tr id="authorised_reporter"> <td width="100%" colspan="2"> <div id="authorisedreporter" {% if not registerform.errors %}style="display:none"{% endif %}> <form method="post" action="." id="reporter-form">{% csrf_token %} <table width="100%"> <tr> <td style="width:100px;">First name:</td><td>{{registerform.first_name}}</td> </tr> <tr> <td>Last name:</td><td>{{registerform.last_name}} </td> </tr> """"""other form fields"""""""" <tr> <td colspan=2""><p align="right"><button type="submit" title="Save" >Save <img src="{{ STATIC_URL }}images/button-icon-ir-fwd.png" width="12" height="17" alt="" /></button></p> </td> </tr> </table></form> </table> {%endblock%}
The above views.py and index.html are for saving a new user entry.
My html template is divided into 2 sections, the tab "Main accountant accountant" and the tab "Authorized reporters". the tab "Main accountant accountant" is used to save information about the profile, and the tab "Authorized reporters" is intended to create a new user page. Downloading the main tab “Account Owner” will be active and the user’s tab will be hidden. If the user tab is selected, the tab "Primary account accountant" will be hidden. After the user is saved, the user data is displayed below, in the lower format.
{% for list in member_list %} <tr class="ir-shade"> <td style="width:120px;"><span><input type="submit" name="delete" value="{{list.0.id}}" class="delete_reporter" /></span><button> id="{{ list.0.id }}" class="openDiv">{{list.0.first_name|title}} {{list.0.last_name}}</button></td> <td style="width:410px;"> {{list.0.email}} {{list.1.phone_daytime}} {{list.1.phone_mobile}}</td> </tr> {% endfor %}
Actually I want Onclicking <button> id="{{ list.0.id }}" class="openDiv">{{list.0.first_name|title}} {{list.0.last_name}}</button> saved user data should be displayed in the same field in editable mode. I go through the user id in the button. By clicking the button, the data related to the user ID should be displayed in editable mode.
JS:
$('.openDiv').click(function () { var id = $(this).attr('id'); var csrf_token = $("#csrf_token").val(); $.ajax({ data:{ csrfmiddlewaretoken: ('{{csrf_token}}'), id:id, }, type:'POST', url: '/setting/save-reporter/', success: function(data) { $('#authorisedreporter').html(data); } }); });
Below are view.py and html to display the saved instance of the form. Now I can show the saved instance of the form, and I load the instance in the authorizedreporter div (please check js and index.html). At this time, if I kit save, it creates a new record, it calls view.py associated with the index.I method I want to update and not save the record.
save_reporter.html
<form method="post" action="." id="{{ id }}"> {% csrf_token %} <table width="100%"> <tr> <td style="width:100px;">First name:</td><td>{{form.first_name}}</td> </tr> <tr> <td>Last name:</td><td>{{form.last_name}}</td> </tr> <tr> <td>Daytime phone:</td><td>{{profile.phone_daytime}}</td> </tr> <tr> <td>Mobile phone:</td><td>{{profile.phone_mobile}}</td> </tr> <tr> <td>Email:</td><td>{{form.email}}</td> </tr> <tr> <td>Password</td><td>{{form.password}}</td> </tr> <tr> <td colspan=2"<p align="right">{% include "buttons/save.html" %}</p></td> </tr></table></form>
views.py
def save_reporter(request): user = request.user id = request.POST.get('id') user = User.objects.get(pk =id) userprofile = UserProfile.objects.get(user=user.id) form = ReporterRegisterForm(instance=user) profileform = ProfilecontactForm(instance=userprofile) return render(request, 'setting/save_reporter.html', {'form': form, 'id':id, 'profile':profileform })
I have explained my current problem that I am facing, please help me with this. thanks