フォームを作成していて、変更してはならないいくつかの非表示の値をバインドして渡しているとしましょう。私の質問は、悪意のあるユーザーがこの非表示の値を変更したかどうかをテストするにはどうすればよいですか? フォーム内のバインドされたデータと初期の違いが正確に何であるかはわかりません。
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>
前もって感謝します!