我正在使用Class Based Views
来渲染我的模板。到目前为止,这是我的代码:
Views.py:
class CashFlowView(TemplateView):
template_name = "visualization/cash_flow.html"
def get_context_data(self, **kwargs):
context = super(CashFlowView, self).get_context_data(**kwargs)
return context
def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
return self.render_to_response(context)
def render_to_response(self, context, **response_kwargs):
response_kwargs.setdefault('content_type', self.content_type)
return self.response_class(
request=self.request,
template=self.get_template_names(),
context=context,
using=self.template_engine,
**response_kwargs
)
urls.py:
from django.conf.urls import url
from django.contrib import admin
from .views import CashFlowView
urlpatterns = [
url(r'cash-flow', CashFlowView, name='cashflowView')
]
正如您所看到的,我正在使用templateView
,但我发现这是一个奇怪的初始化错误。有什么想法吗?:
Internal Server Error: /visualization/cash-flow
Traceback (most recent call last):
File "/Users/andy/Documents/venv_analytics/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/andy/Documents/venv_analytics/lib/python3.4/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/andy/Documents/venv_analytics/lib/python3.4/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
TypeError: __init__() takes 1 positional argument but 2 were given
答案 0 :(得分:4)
您需要使用其as_view
方法传递 TemplateView :
urlpatterns = [
url(r'cash-flow', CashFlowView.as_view(), name='cashflowView')
]