Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: as_view() takes 1 positional argument but 2 were given

I got an error and I'm not sure what it means:

TypeError at / as_view() takes 1 positional argument but 2 were given

from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin

class Employee(models.Model):
    user = models.OneToOneField(User)
    full_name = models.CharField(max_length=50)
    phone_number = models.CharField(max_length=12)
    company = models.CharField(max_length=100)
    department = models.CharField(max_length=50)
    occupation = models.CharField(max_length=50)

    class Meta:
        verbose_name_plural = 'employees'

admin.site.register(Employee)

views.py

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

class EmployeeTemplateView(TemplateView):
    model = Employee.objects.all()
    template_name = 'index.html'
    context_object_name = 'employee_view'

urls.py

from django.conf.urls import url, include
from web_app import routers
from django.contrib import admin
from . import views
from web_app.views import EmployeeTemplateView
admin.autodiscover()

router = routers.DefaultRouter()
router.register(r'employee', views.EmployeeTemplateView, "Employee")

urlpatterns = ['',
               url(r'^$', EmployeeTemplateView.as_view({'emp' : 'employees'})),
               ]

What am I doing wrong?

Thank you in advance!

like image 550
Xhens Avatar asked Jul 01 '16 18:07

Xhens


2 Answers

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

like image 198
felix Avatar answered Sep 19 '22 20:09

felix


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

EmployeeTemplateView.as_view(emp='employees')
like image 39
Daniel Roseman Avatar answered Sep 22 '22 20:09

Daniel Roseman