Home > Software design >  Why ModelForm in my forms.py showing error?
Why ModelForm in my forms.py showing error?

Time:03-07

I have made the simple form using django, i am new to django,yesterday, i have wrote some code to practice,from today it is not working,but it is working yesterday.

template file

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form method="POST" novalidate>
        {% csrf_token %}
        {{form.as_p}}
        <input type="submit" value="Submit">
    </form>
    
</body>
</html>
from django.http import HttpResponse
from django.shortcuts import render
from .forms import FeedBackFormCustom
from .models import FeedBack
# Create your views here.


def home(req):
    if req.method == "POST":
        form = FeedBackFormCustom(req.POST)
        if form.is_valid():
            nm = form.cleaned_data['name']
            ag = form.cleaned_data['age']
            em = form.cleaned_data['email']
            st = form.cleaned_data['state']
            inst = FeedBack(name=nm, age=ag, email=em, state=st)
            inst.save()

            return HttpResponse('<h2>Form successfully submitted.</h2>')
    else:
        form = FeedBackFormCustom()
    context = {'form': form}
    return render(req, 'home/index.html', context)

Models.py

from pyexpat import model
from django.db import models

# Create your models here.


class FeedBack(models.Model):
    STATES_AVAILABLE = (
        ('ap', 'Andhra Pradesh'),
        ('mp', 'Madhya Pradesh'),
        ('kn', 'kutub nagar'),
        ('mu', 'Mumbai'),
        ('de', 'Delhi'),
        ('ch', 'Chennai'),
    )
    name = models.CharField(max_length=30)
    age = models.PositiveIntegerField()
    email = models.EmailField(max_length=200)
    state = models.CharField(max_length=3, choices=STATES_AVAILABLE)

    def __str__(self):
        return self.name

ModelForm.py

from django import forms
from .models import FeedBack
from django.contrib.auth.forms import UserChangeForm, UserCreationForm, PasswordChangeForm, SetPasswordForm


class FeedBackFormCustom(forms.ModelForm):
    class Meta:
        model = FeedBack()
        fields = '__all__'

I am trying since last 2 hours but not able to find out what mistake i did? Django is also not telling on which the error is occurred 'FeedBack' object is not callable

CodePudding user response:

This is because you have called your model that is model = FeedBack() in your forms.py. It should be model = FeedBack, this error means It is not callable object, it requires only its name.

Replace model=FeedBack() to model=FeedBack, only give name not ().

CodePudding user response:

On your ModelForm.py change your Model name inside Meta class as

from django import forms
from .models import FeedBack
from django.contrib.auth.forms import UserChangeForm, UserCreationForm, PasswordChangeForm, SetPasswordForm


class FeedBackFormCustom(forms.ModelForm):
    class Meta:
    model = FeedBack #<--------------- change here
    fields = '__all__'
  • Related