render_to_string() got an unexpected keyword argument 'status'

拥有回忆 提交于 2019-12-24 23:00:05

问题


I am running a django application and getting the following error:

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

The only place I could think of where the error might be coming from is :

render_to_response('login.html', context, status=status, context_instance=RequestContext(request))

status is supposed to be expected keyword for render_to_response, then why this error?


回答1:


You could use the render shortcut instead of render_to_response. The render method does take a status argument in all versions of Django. It's a nicer method to use anyway, because you don't need to supply a RequestContext.

from django.shortcuts import render

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



回答2:


The status argument is accepted in Django 1.8, not in 1.7. Compare the documentation for the render_to_response methods in 1.8 and 1.7.



来源:https://stackoverflow.com/questions/33212565/render-to-string-got-an-unexpected-keyword-argument-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!