0
votes

my Blog models.py is

class Blog(models.Model):
    title = models.CharField('Title', max_length=200)
    text = models.TextField('Text', max_length=2048)
    date = models.DateTimeField('Last Modified')

    def __unicode__(self):
        return '%s' % (self.title)

and in my views.py I'm trying to convert models field to form field and then appending data, in the following views.py code I'll get a page where it asks for title and text only...

class BlogForm(ModelForm): 
    class Meta:
        model = Blog
        fields=('title', 'text')

def add_blog(request, pID='0'):
    bf = BlogForm()
    message = 'Unknown Request'
    p = get_object_or_404(Person, pk=pID)

    if request.method == 'GET':
        message = 'Add Blog for %s ' % p.name

    if request.method == 'POST':
        if request.POST['submit'] == 'Add':
            SaveForm = BlogForm(instance=p)
            bf = BlogForm(request.POST.copy())
            postDict = request.POST.copy()
            postDict['date'] = datetime.datetime.now()
            save_bf = SaveForm(postDict)
            if save_bf.is_valid():
                try:
                    bObj = save_bf.save()
                    p.blogs.add(bObj)
                    p.save()
                    message = 'Blog added to %s.' % p.name
                except:
                    message = 'Database Error.'
            else:
                message = 'Invalid data in Form.'

    return render_to_response(
                     'people/add_blog_form.html',{'bForm': bf,'message':message})

"But after subitting data I'll get the following error"

TypeError at /Blog/AddBlogForm/1/

'BlogForm' object is not callable

Request Method: POST Django Version: 1.4.1 Exception Type: TypeError Exception Value: 'BlogForm' object is not callable

Getting error in the line "save_bf = SaveForm(postDict)"

2

2 Answers

0
votes

You first create an instance of BlogForm named SaveForm (which is a pep08 violation BTW), then you try to call this form. The BlogForm class is callable (like any other class in Python), but (unless you define a __call__(self, ...) method on BlogForm, which would be somewhat surprising) BlogForm instances are not callable themselves.

As a side note : your code is quite a mess and makes things more complicated than they have to be. Also I don't see the relation between Blog and Person, and you're not passing the right kind of value for instance when instaciating BlogForm (you pass a Person when it should be a Blog - or None if you only want to create a blog)

0
votes

I'm really not sure what you're doing inside that POST block. You instantiate the form as SaveForm, then instantiate it again as bf, then try to call the first instance passing the post dictionary. I can't tell what that is supposed to be achieving.

It should just be:

if request.POST['submit'] == 'Add':
    postDict = request.POST.copy()
    postDict['date'] = datetime.datetime.now()
    bf = SaveForm(postDict)
    if bf.is_valid():

Edit actually, I've just realized that p is a Person instance. There's no need to pass that to the form at all, since it's a blog form, not a person form.