Django render_to_response() with parameter names specified
I am not 100% sure whether this is a Django or a Python question, but I think it's got something to do with Django.
As I am new to the Django world, I am trying to establish the good habit of explicitly putting down parameter names when making function calls.
In the case of render_to_response(), I would have something like the following:
render_to_response(template='lend_borrow/MyAccount_mod.html',
dictionary={'user_form': user_form, 'profile_form': profile_form, 'profile': profile_obj},
context_instance=RequestContext(request))
But with that, I got an error, "render_to_string() got an unexpected keyword argument 'template'".
In order for that render_to_response() to work in my view function, I had to change it to
render_to_response('lend_borrow/MyAccount_mod.html',
{'user_form': user_form, 'profile_form': profile_form, 'profile': profile_obj},
RequestContext(request))
OR
render_to_response('lend_borrow/MyAccount_mod.html',
{'user_form': user_form, 'profile_f开发者_StackOverflow中文版orm': profile_form, 'profile': profile_obj},
context_instance=RequestContext(request))
QUESTION: Why is the first approach of calling render_to_response() giving me an error?
render_to_response
is a wrapper around HttpResponse
. However, HttpResponse
takes rendered content, not a template name. Therefore, render_to_response
calls render_to_string
first, and the parameter in render_to_string
is template_name
not template
.
However, prefixing parameter names isn't necessarily "good practice". Good practice is to follow convention and convention is to not use parameter names in render_to_response
, except for context_instance
.
The parameter name is template_name
, not template
.
精彩评论