Reputation: 2122
When i am passing instance1
in context it giving error 'User' object is not iterable
. I want to display Name in Template of pk
. Example http://localhost:8000/users/15/profile-update/
pk=15
def userProfileUpdate(request, pk):
if request.method == 'POST':
u_form = UserUpdateForm(request.POST, instance=User.objects.get(id=pk))
p_form = UserProfileForm(request.POST,
request.FILES,
instance=UserProfile.objects.get(user_id=pk))
if u_form.is_valid() and p_form.is_valid():
u_form.save()
p_form.save()
messages.success(request, 'Profile Updated!!!')
return redirect('users')
else:
instance1 = User.objects.get(id=pk)
u_form = UserUpdateForm(instance=User.objects.get(id=pk))
p_form = UserProfileForm(instance=UserProfile.objects.get(user_id=pk))
context ={
'object': instance1, # This is giving the error
'u_form': u_form,
'p_form': p_form
}
return render(request, 'users/userprofile.html', context)
TypeError at /users/15/profile-update/
'User' object is not iterable
Request Method: GET
Request URL: http://localhost:8000/users/15/profile-update/
Django Version: 2.1.4
Exception Type: TypeError
Exception Value:
'User' object is not iterable
Exception Location: /home/codism-7/.local/share/virtualenvs/django-njoxc1BQ/lib/python3.5/site-packages/django/template/defaulttags.py in render, line 165
Python Executable: /home/codism-7/.local/share/virtualenvs/django-njoxc1BQ/bin/python3
Python Version: 3.5.2
Upvotes: 0
Views: 3389
Reputation: 2122
Found the ans. It does not take object name instead we should put name different thant object. Example
object12
context ={
'object12': instance1, # Change here
'u_form': u_form,
'p_form': p_form
}
and in template we can access it as a {{ object12.first_name }}
Upvotes: 0
Reputation: 6404
user request.user
instead of User.objects.get(id=pk)
and request.user.userprofile
instead of UserProfile.objects.get(user_id=pk))
Upvotes: 1