django错误。关于django.forms

时间:2010-01-20 09:20:07

标签: python django

from django import forms

class a(forms.Form):
    name = forms.CharField(initial='Your name')
    url = forms.URLField(initial='Your Web site')
    comment = forms.CharField()
data = {'name': 'hahaha', 'url': '', 'comment': 'Foo'}

f = a(data,auto_id=False)

#print f.is_valid()
print f.errors

错误:

Traceback (most recent call last):
  File "D:\zjm_code\register\registration\a.py", line 15, in <module>
    print f.errors
  File "D:\Python25.4\Lib\site-packages\django\utils\encoding.py", line 32, in __str__
    return self.__unicode__().encode('utf-8')
  File "D:\Python25.4\Lib\site-packages\django\forms\util.py", line 21, in __unicode__
    return self.as_ul()
  File "D:\Python25.4\Lib\site-packages\django\forms\util.py", line 27, in as_ul
    for k, v in self.items()]))
  File "D:\Python25.4\Lib\site-packages\django\utils\encoding.py", line 71, in force_unicode
    s = unicode(s)
  File "D:\Python25.4\Lib\site-packages\django\forms\util.py", line 37, in __unicode__
    return self.as_ul()
  File "D:\Python25.4\Lib\site-packages\django\forms\util.py", line 42, in as_ul
    % ''.join([u'<li>%s</li>' % conditional_escape(force_unicode(e)) for e in self]))
  File "D:\Python25.4\Lib\site-packages\django\utils\encoding.py", line 71, in force_unicode
    s = unicode(s)
  File "D:\Python25.4\Lib\site-packages\django\utils\functional.py", line 201, in __unicode_cast
    return self.__func(*self.__args, **self.__kw)
  File "D:\Python25.4\Lib\site-packages\django\utils\translation\__init__.py", line 62, in ugettext
    return real_ugettext(message)
  File "D:\Python25.4\Lib\site-packages\django\utils\translation\__init__.py", line 32, in delayed_loader
    if settings.USE_I18N:
  File "D:\Python25.4\Lib\site-packages\django\utils\functional.py", line 269, in __getattr__
    self._setup()
  File "D:\Python25.4\Lib\site-packages\django\conf\__init__.py", line 38, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

1 个答案:

答案 0 :(得分:1)

出现错误,因为您没有将脚本作为Django应用程序的一部分运行。您可以使用number of measures来使其发挥作用,但是使用与应用分开的表单可能不会非常有用。