必要に応じてさまざまなモデルで動作できる、より一般的なバージョンの Django クラス ベースの詳細ビューを実装しようとしています。アイデアは、次のように URL のパラメーターを使用してモデルを設定することでした。
url(r'^product/view/(?P<bought_in_control_panel_id>\d+)/(?P<item_uuid>'+uuid_re+'/)',
GenericModelDetailView().as_view(pk_url_kwarg='item_uuid',
template_name='suppliers/products/view_product.html')
, name='view_product'),
これは私が使用しているクラスですが、django はオブジェクト属性を持たないクラスについて不平を言っています。
class GenericModelDetailView(DetailView):
def __init__(self, *args, **kwargs):
self.model = get_model_for_bought_in_control_panel(super(GenericModelDetailView, self).get_context_data(**kwargs)['bought_in_control_panel_id'])
super(GenericModelDetailView, self).__init__(**kwargs)
スタックトレース:
File "django/core/handlers/base.py", line 89, in get_response
response = middleware_method(request)
File "django/middleware/common.py", line 67, in process_request
if (not _is_valid_path(request.path_info, urlconf) and
File "django/middleware/common.py", line 154, in _is_valid_path
urlresolvers.resolve(path, urlconf)
File "django/core/urlresolvers.py", line 342, in resolve
return get_resolver(urlconf).resolve(path)
File "django/core/urlresolvers.py", line 252, in resolve
sub_match = pattern.resolve(new_path)
File "django/core/urlresolvers.py", line 250, in resolve
for pattern in self.url_patterns:
File "django/core/urlresolvers.py", line 279, in _get_url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "django/core/urlresolvers.py", line 274, in _get_urlconf_module
self._urlconf_module = import_module(self.urlconf_name)
File "django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/home/jsoft/jmsdirectory/srv/jmsdirectory/../jmsdirectory/suppliers/urls.py", line 29, in <module>
GenericModelDetailView().as_view(pk_url_kwarg='item_uuid',
File "jmsdirectory/suppliers/products.py", line 79, in __init__
self.model = get_model_for_bought_in_control_panel(super(GenericModelDetailView, self).get_context_data(**kwargs)['bought_in_control_panel_id'])
File "django/views/generic/detail.py", line 90, in get_context_data
context_object_name = self.get_context_object_name(self.object)