Open utterances-bot opened 3 years ago
作者,您好,本节按照你的教程操作后,发现一个问题:网页端右上角的”Log in“不能登录了。 请解惑。
作者,您好,本节按照你的教程操作后,发现一个问题:网页端右上角的”Log in“不能登录了。 请解惑。
啥信息都没有,我咋解惑
作者,您好,本节按照你的教程操作后,发现一个问题:网页端右上角的”Log in“不能登录了。 请解惑。
你在JWT身份验证那一章 修改配置文件,使 JWT 为默认的验证机制,所以DRF自带的登录自然就失效了,想继续使用Log in的话就注释掉settings里的DEFAULT_AUTHENTICATION_CLASSES
博主,我在发表文章的时候存在后端问题,当我使用JWT在restframework的api页面post的时候,调用 def perform_create(self, serializer): serializer.save(author=self.request.user)报错Cannot assign "<django.contrib.auth.models.AnonymousUser object at 0x000001F0DCA0E040>": "Article.author" must be a "User" instance.就是获取不到我的用户信息,直观地就是在restframework登录刷新后右上角的login依然存在,当然注释掉JWT是可以post.
我的views基本都是用类视图,所以在restframework的router没有注册接口,莫非是没有注册接口不能使用jwt验证吗
在 serializers.py
中 使用 lookup_field='username'
和 views.py
中 也使用 lookup_field='username'
但是报错说 Could not resolve URL for hyperlinked relationship using view name "user-detail". You may have failed to include the related model in your API, or incorrectly configured the
lookup_fieldattribute on this field.
有点困惑……
直接照抄代码也没反应
直接抄也是同样的错误:
ImproperlyConfigured at /api/user/
Could not resolve URL for hyperlinked relationship using view name "user-detail". You may have failed to include the related model in your API, or incorrectly configured the lookup_field
attribute on this field.
上面这个关于lookup_field的问题查了下应该是版本的问题,引用官方最新的更新
Changes to HyperlinkedModelSerializer. The view_name and lookup_field options have been moved to PendingDeprecation. They are no longer required, as you can use the extra_kwargs argument instead:
class MySerializer(serializer.HyperlinkedModelSerializer): class Meta: model = MyModel fields = ['url', 'email', 'notes', 'is_admin'] extra_kwargs = { 'url': {'lookup_field': 'uuid'} } Alternatively, specify the field explicitly on the serializer class:
class MySerializer(serializer.HyperlinkedModelSerializer): url = serializers.HyperlinkedIdentityField( view_name='mymodel-detail', lookup_field='uuid' )
class Meta:
model = MyModel
fields = ['url', 'email', 'notes', 'is_admin']
ImproperlyConfigured at /api/user/
Could not resolve URL for hyperlinked relationship using view name "user-detail". You may have failed to include the related model in your API, or incorrectly configured the lookup_field
attribute on this field.
一直报这个错,有没有人来帮忙看看呢
在 serializers.py 中 使用 lookup_field='username' 和 views.py 中 也使用 lookup_field='username' 但是报错说 Could not resolve URL for hyperlinked relationship using view name "user-detail". You may have failed to include the related model in your API, or incorrectly configured the lookup_field attribute on this field. 有点困惑……
这个问题试验了我好长时间,终于找到问题所在了。 应该是你的username里面有特殊字符导致的,比如我的系统用户是herbert.zhou, 中间有个点,只要点击api/user连接就出现你上面说的问题。把username改成herbert,去掉特殊字符,就没有这个问题了。
Django-Vue搭建个人博客:用户管理 - 杜赛的博客
https://www.dusaiphoto.com/article/120/