1

我希望你能帮助我弄清楚这段代码有什么问题:

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

我一直在寻找答案,但还没有找到任何东西......谢谢!

4

1 回答 1

1

很确定你有一个缩进错误。查看回溯,在super导入 forms.py 时正在执行该行,如果它在__init__方法内部,通常不会发生这种情况。所以我猜你有那行与方法定义在同一级别,并且在定义表单类时执行它。例如,检查您没有在任何地方混合制表符和空格。

于 2013-11-10T14:32:30.297 回答