Django: AssertionError: `HyperlinkedIdentityField` requires the request in the serializer context. Add `context={'request': request}` when instantiating the serializer.
错误翻译
AssertionError: ' HyperlinkedIdentityField '需要在序列化器上下文中请求。在实例化序列化器时添加' context={'request': request} '。
错误代码:
book_ser = BookSerializer(data=request.data)
修改后:
book_ser = BookSerializer(data=request.data, context={'request': request})
全部代码
# views.py
@api_view(['GET', 'POST'])
def books(request):
if request.method == 'GET':
return Response(data={"msg": "get ok"})
elif request.method == "POST":
book_ser = BookSerializer(data=request.data, context={'request': request}) if book_ser.is_valid():
book_ser.save() return Response(book_ser.data)
return Response(data={'msg': 'error'}, status=status.HTTP_400_BAD_REQUEST)
报了一个新的错误
django.core.exceptions.ImproperlyConfigured: Could not resolve URL for hyperlinked relationship using view name "book-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.
上述问题,我引入HyperlinkedModelSerializer方法,重启服务器已解决
# models.py
from rest_framework import serializers
from RESTSerializer.models import Book
class BookSerializer(serializers.HyperlinkedModelSerializer):
"""
对数据库进行序列化
"""
class Meta:
model = Book
fields = ('b_name', 'b_price')
Django: AssertionError: `HyperlinkedIdentityField` requires the request in the serializer context. Add `context={'request': request}` when instantiating the serializer.的更多相关文章
- Request is not available in this context
部署到新服务器的IIS的时候发现这个错误: Request is not available in this context 解决方案: <system.web> <customEr ...
- 遭遇ASP.NET的Request is not available in this context
如果ASP.NET程序以IIS集成模式运行,在Global.asax的Application_Start()中,只要访问Context.Request,比如下面的代码 var request = Co ...
- scrapy-yield scrapy.Request()不执行、失效、Filtered offsite request to错误 [转]
scrapy错误:yield scrapy.Request()不执行.失效.Filtered offsite request to错误.首先我们在Request()方法里面添加这么一个东东: yiel ...
- 此时servlet中的request和我们在页面jsp中的request 是同一个request.
在tomcat容器启动的时候,jsp页面的内置对象request,response,同样是依赖于tomcat容器中的servlet-api.jar包,这个jar包和我们在web项目中的jar包是一样的 ...
- golang micro client 报错500 {"id":"go.micro.client","code":408,"detail":"call timeout: context deadline exceeded","status":"Request Timeout"}
go micro web端连接services时,第一次访问提示500(broken pipe),排查发现客户端请求services时返回 {"id":"go.micro ...
- Django day26 HyperlinkedIdentityField,序列化组件的数据校验以及功能的(全局,局部)钩子函数,序列化组件的反序列化和保存
一:HyperlinkedIdentityField(用的很少):传三个参数:第一个路由名字,用来反向解析,第二个参数是要反向解析的参数值,第三个参数:有名分组的名字 -1 publish = ser ...
- Django框架:2、静态文件配置、form表单、request对象、pycharm链接数据库、django链接数据库、ORM框架
Django框架 目录 Django框架 一.静态文件配置 1.静态文件 2.配置方法 二.form表单 1.action属性 2.method属性 三.request对象 1.基本用法 四.pych ...
- django error: DisallowedHost: Invalid HTTP_HOST header: ''. You may need to add u'' to ALLOWED_HOST
测试环境: [root@nanx-lli ~]# lsb_release -aLSB Version: :core-4.1-amd64:core-4.1-noarchDistributor ID: C ...
- //可以不保存在session中, 并且前面我保存在request,这里session也可以获取 chain.doFilter(request, response); //只有登录名不为空时放行,防止直接登录 成功的页面
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOE ...
- No mapping found for HTTP request with URI [/jiaoyu/student/add] in DispatcherServlet with name 'SpringMVC'
项目是使用spring MVC (1)在浏览器中访问,后台总报错: No mapping found for HTTP request with URI [/exam3/welcome] in Dis ...
随机推荐
- 性能_1 Jmeter脚本编写
一.万能法 先把项目启动 打开项目接口文档,接口文档: 一般是开发 特别注意事项:当你的接口请求参数为json格式时,一定要写请求头,请求头中一定要有 Content-Type: applicatio ...
- Linux期末佛脚指南
Linux的期末佛脚复习 常用文件操作命令 touch (创建文件) cat (查看文件内容) head (查看文件开头) tail (查看文件结尾) cp (复制文件) mv (移动文件) ls ( ...
- 【CSS】画出宽度为1像素的线或边框
由于多倍的设计图在移动设备上显示时会将设计图进行缩小到视口宽度,而1px的边框没有随着页面进行缩小而导致效果太粗,想要还原设计图1px的显示效果,因此需要一些方法来实现边框宽度小于1px. 实现方法很 ...
- 文心一言 VS chatgpt (3)-- 算法导论2.1
一.以图 2-2 为模型,说明INSERTION-SORT 在数组 A=(31,41,59,26,41,58)上的执行过程. 文心一言: 以图 2-2 为模型,说明INSERTION-SORT 在数组 ...
- 2021-02-03:手写代码:KMP算法。
福哥答案2021-02-03: Knuth-Morris-Pratt 字符串查找算法,简称为 KMP算法,常用于在一个文本串 S 内查找一个模式串 P 的出现位置.这个算法由 Donald Knuth ...
- 2021-07-04:股票问题1。给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。你只能选择某一天 买入这只股票,并选择在未来的某一个不同的日子
2021-07-04:股票问题1.给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格.你只能选择某一天 买入这只股票,并选择在未来的某一个不同的日子 ...
- Django4全栈进阶之路17 项目实战(用户管理):user_add.html用户新增画面设计
1.模块: {% extends 'base.html' %} {% block content %} <div class="card mt-3"> <div ...
- 【GiraKoo】Android Studio调试时,提示port无法打开
Android Studio调试时,提示port无法打开 现象描述 在Android Studio在进行调试时,无法正常运行.App闪退. IDE提示: Error running 'app': Un ...
- PTA L1-064 估值一亿的AI核心代码
PTA L1-064 估值一亿的AI核心代码 有坑!不少 题目链接 题目及分析 题目: 本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是: 1. 无论用户说什么,首先把对方说 ...
- PHP反序列化常用魔术方法
PHP反序列化 php序列化(serialize):是将变量转换为可保存或传输的字符串的过程 php反序列化(unserialize):就是在适当的时候把这个字符串再转化成原来的变量使用 PHP反序列 ...