您的位置: 网站首页> 大家问> 当前文章

django框架数据库配置一次错误尝试

老董2019-06-06190围观,147赞

  最近用了diango框架自定义命令的功能,期间需要配置数据库,扫了一眼django框架数据库的配置大概看到如下场面:

  A dictionary containing the settings for all databases to be used with Django. It is a nested dictionary whose contents map a database alias to a dictionary containing the options for an individual database.

The DATABASES setting must configure a default database; any number of additional databases may also be specified.

  DATABASES={

    'default':{

    'ENGINE':'django.db.backends.postgresql',

    'NAME':'mydatabase',

    'USER':'mydatabaseuser',

    'PASSWORD':'mypassword',

    'HOST':'127.0.0.1',

    'PORT':'5432',

    }

  }


  OK,看了之后本能反应是默认数据库连接是default,如果要配置多个数据库可以自己添加!因为只配置一个数据库因此就写了这个default。然后再代码里面一直连接不上,提示数据库不存在!

  from django.db import connections

  with connections['mydatabase'].cursor()ascursor:


  后来才意识到,default才是名字,connections[]的键不应该放真正的数据库名,应该写配置名。于是直接改成了这样:

  DATABASES={

    'mydatabase':{

    'ENGINE':'django.db.backends.postgresql',

    'NAME':'mydatabase',

    'USER':'mydatabaseuser',

    'PASSWORD':'mypassword',

    'HOST':'127.0.0.1',

    'PORT':'5432',

    }

  }

很赞哦!

python编程网提示:转载请注明来源www.python66.com。
有宝贵意见可添加站长微信(底部),获取技术资料请到公众号(底部)。同行交流请加群 python学习会

文章评论

    django框架数据库配置一次错误尝试文章写得不错,值得赞赏

站点信息

  • 网站程序:Laravel
  • 客服微信:a772483200