ไม่สามารถสร้าง django superuser ใน aws beanstalk ผ่านคำสั่งคอนเทนเนอร์หรือ Manage.py

ฉันติดอยู่กับปัญหาคอขวดซึ่งฉันพยายามแก้ไขโดยใช้เอกสารอย่างเป็นทางการและคำตอบอื่น ๆ ที่นี่ใน stackoverflow แต่ก็ยังไม่สามารถสร้าง django superuser โดยทางโปรแกรมในสภาพแวดล้อมของฝักถั่ว

สถานะปัจจุบัน - ก. แอปพลิเคชันได้รับการปรับใช้อย่างราบรื่น และฉันสามารถเข้าถึงฐานข้อมูลจากแอปพลิเคชัน UI ของฉันได้ โดยพื้นฐานแล้วรายการกำลังถูกสร้างขึ้นในตารางอื่นที่ฉันมีในแอปพลิเคชัน

ฉันพยายามสร้าง superuser อย่างไร -

ก. โดยการส่งคำสั่งคอนเทนเนอร์ -

ตัวเลือกที่ 1-

container_commands:
  01_migrate:
    command: "django-admin.py migrate"
    leader_only: true
  02_collectstatic:
    command: "django-admin.py collectstatic --noinput"

commands:
  super_user:
    command: "source /opt/python/run/venv/bin/activate && python <appname>/createuser.py"
    leader_only: true

  option_settings:
  "aws:elasticbeanstalk:application:environment":
    DJANGO_SETTINGS_MODULE: "<Appname>.settings"
    PYTHONPATH: "/opt/python/current/app:$PYTHONPATH"

ในบันทึก - ฉันไม่เห็นว่ามันพยายามเรียกใช้คำสั่งที่กำหนดเอง

ตัวเลือก 2 -

container_commands:
  01_migrate:
    command: "django-admin.py migrate"
    leader_only: true
  02_collectstatic:
    command: "django-admin.py collectstatic --noinput"
  03_createsuperuser:
    command: "source /opt/python/run/venv/bin/activate && django-admin.py createsuperuser"

  option_settings:
  "aws:elasticbeanstalk:application:environment":
    DJANGO_SETTINGS_MODULE: "<appname>.settings"
    PYTHONPATH: "/opt/python/current/app:$PYTHONPATH"

สำหรับสิ่งนี้ ฉันได้สร้างไฟล์ createsuperuser.py ภายใต้ /management/commands/ ตามโครงสร้างของ init.py ในทั้งสองโฟลเดอร์และหนึ่ง createsuperuser.py ภายใต้คำสั่ง -

from django.core.management.base import BaseCommand
from django.contrib.auth.models import User


class Command(BaseCommand):

   def handle(self, *args, **options):
      if not User.objects.filter(username="admin").exists():
          User.objects.create_superuser("admin", "[email protected]", "admin")

เกี่ยวกับเรื่องนี้ ฉันได้รับข้อความต่อไปนี้จากบันทึก -

Superuser creation skipped due to not running in a TTY. You can run `manage.py createsuperuser` in your project to create one manually.    

คำถามของฉันคือ -

  1. เหตุใดฉันจึงไม่สามารถสร้าง superuser จากบรรทัดคำสั่งของ env เสมือนของฉันได้ โดยที่ฉันได้รับข้อความเช่นนี้ -

    Raise ImproperlyConfigured("settings.DATABASES is improperly configuration. " django.core.Exceptions.ImproperlyConfigured: settings.DATABASES is improperly configuration. Please enter the ENGINE value. ตรวจสอบเอกสารการตั้งค่าสำหรับรายละเอียดเพิ่มเติม

ค่อนข้างแปลกเมื่อพิจารณาว่าคำสั่ง makemigrations ทำงานได้ดี

และเมื่อฉันสะท้อน $DJANGO_SETTINGS_MODULE ฉันจะได้รับการตั้งค่าที่ถูกต้อง

appname.settings

แจ้งให้เราทราบว่าฉันผิดพลาดตรงไหนในการสร้างสิ่ง superuser?


person nishith83    schedule 10.07.2016    source แหล่งที่มา


คำตอบ (3)


ฉันแก้ไขปัญหานี้เมื่อเร็ว ๆ นี้ด้วยการใช้งานแอปตัวอย่างของฉันในฝักถั่ว ฉันติดตามเอกสารอย่างเป็นทางการเป็นส่วนใหญ่จากลิงก์

  1. ในโฟลเดอร์แอป django ของคุณให้สร้างแพ็คเกจ 'การจัดการ' หลาม
  2. สร้างแพ็คเกจอื่นภายใน 'คำสั่ง' แพ็คเกจการจัดการ
  3. สร้างไฟล์หลามในคำสั่งแพ็คเกจ mysuperuser.py

    import os
    from django.core.management.base import BaseCommand
    from django.contrib.auth.models import User
    
    class Command(BaseCommand):
        def handle(self, *args, **options):
            if not User.objects.filter(username='myuser').exists():
                User.objects.create_superuser('myuser',
                                              '[email protected]',
                                              'mypassword')
    
  4. ในไฟล์ django-migrate.config ให้เพิ่มคำสั่งที่สอง

    02_create_superuser_for_django_admin: command: "python manage.py mysuperuser" leader_only: true

  5. ทำ python Manage.py collectstatic และ eb ปรับใช้

การทำเช่นนี้สร้าง superuser ให้ฉัน ฉันไม่จำเป็นต้องเพิ่ม PYTHONPATH ใด ๆ ตามที่อธิบายไว้ในคำตอบบางข้อที่มีทางออนไลน์

person Deepesh Pandey    schedule 25.07.2019

ไฟล์ที่คุณกำหนดเองมีชื่อว่า "createsuperuser.py" ซึ่งเหมือนกับคำสั่ง Django และการชนกันนั้นเป็นสาเหตุของปัญหา ใช้ "createsu.py" เป็นชื่อไฟล์ จากนั้นอย่าลืมเปลี่ยนไฟล์กำหนดค่าให้ใช้ "createsu" ด้วย

person thrillhouse    schedule 10.11.2018
comment
ฉันพบคำตอบและแก้ไขความคิดเห็นดั้งเดิมของฉัน ฉันคิดว่ามันแปลกนิดหน่อยที่ฉันสามารถตอบได้โดยไม่ต้องตอบคำถามแต่ไม่สามารถแสดงความคิดเห็นได้! ฉันไม่ค่อยได้รับคำตอบสำหรับคำถามที่นี่ แต่ที่นี่ฉันแค่อยากจะส่งข้อความถึงผู้โพสต์ต้นฉบับของคำถามและดูว่าเขาเคยพบคำตอบหรือไม่ ฉันเดาว่ามันเป็นเพียงมุมแหลมของ stackoverflow ที่ฉันยังคิดไม่ออก - person thrillhouse; 10.11.2018

คุณไม่สามารถใช้ create superuser ได้ในสถานการณ์ที่ผู้ใช้ไม่สามารถป้อนข้อมูลได้ ดู https://realpython.com/blog/python/deploying-a-django-app-to-aws-elastic-beanstalk/#Create.the.Admin.User สำหรับแนวทางอื่น

person user7218711    schedule 28.11.2016