我通常寫的 Laravel 有一個指令可以更新 .env 檔案中的加密金鑰。老實說,我喜歡這種方法,並且想在我的 django 專案上複製。
因此,我按照以下步驟操作:
第 1 步:載入 .env 文件
請參閱:https://dev.to/pcmagas/how-to-load-env-in-django-project-4c9d
步驟 2:使用 SECRET_KEY 環境檔案:
在 settings.py 執行:
SECRET_KEY = os.getenv('SECRET_KEY',None) if SECRET_KEY is None: raise RuntimeError("SECRET_KEY value is not defined upon .env file")
步驟 3 建立更新 .env 的命令:
我使用以下內容製作了腳本 myapp/management/commands/mk_key.py(將 myapp 替換為您自己的應用程式名稱):
from django.core.management.base import BaseCommand from django.core.management.utils import get_random_secret_key import os class Command(BaseCommand): help = 'Create a new Secret Key' def handle(self, *args, **kwargs): key = get_random_secret_key() env_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..',"..","..",'.env') self.updateDotenv(env_file_path,key) def updateDotenv(self,env_file_path,key): with open(env_file_path, 'r') as file: lines = file.readlines() # Update the SECRET_KEY line updated_lines = [] for line in lines: if line.startswith('SECRET_KEY'): continue else: updated_lines.append(line) line = f"SECRET_KEY='{key}'\n" updated_lines.insert(0,line) # Replace with new key # Write the updated lines back to the .env file with open(env_file_path, 'w') as file: file.writelines(updated_lines) # Output the new secret key self.stdout.write(f"Updated .env\n")
然後運行它:
python manage.py mk_key
以上是使用 .env 更新 Django 金鑰的詳細內容。更多資訊請關注PHP中文網其他相關文章!

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

concatenateListSinpythonWithTheSamelements,使用:1)operatoTotakeEpduplicates,2)asettoremavelemavphicates,or3)listcompreanspherensionforcontroloverduplicates,每個methodhasdhasdifferentperferentperferentperforentperforentperforentperfornceandordorimplications。

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允許ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3漢化版
中文版,非常好用

WebStorm Mac版
好用的JavaScript開發工具

Dreamweaver Mac版
視覺化網頁開發工具