我只是在使用@ https://docs.graphene-python.org/projects/django/en/latest/tutorial-plain/教程来理解Django 2的graphQL和graphene。
我已经有一段时间没有使用Django了,但是这个tut似乎是针对Django 11的。尝试这样的GraphQL查询时,我得到的是CSRF_FAILURE_VIEW:
query {
allIngredients {
id
name
}
}
底层json固定看起来像这样:
[{"model": "ingredients.category", "pk": 1, "fields": {"name": "Dairy"}}, {"model": "ingredients.category", "pk": 2, "fields": {"name": "Meat"}}, {"model": "ingredients.ingredient", "pk": 1, "fields": {"name": "Eggs", "notes": "Good old eggs", "category": 1}}, {"model": "ingredients.ingredient", "pk": 2, "fields": {"name": "Milk", "notes": "Comes from a cow", "category": 1}}, {"model": "ingredients.ingredient", "pk": 3, "fields": {"name": "Beef", "notes": "Much like milk, this comes from a cow", "category": 2}}, {"model": "ingredients.ingredient", "pk": 4, "fields": {"name": "Chicken", "notes": "Definitely doesn't come from a cow", "category": 2}}]
请原谅一个菜鸟,但是Django 2必须有所改变吗?我需要应用其他设置吗?
在settings.py中,我有:
GRAPHENE = {
'SCHEMA': 'cookbook.schema.schema'
}
我的文件夹结构与tut略有不同,因为我的配料应用程序未嵌套在我的食谱应用程序中。食谱应用程序是这样的主要应用程序:
答案 0 :(得分:0)
简短答案:将csrf_exempt
添加到您的urls.py [0]
长答案:
CSRF保护在Django [1]中并不是什么新鲜事物,通常将{{csrf_token}}
添加到模板中,然后将其(或csrftoken
cookie)用作其中的X-CSRFToken
标头您对服务器的POST请求。
但是,如果您正在使用Postman或类似产品进行测试,则没有有效的CSRF令牌可以使用,因此,最好的选择是使视图不再期望CSRF令牌,这就是上述csrf_exempt
函数的作用。>
[0] https://github.com/graphql-python/graphene-django/issues/61#issuecomment-261199128