视图.py
def save_report(request):
form = ReporterRegisterForm()
profileform = ProfilecontactForm()
user = request.user
userprofile = UserProfile.objects.get(user=user)
if request.method == 'POST':
id = request.POST.get('id')
user = User.objects.get(pk=id)
userprofile = UserProfile.objects.get(user=user.id)
form = ReporterRegisterForm(request.POST, instance=user)
profileform = ProfilecontactForm(request.POST,instance = userprofile)
if form.is_valid() and profileform.is_valid():
form.save()
profilesave = profileform.save(commit=False)
profilesave.save()
return render(request, 'setting/edit_reporter.html',
{'form': form,
'userprofile':userprofile
})
网址.py
urlpatterns = patterns(
'setting.views',
(r'^$', 'index'),
(r'^save-reporter/$', 'save_reporter'),)
js:
$('.reporter').click(function () {
alert(1)
var id = $(this).attr('id');
var csrf_token = $("#csrf_token").val();
$.ajax({
data:{
csrfmiddlewaretoken: ('{{csrf_token}}'),
// form:form.serialize(),
id:id,
},
type:'POST',
url: '/setting/save-reporter/',
success: function() {
$('#authorisedreporter').show();
}
});
});
html:
<button type="button" id="{{ list.0.id }}" class="openDiv">{{list.0.name}} </button>
{{ list.0.id }} id from list 是用户表中的id
这是我的views.py来更新表单数据。我想实例化表单数据,需要在更新模式下显示该字段。它没有被实例化。我从POST获得正确的ID。需要帮助