无法使用django和virtualenvwrapper

时间:2016-12-26 22:34:16

标签: python django database postgresql

我是网站创建的新手,我正在寻找使用django和mysql创建网站。我一直在关注以下教程:http://www.marinamele.com/taskbuster-django-tutorial/install-and-configure-posgresql-for-django(注意:我从一开始就一直关注本教程,而不仅仅是从那一点开始)。当我尝试使用python3 manage.py migrate迁移数据库时,我的问题接近该页面的底部。这是我得到的错误:

  

Traceback(最近一次调用最后一次):文件   " /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/db/backends/base/base.py" ;,   第199行,在ensure_connection中       self.connect()File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/db/backends/base/base.py",   第171行,在连接中       self.connection = self.get_new_connection(conn_params)File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/db/backends/postgresql/base.py",   第176行,在get_new_connection中       connection = Database.connect(** conn_params)File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/psycopg2/ init .py" ,   第164行,连接       conn = _connect(dsn,connection_factory = connection_factory,async = async)psycopg2.OperationalError:FATAL:role   "“sohaib_taskbuster”"不存在

     

上述异常是导致以下异常的直接原因:

     

回溯(最近一次呼叫最后一次):文件" manage.py",第22行,in          execute_from_command_line(sys.argv)File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/core/management/ init .py" ,   第367行,在execute_from_command_line中       utility.execute()文件" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/core/management/ init .py",   第359行,执行中       self.fetch_command(子命令).run_from_argv(self.argv)File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/core/management/base.py" ,   第294行,在run_from_argv中       self.execute(* args,** cmd_options)File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/core/management/base.py",   第345行,执行中       output = self.handle(* args,** options)File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/core/management/commands/migrate.py&# 34 ;,   第83行,处理中       executor = MigrationExecutor(connection,self.migration_progress_callback)文件   " /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/db/migrations/executor.py" ;,   第20行,在 init 中       self.loader = MigrationLoader(self.connection)File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/db/migrations/loader.py",   第52行,在 init 中       self.build_graph()File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/db/migrations/loader.py",   第203行,在build_graph中       self.applied_migrations = recorder.applied_migrations()File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/db/migrations/recorder.py",   第65行,在applied_migrations中       self.ensure_schema()File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/db/migrations/recorder.py",   第52行,在ensure_schema中       如果self.connection.introspection.table_names(self.connection.cursor())中的self.Migration._meta.db_table:
  文件   " /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/db/backends/base/base.py" ;,   第231行,在游标中       cursor = self.make_debug_cursor(self._cursor())File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/db/backends/base/base.py" ;,   第204行,在_cursor中       self.ensure_connection()File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/db/backends/base/base.py",   第199行,在ensure_connection中       self.connect()File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/db/utils.py",   第94行,在退出       six.reraise(dj_exc_type,dj_exc_value,traceback)File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/utils/six.py",   第685行,重新加入       提高value.with_traceback(tb)File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/db/backends/base/base.py",   第199行,在ensure_connection中       self.connect()File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/db/backends/base/base.py",   第171行,在连接中       self.connection = self.get_new_connection(conn_params)File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/django/db/backends/postgresql/base.py",   第176行,在get_new_connection中       connection = Database.connect(** conn_params)File" /Users/sohaiba/.virtualenvs/env1/lib/python3.5/site-packages/psycopg2/ init .py" ,   第164行,连接       conn = _connect(dsn,connection_factory = connection_factory,async = async)django.db.utils.OperationalError:FATAL:role   "“sohaib_taskbuster”"不存在

(注意角色中双引号内的单引号,用户名应该只是sohaib_taskbuster)现在,在我的设置中我设置了:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': get_env_variable('DATABASE_NAME'),
        'USER': get_env_variable('DATABASE_USER'),
        'PASSWORD': get_env_variable('DATABASE_PASSWORD'),
        'HOST':'',
        'PORT':'',
    }
}

其中get_env_variable是:

def get_env_variable(var_name):
    try:
        return os.environ[var_name]
    except KeyError:
        error_msg = "Set the %s environment variable" % var_name
        raise ImproperlyConfigured(error_msg)

也就是说,它应该使用postactivate挂钩来提取数据库的相关信息。这就是我的postactivate钩子的样子:

export DJANGO_SETTINGS_MODULE="taskbuster.settings.development"
export DATABASE_NAME=‘taskbuster_db’
export DATABASE_USER=‘sohaib_taskbuster’
export DATABASE_PASSWORD=‘********’

使用此方法会引发上述错误,但如果我在我的设置中使用以下内容:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'taskbuster_db',
        'USER': 'sohaib_taskbuster',
        'PASSWORD': '**********',
        'HOST':'',
        'PORT':'',
    }
}

然后迁移工作正常。我不确定为什么会这样。有想法该怎么解决这个吗? (如果它有任何区别,我使用的是postgresql)

0 个答案:

没有答案
相关问题