anderish anderish - 3 years ago 150
Python Question

TypeError: Init Only takes 1 positional Argument

I am using

Class Based Views
to render my templates. Here is my code so far:

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')
]


As you can see I am using the
templateView
but I get this a weird init error. Any ideas?:

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

Answer Source

You need to pass your TemplateView using its as_view method:

urlpatterns = [
    url(r'cash-flow', CashFlowView.as_view(), name='cashflowView')
]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download