使用路由器时,无法在django中创建超级用户

时间:2017-10-07 19:12:51

标签: python mysql django python-3.x multiple-databases

我想用Djthon3在Djago 1.11.4上创建mongodb / mysql项目。我打算将mysql用于用户身份验证,mongodb用于所有其他目的。我掌握创建用户但无法创建超级用户。以下是我尝试创建超级用户时发生的事情:

convert -loop 0 -delay 10 dog_cursor_24_1.png dog_cursor_24_2.png dog_cursor_24_3.png, dog_cursor_24_4.png, dog_cursor_24_5.png test.gif

以下是我的settings.py:

的一部分
$ python3 manage.py createsuperuser username=Admin
System check identified some issues:

WARNINGS:
?: (urls.W001) Your URL pattern '^$' uses include with a regex ending with a '$'. Remove the dollar from the regex to avoid problems including URLs.
Email address: admin@example.com
Password: 
Password (again): 
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/management/commands/createsuperuser.py", line 63, in execute
    return super(Command, self).execute(*args, **options)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/management/commands/createsuperuser.py", line 183, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/models.py", line 170, in create_superuser
    return self._create_user(username, email, password, **extra_fields)
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/models.py", line 153, in _create_user
    user.save(using=self._db)
  File "/usr/local/lib/python3.5/dist-packages/django/contrib/auth/base_user.py", line 80, in save
    super(AbstractBaseUser, self).save(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 807, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 834, in save_base
    with transaction.atomic(using=using, savepoint=False):
  File "/usr/local/lib/python3.5/dist-packages/django/db/transaction.py", line 158, in __enter__
    if not connection.get_autocommit():
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/base.py", line 385, in get_autocommit
    self.ensure_connection()
  File "/usr/local/lib/python3.5/dist-packages/django/db/backends/dummy/base.py", line 20, in complain
    raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

我成功迁移了用户数据库,并以交互模式成功创建了用户:

DATABASES = {
    'default': {
        'ENGINE' : 'django.db.backends.dummy',
        'NAME' : 'my_database'
    },
    'users': {
        'NAME': 'nra_gpb_users',
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'django'
    }
}
class AuthRouter(object):
    """
    A router to control all database operations on models in the
    auth application.
    """
    def db_for_read(model, **hints):
        """
        Attempts to read auth models go to auth_db.
        """
        if model._meta.app_label == 'auth':
            return 'users'
        return 'default'

    def db_for_write(model, **hints):
        """
        Attempts to write auth models go to auth_db.
        """
        print(model._meta.app_label)
        if model._meta.app_label == 'auth':
            return 'users'
        return 'default'

    def allow_relation(obj1, obj2, **hints):
        """
        Allow relations if a model in the auth app is involved.
        """
        return False

    def allow_migrate(db, app_label, model_name=None, **hints):
        """
        Make sure the auth app only appears in the 'auth_db'
        database.
        """
        return db == 'auth_db'
DATABASE_ROUTERS = [AuthRouter]

请解释一下,出了什么问题?

UPD:使用以下命令成功创建超级用户

python3 manage.py createsuperuser --database users --username Admin

但是当我尝试授权时,再次收到ImproperConfigure错误:

Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.setup()
>>> from django.contrib.auth.models import User
>>> user = User.objects.create_user('Admin', 'admin@example.com', 'password')
>>> user.save()
>>> exit()

2 个答案:

答案 0 :(得分:0)

创建用户时使用的数据库需要在终端中传递,因为默认的数据库连接参数是虚拟的。

python3 manage.py createsuperuser --database users --username Admin

答案 1 :(得分:0)

这是一个正确的settings.py部分,可以让我获得成功的管理授权:

button_classes = ["btn", "btn-md"]
button_classes.push("btn-success") if @user.voted_up_on?(@post)

正如@Oluwafemi Sule所提到的,在我的情况下,超级用户可以通过命令创建:

<%= link_to upvote_path(:post => post), class: button_classes, :method => :post do %>
   <span class="glyphicon glyphicon-arrow-up"></span>
<% end %>