使用 cms.api.create_page 时出错“UserWarning: 创建新的 PageContent 对象时未提供用户。”

问题描述 投票:0回答:1

当我使用 Django CMS API“create_page”函数时,出现以下错误:

UserWarning: No user has been supplied when creating a new PageContent object. No version could be created. Make sure that the creating code also creates a Version objects or use PageContent.objects.with_user(user).create(...)
  page_content = PageContent.objects.with_user(user).create(

运行“create_page”函数时,它会创建一个页面,然后运行“create_page_content”函数,该函数会在以下代码行中引发错误:

user = getattr(_thread_locals, "user", "unknown user")
page_content = PageContent.objects.with_user(user).create(
    language=language,
    title=title,
    menu_title=menu_title,
    page_title=page_title,
    redirect=redirect,
    meta_description=meta_description,
    page=page,
    created_by=created_by,
    changed_by=created_by,
    soft_root=soft_root,
    in_navigation=in_navigation,
    template=template,
    limit_visibility_in_menu=limit_visibility_in_menu,
    xframe_options=xframe_options,
)

这是 Django CMS 内置 API 的一部分,所以我无法真正更改它。我可以复制“create_page”和“create_page_content”函数并使用编辑后的副本,但这似乎是一个相当强力的解决方案,我可能只是错过了一些东西。

我的相关代码:

from cms.api import create_page
from cms.constants import TEMPLATE_INHERITANCE_MAGIC

new_page = create_page(title='Example', template=TEMPLATE_INHERITANCE_MAGIC, language='en')

django版本:5.0.1 django-cms 版本:4.1.0 python版本:3.12.2

python django django-cms
1个回答
0
投票

已解决:我需要添加一个“User”对象作为“created_by”参数,例如:

from cms.api import create_page
from cms.constants import TEMPLATE_INHERITANCE_MAGIC
from django.contrib.auth.models import User

new_page = create_page(title='Example', template=TEMPLATE_INHERITANCE_MAGIC, language='en', created_by=User.objects.get(username='user'))

默认情况下,“created_by”参数设置为字符串“python-api”,但不起作用。
api 中的注释将用户检查称为“丑陋的权限黑客”,因此我认为开发人员知道这是一个问题。

© www.soinside.com 2019 - 2024. All rights reserved.