render_to_string() 得到了一个意外的关键字参数“状态”

Sum*_*mit 1 django httpresponse python-2.7

我正在运行 django 应用程序并收到以下错误:

TypeError at /login

render_to_string() got an unexpected keyword argument 'status'

Request Method:     GET

Request URL:    http://10.107.44.122:8002/login?next=/

Django Version:     1.7.1

Exception Type:     TypeError

Exception Value: render_to_string() got an unexpected keyword argument 'status'

Exception Location:     /usr/local/lib/python2.7/dist-packages/django/shortcuts.py in render_to_response, line 23

Python Executable:  /usr/bin/python

Python Version:     2.7.6
Run Code Online (Sandbox Code Playgroud)

我能想到错误可能来自哪里的唯一地方是:

render_to_response('login.html', context, status=status, context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)

status应该是预期的关键字 for render_to_response,那么为什么会出现此错误?

Ala*_*air 5

您可以使用render快捷方式而不是render_to_response. 该render方法status在所有版本的 Django 中都接受一个参数。无论如何,这是一种更好的方法,因为您不需要提供RequestContext.

from django.shortcuts import render

def my_view(request):
    context ={'foo': 'bar'}
    status = 200
    return render(request, 'login.html', context, status=status)
Run Code Online (Sandbox Code Playgroud)