开发者

How do I obtain django default settings values NOT defined in my settings.py file?

I must be missing something.

I'm trying to import DEFAULT_CONTENT_TYPE from the settings file. I don't define it specifically in my settings.py file, but I was assuming that the default would be available.

When I add this variable t开发者_JS百科o my settings.py it works fine, but when it's not there and I try to import I get an ImportError when using the development server via the runserver command:

from settings import DEFAULT_CONTENT_TYPE
ImportError: cannot import name DEFAULT_CONTENT_TYPE

Shouldn't I be able to do this without having to add it to my settings.py file?


Never user from settings import ... or import settings. This will always only import your settings module, but not the whole Django settings.

Use from django.conf import settings instead in all your Django projects. This is using the Django Settings class which is a wrapper around all Django defined settings and your project settings from your settings file.

So for your example you would access DEFAULT_CONTENT_TYPE via settings.DEFAULT_CONTENT_TYPE.


settings is relative to your project, so it'll try to import DEFAULT_CONTENT_TYPE from your settings file. If you want to use Django's built-in DEFAULT_CONTENT_TYPE setting, you'd have to include it from their libraries (sorry, don't have Django installed on this machine, otherwise I'd look up the file that it's defined in).

Something like this: from django.some.settings.file import DEFAULT_CONTENT_TYPE

You should be able to find it by just grep'ing through your Django lib dir.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜