0

I'm trying to add form to my django view. The problem is, I cannot declare class. That is the problem:

enter image description here

Addform.py:

from django import forms

class AddSubjectForm(forms.Form):
def __init__(self):
    pass

name = forms.CharField(max_length=200)

Views.py:

from django.http import HttpResponse
from django.template import Context, loader
from AddSubject.AddForm import AddSubjectForm

def index(request):
    template = loader.get_template('AddSubject/index.html')

    if request.method == 'POST':
        form = AddSubjectForm()
        context = Context({
                       'form': form,
                       })
    else:
        form = AddSubjectForm()
            context = Context({        
                       'form': form,
                       })

    return HttpResponse(template.render(context))

And finally I recieve error:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/AddSubject/

Django Version: 1.5.1
Python Version: 2.7.5
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'AddSubject')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
115.                         response = callback(request, *callback_args, **callback_kwargs)
File "blablabla\AddSubject\views.py" in index
14.         form = AddSubjectForm()

Exception Type: TypeError at /AddSubject/
Exception Value: 'module' object is not callable

I was looking for solution in Google, but every response was about filenames. It does not work for me :/ Have you got any idea, why AddSubjectForm doesn't work?

6
  • 1
    (a) I assume the incorrect indentation is a result of copy-and-pasting here, not a problem with your actual code. (b) Can you please paste the entire error here, not just a part of it?
    – Blair
    Commented Jun 10, 2013 at 7:54
  • __init__ in AddSubjectForm should be indent.
    – user326503
    Commented Jun 10, 2013 at 8:08
  • The code you've pasted wouldn't give that error. Are you sure you haven't defined or imported something else called AddSubjectForm somewhere? Commented Jun 10, 2013 at 8:20
  • 3
    Your import could be wrong from AddSubject.AddForm import AddSubjectForm should be from AddForm import AddSubjectForm. Commented Jun 10, 2013 at 8:27
  • @limelights - you're right! This import is correct, and everything works fine ;) thank you.
    – Fka
    Commented Jun 10, 2013 at 8:35

1 Answer 1

3

Your import statement is wrong as per my comment.

What you've written is

from AddSubject.AddForm import AddSubjectForm

change it to

from AddFrom import AddSubjectForm

What using from does is traverse the ALL modules so that it can import from a relative module. But since AddSubject isn't a package within AddSubject it loads from a module instead and treats the package as a module.

A good read is found here: Simple Statements#import

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.