Return 关键字参数与 None 值到 django 3.2 中的视图
Return keyword arguments with None values to the views in django 3.2
RegexPattern
, used by re_path()
, no longer returns keyword arguments with None
values to be passed to the view for the optional named groups that are missing.
最近从 Django 2.2 升级到 3.2,之后我遇到了一个问题,我怀疑上面提到的变更日志。
问题是在访问 URL 定义的模式时,我在视图中访问 URL 参数作为关键字参数(使用 get_context_data(...)
)时得到 KeyError
re_path()
.
FYI, just to verify I rolled back to Django 2.2 and checked on the context data from the view and could see that the required Key in the self.kwargs
dict was set to None
在Django 3.2中,有什么方法可以将return关键字参数和None值传递给视图吗? ?
根据我的理解,您有一个形式的模式,r'(?P<group>pattern)?'
即您设置为可选的捕获组,在这种情况下通常会 return None
如果没有'匹配任何内容。
如果您真的想这样做而不是重构您的代码,您可以从 RegexPattern
继承并通过从 source code [GitHub]:
复制它来覆盖 match
from django.urls.conf import _path
from django.urls.resolvers import RegexPattern
from functools import partial
class MyRegexPattern(RegexPattern):
def match(self, path):
match = self.regex.search(path)
if match:
# If there are any named groups, use those as kwargs, ignoring
# non-named groups. Otherwise, pass all non-named arguments as
# positional arguments.
kwargs = match.groupdict()
args = () if kwargs else match.groups()
# Commented line below from source code
# kwargs = {k: v for k, v in kwargs.items() if v is not None}
return path[match.end():], args, kwargs
return None
# Make the re_path function
re_path = partial(_path, Pattern=MyRegexPattern)
虽然更好的解决方案是指定多个 url 模式而不是一个模式,或者修复您的视图以允许此类情况发生,而不是使用此解决方法。
假设您有这样的模式:
re_path(r'^prefix(?P<group>pattern)?suffix/$', views.some_view),
您可以编写两种模式,例如:
re_path(r'^prefix(?P<group>pattern)suffix/$', views.some_view),
re_path(r'^prefixsuffix/$', views.some_view, kwargs={'group': None}),
如果您之前有一个基于函数的视图,例如:
def some_view(request, group):
...
只需更改它,使组具有默认值 None
:
def some_view(request, group=None):
...
如果在基于 class 的视图中您正在编写 self.kwargs['group']
而不是编写 self.kwargs.get('group')
.
RegexPattern
, used byre_path()
, no longer returns keyword arguments withNone
values to be passed to the view for the optional named groups that are missing.
最近从 Django 2.2 升级到 3.2,之后我遇到了一个问题,我怀疑上面提到的变更日志。
问题是在访问 URL 定义的模式时,我在视图中访问 URL 参数作为关键字参数(使用 get_context_data(...)
)时得到 KeyError
re_path()
.
FYI, just to verify I rolled back to Django 2.2 and checked on the context data from the view and could see that the required Key in the
self.kwargs
dict was set toNone
在Django 3.2中,有什么方法可以将return关键字参数和None值传递给视图吗? ?
根据我的理解,您有一个形式的模式,r'(?P<group>pattern)?'
即您设置为可选的捕获组,在这种情况下通常会 return None
如果没有'匹配任何内容。
如果您真的想这样做而不是重构您的代码,您可以从 RegexPattern
继承并通过从 source code [GitHub]:
match
from django.urls.conf import _path
from django.urls.resolvers import RegexPattern
from functools import partial
class MyRegexPattern(RegexPattern):
def match(self, path):
match = self.regex.search(path)
if match:
# If there are any named groups, use those as kwargs, ignoring
# non-named groups. Otherwise, pass all non-named arguments as
# positional arguments.
kwargs = match.groupdict()
args = () if kwargs else match.groups()
# Commented line below from source code
# kwargs = {k: v for k, v in kwargs.items() if v is not None}
return path[match.end():], args, kwargs
return None
# Make the re_path function
re_path = partial(_path, Pattern=MyRegexPattern)
虽然更好的解决方案是指定多个 url 模式而不是一个模式,或者修复您的视图以允许此类情况发生,而不是使用此解决方法。
假设您有这样的模式:
re_path(r'^prefix(?P<group>pattern)?suffix/$', views.some_view),
您可以编写两种模式,例如:
re_path(r'^prefix(?P<group>pattern)suffix/$', views.some_view),
re_path(r'^prefixsuffix/$', views.some_view, kwargs={'group': None}),
如果您之前有一个基于函数的视图,例如:
def some_view(request, group):
...
只需更改它,使组具有默认值 None
:
def some_view(request, group=None):
...
如果在基于 class 的视图中您正在编写 self.kwargs['group']
而不是编写 self.kwargs.get('group')
.