简体   繁体   中英

Apps and Projects in Django

I am following The Django Book 2.0 for learning Django, and in there, we created a static website called mysite . After that, we created an app called books with a database. I understand we can create multiple apps under the same project.

However, we can only specify one database in mysite/settings.py , which I believe will be used by all apps under the project since it is not specified in books/ .

How can one create multiple apps that use different databases?

Lastly, what are some use cases of using multiple apps under the same project, while using a static page that's not on an app?

Check out the django documentation on multiple databases. You can actually list as many databases as you'd like in the DATABASES setting with different names.

Specifically check out the Routers section. You can set up routers in each app that will direct your models to use a certain database.

As far as use cases for multiple apps while using a static page that's not on an app, I'm not quite sure what you're asking. If you're talking about static webpages you can use flatpages . It is an optional django application that allows you to store HTML in a database to be edited from the admin.

In Django you define multiple databases like so:

In your settings.py

DATABASES = {
    'default': {
        'NAME': 'app_data',
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'USER': 'postgres_user',
        'PASSWORD': 's3krit'
    },
    'users': {
        'NAME': 'user_data',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'mysql_user',
        'PASSWORD': 'priv4te'
    }
}

here I have defined 2 databases a default and user, to know more about how to sync or use these database follow this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM