[Fixed]-TypeError: as_view() takes 1 positional argument but 2 were given

13👍

as_view doesn’t take positional arguments, it takes keyword arguments.

EmployeeTemplateView.as_view(emp='employees')

63👍

Make sure that you put “as_view()” as such. Not “as_view”. I made such a big mistake

👤felix

1👍

Since it looks like you are just trying to pass the employees variable from the view to the template, you can just send it this way:

views.py

from django.views.generic import ListView
from web_app.models import Employee

class EmployeeListView(ListView):
    model = Employee
    template_name = 'index.html'
    context_object_name = 'employees'

urls.py

urlpatterns = [
               url(r'^$', EmployeeListView.as_view(), name="employees"),
               ]

Then you can use the context_object_name in the template as so:

index.html

<div>{% for employee in employees %} {{ employee }} {% endfor %}</div>
👤Hybrid

Leave a comment