Encrpyt URL Id in django
I was trying to excrypt Update URL id to encrpyted format in the url.
from my profile page of a user one edit button is there. that button contains url of the update view.
#My profile page. code
def profile(request):
lst = request.user.id #This line is the problem
l=[]
for i in lst:
i['encrypt_key']=encrypt(i['id'])
i['id']=i['id']
l.append(i)
return render(request, 'profile.html', {'lst':l})
#The update view code
def update(request, id):
id=decrypt(id)
#Update code.
return render(request, 'edit_profile.html', context)
In the profile page for that edit button, since it is an iteratable for loop I coded like this is the Html template
<li class="ctx-item">
<button class="ctx-menu-btn icon-box">
<span class="material-symbols-rounded icon" aria-hidden="true">edit</span>
{% for x in lst %}
<span class="ctx-menu-text"><a href="/update/{{x.encrypt_key}}">Edit</a></span>
{% endfor %}
</button>
</li>
Since three users are there in my database, three edit links are coming. To restrict it in the profile view I have to change the filteration while retriving the user in lst.
If i put request.user.id, It is showing the following error.
TypeError at /profile/
'int' object is not iterable
Please help me to correct this error.
The filtration answer is:
def profile(request):
if request.user.is_authenticated:
lst = User.objects.filter(username=request.user).values('id') #working
l=[]
for i in lst:
i['encrypt_key']=encrypt(i['id'])
i['id']=i['id']
l.append(i)
return render(request, 'profile.html', {'lst':l})
else:
messages.error(request, 'Please Provide the credentials to Login to your account.')
return redirect("login")