我希望你能帮助我弄清楚这段代码有什么问题:
from django.forms import ModelForm
from models import Persona
class PersonaForm(ModelForm):
class Meta:
model = Persona
exclude = ('cuenta',)
def __init__(self, *args, **kwargs):
cuenta = kwargs.pop('cuenta')
super(PersonaForm, self).__init__(*args, **kwargs)
self.fields['organizacion'].queryset = Organizacion.objects.filter(cuenta=cuenta)
我收到以下错误:name 'PersonaForm' is not defined
这是整个追溯:
Traceback:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
103. resolver_match = resolver.resolve(request.path_info)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
319. for pattern in self.url_patterns:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
347. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
342. self._urlconf_module = import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "/Applications/djangostack-1.4.5-0/apps/django/django_projects/tomate/tomate/urls.py" in <module>
18. url(r'^dashboard/', include('dashboard.urls')),
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/conf/urls/__init__.py" in include
25. urlconf_module = import_module(urlconf_module)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py" in import_module
35. __import__(name)
File "/Applications/djangostack-1.4.5-0/apps/django/django_projects/tomate/dashboard/urls.py" in <module>
3. from dashboard import views
File "/Applications/djangostack-1.4.5-0/apps/django/django_projects/tomate/dashboard/views.py" in <module>
7. from forms import PersonaForm
File "/Applications/djangostack-1.4.5-0/apps/django/django_projects/tomate/dashboard/forms.py" in <module>
4. class PersonaForm(ModelForm):
File "/Applications/djangostack-1.4.5-0/apps/django/django_projects/tomate/dashboard/forms.py" in PersonaForm
12. super(PersonaForm, self).__init__(*args, **kwargs)
Exception Type: NameError at /
Exception Value: name 'PersonaForm' is not defined
我找到了答案,但还没找到任何答案...... 谢谢!
答案 0 :(得分:1)
很确定你有一个缩进错误。查看回溯时,正在导入forms.py时正在执行super
的行,如果它在__init__
方法内,通常不会发生。所以我猜你的那一行与方法定义处于同一级别,并且在定义表单类时执行它。例如,检查您没有在任何地方混合标签和空格。