我尝试将环境变量从名为 .env 的文件加载到 settings.py 文件,在这里我创建了 .env 文件和设置文件相同的文件夹。
这是我的 .env 文件
DEBUG=on
SECRET_KEY=ksmdfw3324@#jefm
DATABASE_URL=psql://urser:un-githubbedpassword@127.0.0.1:8458/database
SQLITE_URL=sqlite:///my-local-sqlite.db
CACHE_URL=memcache://127.0.0.1:11211,127.0.0.1:11212,127.0.0.1:11213
REDIS_URL=rediscache://127.0.0.1:6379/1?
client_class=django_redis.client.DefaultClient&password=ungithubbed-secret
MYSQL_DATABASE = student
MYSQL_USERNAME = root
SECRET_KEY=secret-key
Run Code Online (Sandbox Code Playgroud)
这是我的 setting.py 文件
import os
from os.path import join, dirname
from dotenv import load_dotenv
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
# Accessing variables.
dbname = os.getenv('MYSQL_DATABASE')
secret_key = os.getenv('SECRET_KEY')
# Using variables.
print(dabname)
print(secret_key)
Run Code Online (Sandbox Code Playgroud)
我安装了 pip install -U python-dotenv
问题是我无法在设置文件中获取环境变量
在尝试python manage.py runserver时我收到此错误
C:\Users\mehul\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\dotenv\main.py:65: UserWarning: File doesn't exist
warnings.warn("File doesn't exist {}".format(self.dotenv_path))
Traceback (most …Run Code Online (Sandbox Code Playgroud) 1)类别实体:
/**
* Category
*
* @ORM\Table(name="category")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
*/
class Category {
/**
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @ORM\Column(name="categoryname", type="string")
*/
protected $categoryname;
/**
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="categories")
*/
protected $tags;
/**
* @return ArrayCollection
*/
public function __construct(){
$this->tags = new ArrayCollection();
}
Run Code Online (Sandbox Code Playgroud)
2)标签实体:
/**
* Tag
* @ORM\Table(name="tag")
* @ORM\Entity(repositoryClass="AppBundle\Repository\TagRepository")
*/
class Tag {
/**
* @var int
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO") …Run Code Online (Sandbox Code Playgroud) 我需要从字符串中提取日期,下面是我的代码和字符串。
$str = "Updated status to Masters - Software Engineering (Enrolled-
Documents to Send) on 03/06/2014 14:10 by Rabih Haddad .";
if(preg_match_all('/.*([A-Za-z]{3,4}, [A-Za-z]{3,4} [\d]{1,2}, [\d]{4}).*/',$str,$matches))
{
print_r($matches);
}
else
echo "not matched";
Run Code Online (Sandbox Code Playgroud)
如何date(03/06/2014)从字符串中提取值。