{"id":983760,"date":"2024-12-27T07:22:08","date_gmt":"2024-12-26T23:22:08","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/983760.html"},"modified":"2024-12-27T07:22:10","modified_gmt":"2024-12-26T23:22:10","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5setting%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/983760.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165setting\u7c7b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211651\/9e3d3d4c-9433-4b62-90e3-7db64b26d06d.webp\" alt=\"python\u5982\u4f55\u5bfc\u5165setting\u7c7b\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u5bfc\u5165\u8bbe\u7f6e\u7c7b\uff0c\u901a\u5e38\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u6a21\u5757\u5bfc\u5165\u3001\u5229\u7528Django\u6846\u67b6\u4e2d\u7684\u8bbe\u7f6e\u6a21\u5757\u3001\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u7ba1\u7406\u914d\u7f6e\u3002\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u9879\u76ee\u7ed3\u6784\u548c\u9700\u6c42\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528\u6a21\u5757\u5bfc\u5165\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u5c06\u8bbe\u7f6e\u7c7b\u5b9a\u4e49\u5728\u4e00\u4e2a\u5355\u72ec\u7684Python\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u5728\u9700\u8981\u7684\u5730\u65b9\u8fdb\u884c\u5bfc\u5165\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6a21\u5757\u5bfc\u5165<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5bfc\u5165\u6a21\u5757\u662f\u975e\u5e38\u57fa\u7840\u4e14\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u901a\u8fc7\u5b9a\u4e49\u8bbe\u7f6e\u7c7b\u5e76\u5c06\u5176\u4fdd\u5b58\u5728\u4e00\u4e2a\u72ec\u7acb\u7684\u6a21\u5757\u4e2d\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u5176\u4ed6\u6a21\u5757\u4e2d\u5bfc\u5165\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u521b\u5efa\u8bbe\u7f6e\u7c7b\u6a21\u5757<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff0c\u4f8b\u5982<code>settings.py<\/code>\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u4f60\u7684\u8bbe\u7f6e\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># settings.py<\/p>\n<p>class Settings:<\/p>\n<p>    def __init__(self):<\/p>\n<p>        self.debug = True<\/p>\n<p>        self.database_url = &quot;sqlite:\/\/\/my_database.db&quot;<\/p>\n<p>        self.secret_key = &quot;your-secret-key&quot;<\/p>\n<p>    def display(self):<\/p>\n<p>        print(f&quot;Debug: {self.debug}&quot;)<\/p>\n<p>        print(f&quot;Database URL: {self.database_url}&quot;)<\/p>\n<p>        print(f&quot;Secret Key: {self.secret_key}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5bfc\u5165\u8bbe\u7f6e\u7c7b<\/h4>\n<\/p>\n<p><p>\u5728\u9700\u8981\u4f7f\u7528\u8bbe\u7f6e\u7684\u5730\u65b9\uff0c\u901a\u8fc7<code>import<\/code>\u8bed\u53e5\u5bfc\u5165\u5e76\u5b9e\u4f8b\u5316\u8be5\u7c7b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/p>\n<p>from settings import Settings<\/p>\n<p>def main():<\/p>\n<p>    config = Settings()<\/p>\n<p>    config.display()<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5229\u7528Django\u6846\u67b6\u4e2d\u7684\u8bbe\u7f6e\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u6b63\u5728\u4f7f\u7528Django\u6846\u67b6\uff0cDjango\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684\u8bbe\u7f6e\u6a21\u5757\u6765\u7ba1\u7406\u9879\u76ee\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><h4>1. \u914d\u7f6eDjango\u8bbe\u7f6e<\/h4>\n<\/p>\n<p><p>\u5728Django\u9879\u76ee\u4e2d\uff0c\u901a\u5e38\u5728<code>settings.py<\/code>\u6587\u4ef6\u4e2d\u5b9a\u4e49\u5404\u79cd\u914d\u7f6e\u9009\u9879\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># settings.py<\/p>\n<p>DEBUG = True<\/p>\n<p>DATABASES = {<\/p>\n<p>    &#39;default&#39;: {<\/p>\n<p>        &#39;ENGINE&#39;: &#39;django.db.backends.sqlite3&#39;,<\/p>\n<p>        &#39;NAME&#39;: &#39;my_database.db&#39;,<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>SECRET_KEY = &#39;your-secret-key&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5bfc\u5165Django\u8bbe\u7f6e<\/h4>\n<\/p>\n<p><p>\u5728\u5176\u4ed6\u6a21\u5757\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>django.conf.settings<\/code>\u8bbf\u95ee\u8fd9\u4e9b\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># views.py<\/p>\n<p>from django.conf import settings<\/p>\n<p>def my_view(request):<\/p>\n<p>    debug_mode = settings.DEBUG<\/p>\n<p>    secret_key = settings.SECRET_KEY<\/p>\n<p>    # Perform actions using the settings<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u7ba1\u7406\u914d\u7f6e<\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u6765\u7ba1\u7406\u914d\u7f6e\u3002\u8fd9\u79cd\u65b9\u6cd5\u5c24\u5176\u9002\u5408\u9700\u8981\u5728\u4e0d\u540c\u73af\u5883\uff08\u5982\u5f00\u53d1\u3001\u6d4b\u8bd5\u3001\u751f\u4ea7\uff09\u4e2d\u8fd0\u884c\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bbe\u7f6e\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u6216\u8005\u5728\u9879\u76ee\u4e2d\u4f7f\u7528<code>.env<\/code>\u6587\u4ef6\u8fdb\u884c\u914d\u7f6e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\"># .env<\/p>\n<p>DEBUG=True<\/p>\n<p>DATABASE_URL=sqlite:\/\/\/my_database.db<\/p>\n<p>SECRET_KEY=your-secret-key<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u73af\u5883\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728Python\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u6216<code>python-dotenv<\/code>\u5e93\u6765\u8bfb\u53d6\u8fd9\u4e9b\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># config.py<\/p>\n<p>import os<\/p>\n<p>from dotenv import load_dotenv<\/p>\n<p>load_dotenv()  # \u52a0\u8f7d\u73af\u5883\u53d8\u91cf<\/p>\n<p>class Config:<\/p>\n<p>    DEBUG = os.getenv(&quot;DEBUG&quot;, &quot;False&quot;) == &quot;True&quot;<\/p>\n<p>    DATABASE_URL = os.getenv(&quot;DATABASE_URL&quot;)<\/p>\n<p>    SECRET_KEY = os.getenv(&quot;SECRET_KEY&quot;)<\/p>\n<h2><strong>\u4f7f\u7528\u914d\u7f6e<\/strong><\/h2>\n<p>config = Config()<\/p>\n<p>print(f&quot;Debug: {config.DEBUG}&quot;)<\/p>\n<p>print(f&quot;Database URL: {config.DATABASE_URL}&quot;)<\/p>\n<p>print(f&quot;Secret Key: {config.SECRET_KEY}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5bfc\u5165\u548c\u7ba1\u7406\u8bbe\u7f6e\u7c7b\u5728Python\u9879\u76ee\u4e2d\u662f\u975e\u5e38\u91cd\u8981\u7684\u64cd\u4f5c\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u9879\u76ee\u7684\u89c4\u6a21\u3001\u590d\u6742\u5ea6\u4ee5\u53ca\u90e8\u7f72\u73af\u5883\u3002\u65e0\u8bba\u662f\u901a\u8fc7\u6a21\u5757\u5bfc\u5165\u3001\u5229\u7528Django\u7684\u8bbe\u7f6e\u6a21\u5757\uff0c\u8fd8\u662f\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u7ba1\u7406\u914d\u7f6e\uff0c\u90fd\u9700\u8981\u786e\u4fdd\u914d\u7f6e\u7684\u5b89\u5168\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u7ed3\u5408\u591a\u79cd\u65b9\u6cd5\u6765\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\uff0c\u786e\u4fdd\u9879\u76ee\u7684\u7075\u6d3b\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u9879\u76ee\u4e2d\u6709\u6548\u7ba1\u7406\u8bbe\u7f6e\uff1f<\/strong><br \/>\u5728Python\u9879\u76ee\u4e2d\uff0c\u7ba1\u7406\u8bbe\u7f6e\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u6216\u73af\u5883\u53d8\u91cf\u6765\u5b9e\u73b0\u3002\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u4f7f\u7528<code>configparser<\/code>\u6a21\u5757\u8bfb\u53d6.ini\u6587\u4ef6\uff0c\u6216\u8005\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u3002\u8fd9\u6837\u53ef\u4ee5\u4f7f\u8bbe\u7f6e\u66f4\u52a0\u7075\u6d3b\u4e14\u6613\u4e8e\u7ef4\u62a4\uff0c\u5c24\u5176\u662f\u5728\u4e0d\u540c\u73af\u5883\uff08\u5982\u5f00\u53d1\u3001\u6d4b\u8bd5\u548c\u751f\u4ea7\uff09\u4e2d\u3002<\/p>\n<p><strong>\u5728Django\u9879\u76ee\u4e2d\uff0c\u5982\u4f55\u6b63\u786e\u5bfc\u5165settings.py\u4e2d\u7684\u914d\u7f6e\uff1f<\/strong><br \/>\u5728Django\u9879\u76ee\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7<code>from django.conf import settings<\/code>\u6765\u5bfc\u5165settings.py\u4e2d\u7684\u914d\u7f6e\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u8ba9\u4f60\u5728\u5e94\u7528\u4e2d\u7684\u4efb\u4f55\u5730\u65b9\u8bbf\u95ee\u5168\u5c40\u8bbe\u7f6e\uff0c\u786e\u4fdd\u9879\u76ee\u7684\u4e00\u81f4\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u540c\u65f6\uff0c\u4f7f\u7528<code>getattr<\/code>\u65b9\u6cd5\u53ef\u4ee5\u63d0\u4f9b\u9ed8\u8ba4\u503c\uff0c\u8fd9\u6837\u5728\u914d\u7f6e\u7f3a\u5931\u65f6\u4e0d\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002<\/p>\n<p><strong>Python\u4e2d\u5982\u4f55\u5904\u7406\u654f\u611f\u4fe1\u606f\u4ee5\u4fdd\u62a4\u8bbe\u7f6e\u5b89\u5168\uff1f<\/strong><br \/>\u5904\u7406\u654f\u611f\u4fe1\u606f\uff08\u5982API\u5bc6\u94a5\u3001\u6570\u636e\u5e93\u5bc6\u7801\u7b49\uff09\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u5b58\u50a8\u8fd9\u4e9b\u4fe1\u606f\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u5199\u5165\u4ee3\u7801\u4e2d\u3002\u4f7f\u7528<code>os.environ<\/code>\u53ef\u4ee5\u5b89\u5168\u5730\u8bfb\u53d6\u8fd9\u4e9b\u73af\u5883\u53d8\u91cf\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u5e93\u5982<code>python-decouple<\/code>\u4e5f\u53ef\u4ee5\u5e2e\u52a9\u5c06\u8bbe\u7f6e\u548c\u654f\u611f\u4fe1\u606f\u5206\u79bb\uff0c\u786e\u4fdd\u5b89\u5168\u6027\u548c\u7075\u6d3b\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u5bfc\u5165\u8bbe\u7f6e\u7c7b\uff0c\u901a\u5e38\u6709\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u6a21\u5757\u5bfc\u5165\u3001\u5229\u7528Django\u6846\u67b6\u4e2d\u7684\u8bbe\u7f6e\u6a21\u5757\u3001\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u7ba1 [&hellip;]","protected":false},"author":3,"featured_media":983766,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/983760"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=983760"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/983760\/revisions"}],"predecessor-version":[{"id":983769,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/983760\/revisions\/983769"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/983766"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=983760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=983760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=983760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}