I am rendering current user data in hidden input so user don’t need to retype data again if he is login. User can inspect html page and see those hidden input data. He can also change hidden input data which one kind of security risk for my website. Such as I have an hidden input where user email and name rendering like this
<input type="hidden" name="name" class="form-control" placeholder="Your Name" value="Jhone" required="">
User can inspect the html and change the value then my forms submitting with new updated value. is there any way to stop submitting forms if value is changed. here is my code:
#html template
{% for i in currentuser_details %}
{%for y in user_profile%}
<input type="hidden" name='userprofile' value="{{y.id}}">
{%endfor%}
<input type="hidden" name="name" class="form-control" placeholder="Your Name" value="{{i.first_name}}" required>
<input type="hidden" name="email" class="form-control" placeholder="Enter email"value="{{ i.email }}" required>
{%endfor%}
#html hidden input rendering data
<input type="hidden" name="name" class="form-control" placeholder="Your Name" value="Jhone" required="">
<input type="hidden" name="email" class="form-control" placeholder="Your Name" value="Jhone@gmail.com" required="">
<input type="hidden" name='userprofile' value="1">
<input type="hidden" name="parent" id="parent_id" value="95">
The most import fields for me userprofile and parent . I want to prevent forms submitting if any hidden value change.
froms.py
class CommentFrom(forms.ModelForm):
captcha = CaptchaField()
class Meta:
model = BlogComment
fields = ['name','email','comment','parent','sno','blog','user','userprofile']
views.py
if request.method == "POST":
if comment_form.is_valid():
isinstance = comment_form.save(commit=False)
if request.user.is_authenticated:
isinstance.user = request.user
elif not request.user.is_authenticated:
User = get_user_model()
isinstance.user = User.objects.get(username='anonymous_user')
isinstance.blog = blog
isinstance.save()
messages.add_message(request, messages.INFO, 'Your Comment Pending for admin approval')
return redirect('blog:blog-detail',slug=blog.slug)
else:
messages.add_message(request, messages.INFO, "your comment didn't submitted. please submit again ")
else:
comment_form = CommentFrom()
I think I need to be add validation in my froms.py or views.py but how to do that for hidden input and hidden foreignkey fields I don’t know.