【问题标题】:Building Django modules (app) without a project在没有项目的情况下构建 Django 模块(应用程序)
【发布时间】:2019-12-16 09:59:57
【问题描述】:

按照django tutorial 模块化我的 django 代码,我已经能够分离出一些应用程序。我已经为这些应用程序制作了包,我们成功地在多个项目中使用了这些包。

我们的样板项目目录看起来有点像这样(非常像教程中的):

boilerplate/ 
boilerplate/boilerplate/settings.py
boilerplate/boilerplate/....
boilerplate/user_profile/admin.py, models.py, etc. 

理想情况下,我们只需 pip install user_profile 应用程序并将其包含在项目设置中,我们就可以开始了。

但是,无论何时我必须更新应用程序user_profile,我都必须在项目中运行它并进行开发。

是否有我可以遵循的可能的工作流程,以便我可以仅在 user_profile 应用程序/模块上进行大致的开发,而无需设置整个项目。我知道痛苦设置项目的要点几乎没有一两行,但在我的情况下,我们使用自定义数据库设置(使用 MongoDB)并且快速设置这些东西意味着我每次都必须更改我的机器(或环境)上的设置从事这个项目。

【问题讨论】:

    标签: python django django-rest-framework


    【解决方案1】:

    一个简单的最小示例是使用以下代码创建文件 manage.py

    import sys
    
    urlpatterns = []  
    # since django will search for ROOT_URLCONF from settings,
    # and we have given this same file as ROOT_URLCONF on settings, see below
    if __name__ == "__main__":
        import django
        from django.conf import settings
        from django.core.management import execute_from_command_line
    
        settings.configure(
            SECRET_KEY='YOURSECRETKEY',
            DEBUG=True,
            ROOT_URLCONF='manage',
            INSTALLED_APPS=[
                # list of your apps
            ]
        )
        # Note: you may need to configure database also
        django.setup()  # setup django after configuring settings
    
        execute_from_command_line(sys.argv)  # let this handle commands as always
    

    根据需要配置设置,然后运行 - 以python manage.py runserver为例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-04
      • 2014-02-20
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-15
      相关资源
      最近更新 更多