我正在尝试将表单添加到我的django视图中。问题是,我无法声明课程。那就是问题所在:Addform.py:from django import formsclass AddSubjectForm(forms.Form):def __init__(self): passname = forms.CharField(max_length=200)Views.py:from django.http import HttpResponsefrom django.template import Context, loaderfrom AddSubject.AddForm import AddSubjectFormdef 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))最后,我收到错误消息:Environment:Request Method: GETRequest URL: http://127.0.0.1:8000/AddSubject/Django Version: 1.5.1Python Version: 2.7.5Installed 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_response115. response = callback(request, *callback_args, **callback_kwargs)File "blablabla\AddSubject\views.py" in index14. form = AddSubjectForm()Exception Type: TypeError at /AddSubject/Exception Value: 'module' object is not callable我一直在寻找Google的解决方案,但每个回应都与文件名有关。它对我不起作用:/您有什么主意,为什么AddSubjectForm不起作用?
1 回答
慕妹3242003
TA贡献1824条经验 获得超6个赞
根据我的评论,您的进口声明有误。
你写的是
from AddSubject.AddForm import AddSubjectForm
更改为
from AddFrom import AddSubjectForm
using的from
作用是遍历所有模块,以便可以从相关模块导入。但是由于其中AddSubject
不是包,AddSubject
它而是从模块加载,并将该包视为模块。
添加回答
举报
0/150
提交
取消