简体   繁体   English

CSRF 令牌验证失败

[英]CSRF Token Verification Failed

I see this question has been asked many times - but I cannot determine what specifically I am doing wrong.我看到这个问题已经被问过很多次了 - 但我无法确定我做错了什么。 Could anyone help me identify what is wrong?谁能帮我找出问题所在? I have the CSRF token there but I feel I must be doing something wrong in the view?我在那里有 CSRF 令牌,但我觉得我一定在视图中做错了什么?

views.py视图.py

    if request.method == "POST":
        form = RegistrationForm(request.POST)


        if form.is_valid():

            user = form.save(commit=False)

            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user.set_password(password)
            user.save()

            user = authenticate(username=username, password=password)
            auth_login(request, user)

            return render('test.html')


    else:
        form = RegistrationForm()

        return render(request, 'register.html', {'form': form})

register.html注册.html

  <form method="POST" class="border border-light p-5">

  {% csrf_token %}

    <p class="h4 mb-4 text-center">Registration</p>

    {{form.username}}

    {{form.email}}

    {{form.password}}

    <button type="submit" class="btn btn-default btn-block my-4" type="submit">Register</button>


  </form>

Did you use CSRF tag in your view?您是否在您的视图中使用了 CSRF 标签? You should import:您应该导入:

from django.views.decorators.csrf import csrf_exempt

then add csrf_excemp before your function definition like this:然后在你的函数定义之前添加 csrf_excemp ,如下所示:

@login_required
@csrf_exempt
def update(request):

I hope it solve your problem我希望它能解决你的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM