如何在Django的不同设置上正确运行服务器? [英] How to properly runserver on different settings for Django?

查看:82
本文介绍了如何在Django的不同设置上正确运行服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的django rest API。我只想出于组织目的将一些设置分别用于开发和生产。我也只是在学习如何分离环境。我已经阅读了几本书,但似乎无法按照我想要的方式使其工作。

I have a basic django rest API. I want to separate some of my settings for dev and prod just for organizational purposes. I'm also just learning about separating environments. I've read a few things, but I can't seem to get it working the way I want it to.

层次结构如下所示:

- djangorest
  - api
    - __init__.py
    - models.py
    - views.py
    - urls.py
    - etc..
  - djangorest
    - __init__.py
    - settings.py
    - urls.py
    - wsgi.py

现在,当我运行服务器时,我做了一个简单的操作: / p>

Right now, when I run the server, I do a simple:

python3 manage.py runserver

该命令从settings.py中读取设置,并适当地运行它,但我一直在研究如何将设置分为prod vs dev,但无法正常工作。

That command reads the settings from settings.py and runs it appropriately but I've looked around on how to separate the settings into prod vs dev and it doesn't work right.

我希望能够拥有:

commonsettings.py
dev.py
prod.py

在常规设置中,开发人员和产品我试过运行:

In commonsettings, it'll just have whatever's in both dev and prod. I've tried running:

python3 manage.py runserver --settings=settings.dev

但这给我一个错误,说没有名为设置的模块。

But it gives me an error saying there is no module named 'setting'.

请帮助。谢谢!

推荐答案

例如,我在开发人员设置中打开SQL登录

For example I open SQL log in dev settings


  1. 我的文件结构:

    创建settings文件夹并将mv原始settings.py更改为settings / defaults.py

  1. My file structure:
    create settings folder and mv original settings.py to settings/defaults.py

init .py

中加载默认值

# proj/proj/settings/__init__.py

from .defaults import *




  1. 编辑dev.py



# proj/proj/settings/dev.py

from .defaults import *
DEBUG = True

# print sql to the console
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        }
    },
    'loggers': {
        'django.db.backends': {
            'handlers': ['console'],
            'propagate': True,
            'level': 'DEBUG',
        }
    },
}




  1. 在python env中运行



./manage.py runserver --settings proj.settings.dev

Django 2.1.7 | Mac

这篇关于如何在Django的不同设置上正确运行服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆