假设我正在创建一个表单,并且我正在通过绑定传递一些隐藏值,这些值不能更改。我的问题是如何测试恶意用户是否更改了此隐藏值?我不确定表单中的绑定数据到底是什么以及初始数据之间的区别。
在 Django 的forms.py中有一个名为changed_data的属性,但我不知道是否有帮助。
演示代码:
表格.py
class ConfirmForm(forms.Form):
client_id = forms.CharField(widget=forms.HiddenInput())
identifier = forms.CharField(widget=forms.HiddenInput())
def clean(self):
# Maybe here the validation process of cliend_id and identifier like:
clean_client_id = self.cleaned_data.get('client_id')
clean_identifier = self.cleaned_data.get('identifier')
if last_client_id == clean_client_id and
last_identifier == clean_identifier:
return self.cleaned_data
else:
raise forms.ValidationError("False data.")
视图.py
def form_confirm_handler(request):
if request.method == 'POST':
form = ConfirmForm(request.POST)
if form.is_valid():
#Do something...
return redirect('home:index')
#The following values are not fixed. Are generated variables!
bound_data = {'client_id':'123456','identifier':'wuiy5895'}
form = ConfirmForm(bound_data)
return render(request, 'client/theform.html', {'form':form})
html模板
<form action="{% url 'client:confirm' %}" method="post">
<p>Do you really want to proceed?</p>
{% csrf_token %}
{{ form.client_id }}
{{ form.identifier }}
<button id="submit" type="submit" name="submit" class="btn" value="accept">Accept</button>
<button id="cancel" type="submit" name="cancel" class="btn btn-primary" value="cancel">Cancel</button>
</form>
提前致谢!