我正在为 Django 的 render_to_response() 编写一个包装函数来添加 CSRF 处理。
逻辑是:
def some_view (request)
dictionary = {'context_param': some_param}
dictionary.update(csrf(request))
# ... view code here
return render_to_response("a_template.html", dictionary)
render_to_response() 具有以下签名:
def render_to_response(*args, **kwargs)
我想编写透明的包装器 - 只需添加一些功能(之前提到过)并保留其他内容。我想我应该这样写:
def csrf_response (request, *args, **kwargs):
# Here I need to somehow extract dictionary from args or kwargs
if dictionary is None:
dictionary = {}
dictionary.update(csrf(request))
# And here I somehow need to pass dictionary into render_to_response() fot the furher processing
return render_to_response(*args, **kwargs)
所以问题是 - 从 args/kwargs 中提取所需参数(然后更改它)并进一步传递它的最佳实践是什么?
BTW 的 render_to_response() 代码让我觉得有点奇怪。这里是:
def render_to_response(*args, **kwargs):
"""
Returns a HttpResponse whose content is filled with the result of calling
django.template.loader.render_to_string() with the passed arguments.
"""
httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)}
return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
如果有人使用所有位置参数调用它会怎样,所以 kwargs 将是空的,但是
mimetype
参数将被指定为最后一个位置参数?看起来在那种情况下它的行为是错误的。
最佳答案
因为如何render_to_response
已实现,指定 mimetype 的唯一方法是使用命名参数。任何作为位置参数传递的内容都将传递给 loader.render_to_string
.
如何提取某些参数并传递其他参数的方法实际上取决于您在做什么。没有一种“正确”的方法可以始终做到这一点。自然地,不同的作者有他们自己偏好的约定。
代替您的评论 # Here I need to somehow extract dictionary from args or kwargs
您可以将 kwargs 直接用作字典,将 args 用作元组,因为它们正是如此。对于 args,您别无选择,只能断言(即假设)每个位置的值的含义。
我个人更喜欢如果您正在编写知道某些参数值的代码,则应在签名中声明它们,如下所示:
def spam(session, name, *args, clear=True, **kwargs):
# do something with session, name, clear
return eggs(name, *args, **kwargs) # if eggs requires name
关于django - Python : Using *args, **kwargs 在包装函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5940856/