0

Getting ERROR:TypeError at /test/

'str' object is not callable

form view:

class Test(FormView):
template_name = "pkm_templates/main.html"
 form_class = "Testform"
 success_url = '/thanks/'
 def form_valid(self,form):
    title = form.cleaned_data.get("title")
    fkey = form.cleaned_data.get("fkey")
    instance = Buildkb.objects.create(title=title,fkey=fkey)
    for user in fkey:
        instance.fkey.add(user)
        instance.save()
    return redirect("/")

URL:

path('test/',views.Test.as_view(),name="test")

for another model i used approch formview , modelform and url same has this one no exception.

2
  • 2
    Can you post your full error? Commented May 11, 2018 at 12:03
  • what kind of field is fkey? Commented May 11, 2018 at 12:07

1 Answer 1

2

As documented, form_class is supposed to be your form class object, not it's name:

# assuming TestForm is defined in "yourapp.forms"
from yourapp.forms import TestForm

class Test(FormView):
   template_name = "pkm_templates/main.html"
   form_class = Testform

   # XXX you don't need this one since you're
   # bypassing `FormView.form_valid()` and
   # directly returning a redirect...
   success_url = '/thanks/'

   def form_valid(self,form):
       title = form.cleaned_data.get("title")
       fkey = form.cleaned_data.get("fkey")
       instance = Buildkb.objects.create(title=title,fkey=fkey)
       for user in fkey:
          instance.fkey.add(user)
       instance.save()
       # Unrelated, but you should NOT hardcode urls
       # - use `reverse()` instead
       return redirect("/")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.