stacklens / dusai-blog

杜赛的博客的评论区。详情见 Issue。
2 stars 0 forks source link

article/120/ #86

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

Django-Vue搭建个人博客:用户管理 - 杜赛的博客

https://www.dusaiphoto.com/article/120/

sass1s commented 3 years ago

作者,您好,本节按照你的教程操作后,发现一个问题:网页端右上角的”Log in“不能登录了。 请解惑。

stacklens commented 3 years ago

作者,您好,本节按照你的教程操作后,发现一个问题:网页端右上角的”Log in“不能登录了。 请解惑。

啥信息都没有,我咋解惑

Icecream-2018 commented 3 years ago

作者,您好,本节按照你的教程操作后,发现一个问题:网页端右上角的”Log in“不能登录了。 请解惑。

你在JWT身份验证那一章 修改配置文件,使 JWT 为默认的验证机制,所以DRF自带的登录自然就失效了,想继续使用Log in的话就注释掉settings里的DEFAULT_AUTHENTICATION_CLASSES

xzg3134 commented 3 years ago

博主,我在发表文章的时候存在后端问题,当我使用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.

xzg3134 commented 3 years ago

我的views基本都是用类视图,所以在restframework的router没有注册接口,莫非是没有注册接口不能使用jwt验证吗

volvo007 commented 1 year ago

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 thelookup_fieldattribute on this field. 有点困惑……

直接照抄代码也没反应

Herbert-CN commented 1 year ago

直接抄也是同样的错误:

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.

Herbert-CN commented 1 year ago

上面这个关于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']
Herbert-CN commented 1 year ago

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.

一直报这个错,有没有人来帮忙看看呢

Herbert-CN commented 1 year ago

在 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,去掉特殊字符,就没有这个问题了。