メソッド 'as_view()'内に独自の属性 'fileName'を追加したい
path('dialogs/', CodeResponseView.as_view(fileName='Dialogs.py')),
Djangoは私にarrorを与えます:
TypeError: CodeResponseView() received an invalid keyword 'fileName'. as_view only accepts arguments that are already attributes of the class.
エラーはあなたが何をすべきかを正確に教えてくれます:
as_viewは、すでにクラスの属性である引数のみを受け入れます
したがってfileName
、属性としてクラスに追加します。
class CodeResponseView(View):
fileName = ''
# rest of view code can now use the fileName attribute
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['file'] = self.fileName
return context
これで、に渡さfileName
れるすべてのURLパターンas_view()
が機能します。
path('dialogs/', CodeResponseView.as_view(fileName='Dialogs.py')),
path('alerts/', CodeResponseView.as_view(fileName='Alerts.py')),
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加