Django数据库设置

时间:2014-07-31 05:23:53

标签: python django django-templates django-settings django-database

我的Django应用程序中有两个数据库,但有不同的IP,即HOST不同。

    'default': { # This is the DB in the same machine itself.
        'ENGINE': 'django.db.backends.postgresql_psycopg2', 
        'NAME': 'sortation_gor',              
        'USER': 'vms_gor',
        'PASSWORD': 'apj0702',
        'HOST': '127.0.0.1',
        'PORT': '',
    },
    'a1': { # This is in another machine with IP listed below.
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'a1',              
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '192.168.1.27',
        'PORT': '',
    }
}

当我运行我的代码,并尝试在a1数据库中保存数据时,它会返回500 error。但是如果我在应用程序运行的同一台机器上运行数据库,它就可以正常工作。 可能是什么问题?

Q2)在我的模板中,我有一个检查字段: -

{% if admin_permission %}
   <li id='main_menu_config'><a href="{% url 'configuration' %}"><span class="icon-cog icon-white"></span> Configuration</a></li
{% endif %}

虽然Configuration标签不可见,但即使我不是管理员,我仍然可以导航到网址(前提是我知道确切的网址)。

如何解决这个问题。

Routers.py文件

class GorRouter(object):
    """
    A router to control all database operations on models in the
    gor application.
    """
    def db_for_read(self, model, **hints):
        """
        Attempts to read gor models go to gor_db.
        """
        if model._meta.app_label == 'gor_data':
            return 'default'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write gor models go to gor_db.
        """
        if model._meta.app_label == 'gor_data':
            return 'default'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the gor app is involved.
        """
        if obj1._meta.app_label == 'gor_data' or \
           obj2._meta.app_label == 'gor_data':
           return True
        return None

    def allow_migrate(self, db, model):
        """
        Make sure the gor app only appears in the 'gor_db'
        database.
        """
        if db == 'default':
            return model._meta.app_label == 'gor_data'
        elif model._meta.app_label == 'gor_data':
            return False
        return None



class A1Router(object):
    def db_for_read(self, model, **hints):

        if model._meta.app_label == 'a1_data':
            return 'a1'
        return None

    def db_for_write(self, model, **hints):

        if model._meta.app_label == 'a1_data':
            return 'a1'
        return None

    def allow_relation(self, obj1, obj2, **hints):

        if obj1._meta.app_label == 'a1_data' or \
           obj2._meta.app_label == 'a1_data':
           return True
        return None

    def allow_migrate(self, db, model):
        """
        Make sure the aramex app only appears in the 'aramex_db'
        database.
        """
        if db == 'a1':
            return model._meta.app_label == 'a1_data'
        elif model._meta.app_label == 'a1_data':
            return False
        return None

0 个答案:

没有答案