CSRF 验证失败。请求中止。我保留了 ta 标签 % csrf_token % 仍然得到这个
Posted
技术标签:
【中文标题】CSRF 验证失败。请求中止。我保留了 ta 标签 % csrf_token % 仍然得到这个【英文标题】:CSRF verification failed. Request aborted. I kept the ta tag % csrf_token % still iam getting thisCSRF 验证失败。请求中止。我保留了 ta 标签 % csrf_token % 仍然得到这个 【发布时间】:2020-09-02 20:37:44 【问题描述】:我有一个关于 csrf 令牌不正确或无效的问题。我在表格上方写了 % csrf_token % 仍然遇到这个问题。任何人都可以帮助我吗??
我的 html 文件代码。
<h4>register page</h4>
<form method="POST" action="">
% csrf_token %
form.as_p
<input type="submit" name="Create User">
</form>
my view.py code
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.forms import inlineformset_factory
from django.contrib.auth.forms import UserCreationForm
from .models import *
from .forms import OrderForm
from .filters import OrderFilter
def registerPage(request):
form = UserCreationForm
if request.method=='POST':
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
context='form':form
return render(request, 'accounts/register.html', context)
def loginPage(request):
context=
return render(request, 'accounts/login.html', context)
def home(request):
orders = Order.objects.all()
customers = Customer.objects.all()
total_customers= customers.count()
total_orders = orders.count()
delivered = orders.filter(status='Delivered').count()
pending = orders.filter(status='Pending').count()
context = 'orders':orders,'customers':customers, 'total_customers': total_customers,
'total_orders':total_orders,'delivered':delivered,'pending':pending
return render(request, 'accounts/index.html', context)
def products(request):
products = Product.objects.all()
return render(request, 'accounts/products.html', 'products':products)
def customers(request, pk):
customer = Customer.objects.get(id=pk)
orders = customer.order_set.all()
orders_count =orders.count()
myFilter= OrderFilter(request.GET, queryset=orders)
orders = myFilter.qs
context='customer': customer, 'orders': orders, 'orders_count':orders_count,'myFilter':myFilter
return render(request, 'accounts/customers.html', context)
def createOrder(request, pk):
OrderFormSet = inlineformset_factory(Customer, Order, fields=('product', 'status'), extra=10 )
customer = Customer.objects.get(id=pk)
formset = OrderFormSet(queryset=Order.objects.none(),instance=customer)
#form = OrderForm(initial='customer':customer)
if request.method == 'POST':
#print('Printing POST:', request.POST)
#form = OrderForm(request.POST)
formset = OrderFormSet(request.POST, instance=customer)
if formset.is_valid():
formset.save()
return redirect('/')
context = 'form':formset
return render(request, 'accounts/order_form.html', context)
def updateOrder(request, pk):
order=Order.objects.get(id=pk)
form = OrderForm(instance=order)
if request.method == 'POST':
form= OrderForm(request.POST, instance=order)
if form.is_valid():
form.save()
return redirect('/')
context='form':form
return render(request,'accounts/order_form.html', context)
def deleteOrder(request, pk):
order=Order.objects.get(id=pk)
if request.method=="POST":
order.delete()
return redirect('/')
context='item':order
return render(request, 'accounts/delete.html', context)
enter code here
这些是我的文件,除了我在应用 csrf 令牌后可以提供给您之外的任何其他文件,我仍然收到错误表单未提交。
【问题讨论】:
【参考方案1】:表单中的 action 属性为空:
action=""
你需要包含一个动作,例如:
action="/somefolder/someviewname/"
或:
action="% url 'name_of_app:name_of_view' %"
【讨论】:
以上是关于CSRF 验证失败。请求中止。我保留了 ta 标签 % csrf_token % 仍然得到这个的主要内容,如果未能解决你的问题,请参考以下文章