2017-07-24 79 views
0

我正在使用Class Based Views來呈現我的模板。這是我到目前爲止的代碼:TypeError:Init只需要1個位置參數

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 

回答

4

你需要通過你的TemplateView使用其as_view方法:

urlpatterns = [ 
    url(r'cash-flow', CashFlowView.as_view(), name='cashflowView') 
] 
+0

OMG我怎麼會錯過呢? :( – anderish

相關問題