Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comments.admin中管理后台评论管理模型中点击文章显示已被删除或不存在问题 #736

Open
4 of 6 tasks
JOYCAT-Q opened this issue Jul 19, 2024 · 0 comments

Comments

@JOYCAT-Q
Copy link

我确定我已经查看了 (标注[ ][x])


我要申请 (标注[ ][x])

  • BUG 反馈
  • 添加新的特性或者功能
  • 请求技术支持
    comments.admin中的class CommentAdmin(admin.ModelAdmin)
class CommentAdmin(admin.ModelAdmin):
    ...省略...

    def link_to_article(self, obj):
        info = (obj.author._meta.app_label, obj.author._meta.model_name)
        link = reverse('admin:%s_%s_change' % info, args=(obj.article.id,))
        # print(link)
        return format_html(
            u'<a href="%s">%s</a>' % (link, obj.article.title))

BUG表现:当使用管理员账号在评论应用管理后台点击评论所属文章进行跳转不成功
屏幕截图 2024-07-19 204904
点击跳转后:
屏幕截图 2024-07-19 204918

失效原因:
我们将print(link)打开,可以在终端看到如下:
屏幕截图 2024-07-19 204757

此处我们需要的是文章的相关详细,而并非用户信息
解决方法:在def link_to_article(self, obj):中修改info信息的获取

class CommentAdmin(admin.ModelAdmin):
    ...省略...
    # 其余不变
    def link_to_article(self, obj):
        # 修改info,从文章模型中获取信息
        info = (obj.article._meta.app_label, obj.article._meta.model_name)
        link = reverse('admin:%s_%s_change' % info, args=(obj.article.id,))
        # print(link)
        return format_html(
            u'<a href="%s">%s</a>' % (link, obj.article.title))

即可解决问题,解决后输出如下:
屏幕截图 2024-07-19 205006
点击后即可正常跳转:
屏幕截图 2024-07-19 205035

当前大体环境配置:
Django==4.2.14
Windows10
MySQL==8.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant