开发者

Extend an existing list in Django settings

I'm trying to allow a more flexible date input into my application, and would like to use the default Django settings. Per the documentation, I can define DATE_INPUT开发者_如何学运维_FORMATS in settings.py, but I would like to keep the defaults and add a few others.

I tried extending it as if it already existed that failed miserably:

DATE_INPUT_FORMATS += ['%m-%d-%y',]

Yields: NameError: name 'DATE_INPUT_FORMATS' is not defined

I also tried importing the default settings from django.conf, but that is circular, as it tries to include my settings.py that I'm loading it in.

from django.conf import settings as default_settings
DATE_INPUT_FORMATS = default_settings.DATE_INPUT_FORMATS
DATE_INPUT_FORMATS += ['%m-%d-%y',]

Yields:

Error: Can't find the file 'settings.py' in the directory containing './manage.py'. It appears you've customized things.
You'll have to run django-admin.py, passing it your settings module.
(If the file settings.py does indeed exist, it's causing an ImportError somehow.)

What is the correct way to extend a default setting in Django's settings.py?


You should be able to import form django.conf.global_settings


It's a bit annoying, but it's much simpler just to redefine things and append your own to it. Also, as far as maintainability is concerned its smarter to be explicit with your settings.

Look in the django source code for django.conf.global_settings.py for the defaults.

DATE_INPUT_FORMATS = (
    '%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', # '2006-10-25', '10/25/2006', '10/25/06'
    '%b %d %Y', '%b %d, %Y',            # 'Oct 25 2006', 'Oct 25, 2006'
    '%d %b %Y', '%d %b, %Y',            # '25 Oct 2006', '25 Oct, 2006'
    '%B %d %Y', '%B %d, %Y',            # 'October 25 2006', 'October 25, 2006'
    '%d %B %Y', '%d %B, %Y',            # '25 October 2006', '25 October, 2006'

        '%m-%d-%y'        # <-- Yours
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜