简体   繁体   English

Django Forbidden (CSRF cookie not set.): / POST / HTTP/1.1" 403 2864 on Google Colab

[英]Django Forbidden (CSRF cookie not set.): / POST / HTTP/1.1" 403 2864 on Google Colab

so i'm tryin to run my Django web program on Google collab based on this tutorial https://medium.com/@arsindoliya/running-django-on-google-colab-ea9392cdee86所以我尝试根据本教程https://medium.com/@arsindoliya/running-django-on-google-colab-ea9392cdee86Google collab上运行我的 Django web 程序

And i did it the web can running!我做到了 web 可以运行! running web运行 web

But, if i want to make POST it's always error like this但是,如果我想做POST ,它总是这样的错误

Forbidden (CSRF cookie not set.): / [22/Feb/2022 02:13:47] "POST / HTTP/1.1" 403 2864禁止(未设置 CSRF cookie。):/ [22/Feb/2022 02:13:47] "POST / HTTP/1.1" 403 2864

And i'm already try some solution like put the CSRF_COOKIE_SECURE = True inside my settings.py but still not working and i do put the @csrf_token on my form too and it's still not working.我已经尝试了一些解决方案,比如将CSRF_COOKIE_SECURE = True放在我的 settings.py 中,但仍然无法正常工作,我也将@csrf_token放在我的表单上,但它仍然无法正常工作。

Also i wanna try this solution to我也想试试这个解决方案

from django.views.decorators.csrf import csrf_exempt

but i'm still don't understand how to use that.但我仍然不明白如何使用它。

Does anyone has a solution for it?有人有解决方案吗? I'm still new in Django and i made it for my college's final project, so any solution that you guys make really helpful for me.我还是 Django 的新人,我为我大学的期末项目做了这个,所以你们提供的任何解决方案对我都非常有帮助。 Thank you谢谢

2 ways to solve this problem. 2种方法来解决这个问题。

  1. comment out the code about MIDDLEWARE in settings.py like this:将settings.py中关于MIDDLEWARE的代码注释掉,如下:
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
  1. use @csrf_exempt to handle it,like this:使用@csrf_exempt 来处理它,像这样:
from django.views.decorators.csrf import csrf_exempt


@csrf_exempt
def rule_api(request):

    pass

In Django you useing method post you should do add it:在 Django 中,您使用方法发布,您应该添加它:

{% csrf_token %}

you can refer if at: https://docs.djangoproject.com/en/4.0/ref/csrf/您可以参考: https://docs.djangoproject.com/en/4.0/ref/csrf/

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

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