python - form instance not displayed in widgets -
views.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() , profileform.is_valid(): form.save() profilesave = profileform.save(commit=false) profilesave.save() return render(request, 'setting/edit_reporter.html', {'form': form, 'userprofile':userprofile })
urls.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 list id in user table
this views.py update forms data.i want instantiate forms data,need show field in update mode.it not getting instantiated.i getting right id post.need help
assuming, want populate form request.user
. if want populate other user, need id url, , retrieve user
, userprofile
.
so view can updated
def save_report(request): user = request.user userprofile = userprofile.objects.get(user=user) form = reporterregisterform(instance=user) profileform = profilecontactform(instance=userprofile) if request.method == 'post': form = reporterregisterform(request.post, instance=user) profileform = profilecontactform(request.post,instance = userprofile) if form.is_valid() , profileform.is_valid(): form.save() profilesave = profileform.save(commit=false) profilesave.save() return render(request, 'setting/edit_reporter.html', {'form': form, 'userprofile':userprofile })
Comments
Post a Comment