在这种情况下,我练习了许多说明,但没有任何改善。当我要在输入标签内提交值时,页面弹出,显示 403 CSRF验证失败。请求已中止。您能告诉我什么地方不对吗
这是我代码的一部分:
views.py
class ODMS2FixEndView(TemplateView):
template_name = 'ODMS2FixEnd.html'
def get(self, request):
form = HomeForm()
return render(request, self.template_name, {'form': form})
@csrf_exempt
def post(self,request):
if request.method == 'post':
form = HomeForm()
if form.is_valid():
print("YESSSS")
form.update(csrf(request))
text = request.form['post']
print(text)
print(form)
args = {'form': form, 'text': text}
return HttpResponseRedirect('/success/')
else:
print("NOOOO")
return render (request, self.template_name, context=form)
ODMS2FixEnd.html
<!DOCTYPE html>
{% extends 'base.html' %}
<meta name="csrf_token" content="{{ csrf_token }}">
....
<div id="forID" class="container">
<form method="post" id="form1ID" action="">{% csrf_token %}
<input type="text" id="OUtPUTCal">
<button type="submit"> Calculate</button>
</form>
<h2>{{ text }}</h2>
</div>
<script>
.....
document.getElementById("OUtPUTCal").value=TEXT4;
}
......
</script>
urls.py
urlpatterns = [
....
url(r'^myprojects/ODMS2FixEnd', TemplateView.as_view(template_name =
'ODMS2FixEnd.html'), name='ODMS2FixEnd'),
....
form.py
from django import forms
class HomeForm(forms.Form):
post = forms.CharField()