【问题标题】:elastic beanstalk not creating superuser, invalid syntax弹性豆茎没有创建超级用户,语法无效
【发布时间】:2020-04-13 22:37:51
【问题描述】:

我只是想创建一个超级用户,以便我可以登录到我网站的管理部分。到目前为止,其他一切都在工作。 这是我的活动错误日志的相关部分:

[2019-12-22T05:02:06.721Z] INFO  [26175] - [Application update app-191222_070106@18/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_ss_app/Test for Command 02_createsuperuser] : Starting activity...
[2019-12-22T05:02:06.725Z] INFO  [26175] - [Application update app-191222_070106@18/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_ss_app/Test for Command 02_createsuperuser] : Completed activity. Result:

  Completed successfully.
[2019-12-22T05:02:06.726Z] INFO  [26175] - [Application update app-191222_070106@18/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_ss_app/Command 02_createsuperuser] : Starting activity...
[2019-12-22T05:02:07.074Z] INFO  [26175] - [Application update app-191222_070106@18/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_ss_app/Command 02_createsuperuser] : Activity execution failed, because: Traceback (most recent call last):
  File "/opt/python/current/app/manage.py", line 15, in <module>
  execute_from_command_line(sys.argv)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
  utility.execute()
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
  self.execute(*args, **cmd_options)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
  output = self.handle(*args, **options)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/management/commands/shell.py", line 92, in handle
  exec(sys.stdin.read())
  File "<string>", line 1
  from users.models import UserProfile; UserProfile.objects.create_superuser(xxxxxxx@gmail.com', 'passxxxword')
  ^
  SyntaxError: invalid syntax
   (ElasticBeanstalk::ExternalInvocationError)[2019-12-22T05:02:06.721Z] INFO  [26175] - [Application update app-191222_070106@18/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_ss_app/Test for Command 02_createsuperuser] : Starting activity...
[2019-12-22T05:02:06.725Z] INFO  [26175] - [Application update app-191222_070106@18/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_ss_app/Test for Command 02_createsuperuser] : Completed activity. Result:

  Completed successfully.
[2019-12-22T05:02:06.726Z] INFO  [26175] - [Application update app-191222_070106@18/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_ss_app/Command 02_createsuperuser] : Starting activity...
[2019-12-22T05:02:07.074Z] INFO  [26175] - [Application update app-191222_070106@18/AppDeployStage0/EbExtensionPostBuild/Infra-EmbeddedPostBuild/postbuild_0_ss_app/Command 02_createsuperuser] : Activity execution failed, because: Traceback (most recent call last):
  File "/opt/python/current/app/manage.py", line 15, in <module>
  execute_from_command_line(sys.argv)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
  utility.execute()
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
  self.execute(*args, **cmd_options)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
  output = self.handle(*args, **options)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/management/commands/shell.py", line 92, in handle
  exec(sys.stdin.read())
  File "<string>", line 1
  from users.models import UserProfile; UserProfile.objects.create_superuser(xxxxxxx@gmail.com', 'passxxxword')
  ^
  SyntaxError: invalid syntax
   (ElasticBeanstalk::ExternalInvocationError)

我正在尝试通过 db-migrate.config 文件进行部署:

container_commands:
  01_migrate:
    command: "django-admin.py migrate"
    leader_only: true
  02_createsuperuser:
    command: "echo \"from users.models import UserProfile; UserProfile.objects.create_superuser(xxxxxxx@gmail.com', 'passxxxword')\" | /opt/python/run/venv/bin/python /opt/python/current/app/manage.py shell"
    leader_only: true
option_settings:
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: studyspot.settings

【问题讨论】:

  • 您尝试通过shell 管理命令而不是createsuperuser 管理命令执行此操作是否有特殊原因?
  • 这是一个自定义用户模型,我不知道还有其他方法,因为它确实需要在 manager.py 中实现 create_superuser
  • 只要设置了DJANGO_SETTINGS_MODULEsettings.AUTH_USER_MODEL,使用createsuperuser应该没问题。
  • django 用户模型的重点不是让所有用户,包括同一个用户表中的超级用户吗?
  • 没有关注?只要设置正确,createsuperuser 应该在您的自定义用户表中创建用户。

标签: django amazon-elastic-beanstalk


【解决方案1】:

Elastic beanstalk 终于在 db-migrate.config 文件中使用了我的超级用户命令:

02_createsuperuser:
command: "echo \"from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('xxxxx@gmail.com', 'xxxxxpasword')\" | python manage.py shell"

这是使用自定义用户模型,目前没有太多关于此的信息,所以我希望这对某人有所帮助。我没有在此处包含managers.py 文件,但该部分应该很简单,而且我确实必须确保在我的forms.py 文件中的用户创建表单中具有保存用户功能。这花了我一周的时间才弄清楚,所以我将其发布以帮助其他人。

【讨论】:

    【解决方案2】:

    对于这不起作用的任何人,您可以尝试以下方法(适用于部署在 AWS EB Linux 2/3.3.1 上的 python 3.8.5 环境中的 Django 3.0.6)。我使用 PostgreSQL。

    这是适用于我的 db-migrate.config 文件的完整内容(2021 年)。

    首次部署:

    container_commands:
      01_migrate:
        command: "source /var/app/venv/*/bin/activate && python3 manage.py migrate && django-admin.py migrate"
        leader_only: true
    
      02_createsuperuser:
        command: "source /var/app/venv/*/bin/activate && echo \"from django.contrib.auth.models import User; User.objects.create_superuser('ADMIN_NAME', 'EMAIL', 'PASSWORD')\" | python3 manage.py shell"
        leader_only: true
    
    option_settings:
      aws:elasticbeanstalk:application:environment:
        DJANGO_SETTINGS_MODULE: ebdjango.settings
    

    每隔一段时间(必须更改,否则会出错)。请注意,“django-admin.py migrate”已经消失了:

    container_commands:
     01_migrate:
         command: "source /var/app/venv/*/bin/activate && python3 manage.py migrate"
            leader_only: true
        
        option_settings:
          aws:elasticbeanstalk:application:environment:
            DJANGO_SETTINGS_MODULE: ebdjango.settings
    

    (抱歉,空格有点不对 - 所以只需复制粘贴更正它)

    您需要了解部署在 EC2 实例上的 python 环境实际上是 python2。而python3系统环境(我认为)是3.6还是3.7?因此,您必须激活您在 django 部署中使用的 python venv。那是在 /var/app/venv/*/bin/...

    您可以通过 SSH 方式检查您的 EC2 实例并在那里进行测试。我认为 Python2 仍然是大多数 linux 机器的默认 python。除非您另有说明,否则即使 Ubuntu 18 也默认使用 Python2。您可以简单地检查各种 Python 版本。但在任何情况下,您都必须像上面的代码那样获取正确的环境。否则这是行不通的。

    祝你好运!希望 AWS 可以在不久的某个时候更新他们的文档。

    【讨论】:

      猜你喜欢
      • 2017-11-27
      • 2015-03-20
      • 2020-05-08
      • 1970-01-01
      • 2016-01-04
      • 2021-03-25
      • 1970-01-01
      • 2018-02-25
      • 2019-11-06
      相关资源
      最近更新 更多