简体   繁体   English

python'模块'对象不可调用

[英]python 'module' object is not callable

I'm trying to add form to my django view. 我正在尝试将表单添加到我的django视图中。 The problem is, I cannot declare class. 问题是,我无法声明课程。 That is the problem: 那就是问题所在:

在此处输入图片说明

Addform.py : Addform.py

from django import forms

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

name = forms.CharField(max_length=200)

Views.py : 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. 我一直在寻找Google的解决方案,但每个回应都涉及文件名。 It does not work for me :/ Have you got any idea, why AddSubjectForm doesn't work? 它对我不起作用:/您有任何想法,为什么AddSubjectForm不起作用?

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. using from作用是遍历所有模块,以便可以从相关模块导入。 But since AddSubject isn't a package within AddSubject it loads from a module instead and treats the package as a module. 但是由于AddSubject不是AddSubject中的包, AddSubject它从模块加载,并将该包视为模块。

A good read is found here: Simple Statements#import 在这里可以找到很好的阅读: Simple Statements#import

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM