Opened 6 years ago

Closed 5 years ago

#16229 closed Cleanup/optimization (invalid)

django.core.validators not usable without a DJANGO_SETTINGS_MODULE

Reported by: Jonas H. Owned by: nobody
Component: Core (Other) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

https://code.djangoproject.com/browser/django/trunk/django/core/validators.py#L16 reads (after catching a Django settings ImportError):

It's OK if Django settings aren't configured.

However, using the validate_email validator crashes without a DJANGO_SETTINGS_MODULE:

>>> from django.core.validators import validate_email
>>> validate_email('foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "django/core/validators.py", line 120, in __call__
    super(EmailValidator, self).__call__(value)
  File "django/core/validators.py", line 40, in __call__
    raise ValidationError(self.message, code=self.code)
  File "django/core/exceptions.py", line 60, in __init__
    message = force_unicode(message)
  File "django/utils/encoding.py", line 71, in force_unicode
    s = unicode(s)
  File "django/utils/functional.py", line 108, in __unicode_cast
    return self.__func(*self.__args, **self.__kw)
  File "django/utils/translation/__init__.py", line 87, in ugettext
    return _trans.ugettext(message)
  File "django/utils/translation/__init__.py", line 49, in __getattr__
    if settings.USE_I18N:
  File "django/utils/functional.py", line 180, in __getattr__
    self._setup()
  File "django/conf/__init__.py", line 40, 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.

Change History (3)

comment:1 Changed 6 years ago by melinath

Resolution: invalid
Status: newclosed
Type: BugCleanup/optimization
Version: 1.3SVN

This is not a bug. It's not even a documentation error. Looking at the code, the comment only refers to the setting of URL_VALIDATOR_USER_AGENT and makes no claims about the running of the EmailValidator. I suppose the comment could be clarified. However, it doesn't really seem necessary.

comment:2 Changed 5 years ago by Jonas H.

Resolution: invalid
Status: closedreopened

Reopening as this comment should be fixed.

comment:3 Changed 5 years ago by Luke Plant

Resolution: invalid
Status: reopenedclosed

I agree with melinath that fixing the comment is unnecessary - it is clear enough that it refers to settings being optional for the purpose of setting URL_VALIDATOR_USER_AGENT.

Note: See TracTickets for help on using tickets.
Back to Top