Django Sites Framework initial setup

前端 未结 1 413
感情败类
感情败类 2021-02-06 11:38

I\'m comfortable with fairly one-dimensional Django implementations, but now trying to understand the multi-sites-with-shared-stuff process.

I\'ve read through the Dj

相关标签:
1条回答
  • 2021-02-06 12:15

    Q1: Does the Sites Framework assume each site is a separate project or a separate app?

    A Django website usually consists of multiple apps, so the "single app" approach wouldn't really work. But it's not helpful to think in terms of a separate project for every site either. The only thing that has to be separate for every site is the settings file.

    Does having a separate settings file for every site is a violation of the DRY principle? Not necessarily. You can (and should) create a base settings file with common settings. You would then import them in per-site settings files.

    For example in your project directory you could have three settings files:

    base_settings.py
    siteA_settings.py
    siteB_settings.py
    

    siteA_settings.py and siteB_settings.py would import all the settings from base_settings.py.

    The only thing you have to put in the per-site setting files is the SITE_ID setting with an individual site id. All the other settings may be shared across the sites (base_settings.py) or be specific to a particular site (siteA_settings.py, siteB_settings.py) depending on your individual needs. For example you can redefine the TEMPLATE_DIRS setting in per-site settings, if you have seperate per-sites templates, but if the templates are shared across the sites you don't have to. The same with all the other settings - url structure, list of installed apps, etc.

    You would then choose which site you want to run just by specifying the settings files. For example with the Django development server:

    python manage.py runserver --settings=mysite.siteA_settings
    

    or

    python manage.py runserver --settings=mysite.siteB_settings
    

    Q2: Sounds like I'll need to redefine database models (models.py) into a many-to-many relationships for sharing data between sites. Does that just change the way Django accesses those tables, or will the existing site's database need to be rebuilt as well?

    Models that are just shared between the sites don't need to be modified, as long as all the objects are shared. Only in cases when you want to be able to control which object of the model appears on each site, you need to account for that by adding a relationship (Many To Many or a Foreign Key, depending on your needs) from your model to the Django's Site model.

    Doing that does indeed change the underlying database structure (as adding a Foreign Key requires an additional column in a database table for a given model, and adding a Many To Many relationship requires a whole additional table). The Sites framework itself also uses its own database table, so you need to sync the database after enabling it.

    After you add the relationship you can easily restrict your query to objects intended for the current site - for example by using the CurrentSiteManager. On the other hand you still can access all the objects, using the default manager (i.e. the usual Model.objects form of accessing the ORM).

    0 讨论(0)
提交回复
热议问题