【问题标题】:RemovedInDjango110Warning: The context_instance argument of render_to_string is deprecatedRemovedInDjango110警告:不推荐使用 render_to_string 的 context_instance 参数
【发布时间】:2016-11-15 03:24:12
【问题描述】:

对于其中一个应用程序,我在使用管理面板的 Django 1.9.x 项目中重载了“删除选定对象”方法。为此,我有一个类似这样的代码:

from django.contrib.admin import helpers
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext

class MAdmin(admin.ModelAdmin):
    actions = ['delete_selected']

    def delete_selected(self, request, queryset):
        if 'apply' in request.POST:
            # User has confirmed deletion of items
            return HttpResponseRedirect(request.get_full_path())
        else:
            # User must confirm if they wish to delete selected items
            return render_to_response('admin/confirm_delete.html', { 'queryset': queryset, 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME }, context_instance=RequestContext(request))

最后一行在控制台中产生以下警告:

.../virtualenv/lib/python2.7/site-packages/django/shortcuts.py:45: RemovedInDjango110Warning: render_to_string 的 context_instance 参数已弃用。使用=使用)

我试图找到一些资源来解释如何根据 1.10.x 版本“更新”语法,但我一直无法做到。

here 已经提出了类似的问题,但它略有不同,因为 OP 明确调用 render_to_string 而我没有。

我应该如何更新上面的调用以使警告消失?

【问题讨论】:

    标签: python django django-templates django-admin


    【解决方案1】:

    是的,修复它的“正确”方法是更新它。根据documentation, they recommend using render()

    自 1.8 版起已弃用: 不推荐使用 context_instance 参数。改用 render() 函数,它总是使 RequestContext 可用。

    如果您确实想禁止显示此警告(极不推荐),您可以使用SILENCE_SYSTEM_CHECK 设置。

    More on the system check can be found here

    【讨论】:

    • 请注意,这是 Python 警告,而不是 Django 系统检查。您需要使用warnings module 添加过滤器并忽略警告。
    • 除非真的不可避免,否则我宁愿不让警报静音,因为这基本上是在推迟问题。我会试一试然后回来。
    • 正确,抑制这些错误会使维护升级周期成为一场噩梦。
    • 成功了。只需将request 传递给render() 调用并删除context_instance 参数就可以了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    • 2021-10-09
    • 2020-05-14
    • 1970-01-01
    • 2020-02-23
    • 2019-09-21
    • 2016-09-19
    相关资源
    最近更新 更多