简体   繁体   English

使用 Heroku 创建 django 超级用户时出错 - FileNotFoundError

[英]Error when creating a django superuser with Heroku - FileNotFoundError

I'm trying to deploy a django application on Heroku, but when it comes to the part where I need to create a superuser, I get an error:我正在尝试在 Heroku 上部署 django 应用程序,但是当涉及到我需要创建超级用户的部分时,出现错误:

Traceback (most recent call last):
  File "/app/django_project/manage.py", line 22, in <module>
    main()
  File "/app/django_project/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 414, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 87, in execute
    return super().execute(*args, **options)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 460, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 232, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/auth/models.py", line 171, in create_superuser
    return self._create_user(username, email, password, **extra_fields)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/auth/models.py", line 154, in _create_user
    user.save(using=self._db)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/auth/base_user.py", line 68, in save
    super().save(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 806, in save
    self.save_base(
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 872, in save_base
    post_save.send(
  File "/app/.heroku/python/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 176, in send
    return [
  File "/app/.heroku/python/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 177, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "/app/django_project/users/signals.py", line 18, in create_profile
    Profile.objects.create(user=instance)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/query.py", line 514, in create
    obj.save(force_insert=True, using=self.db)
  File "/app/django_project/users/models.py", line 20, in save
    img = Image.open(self.image.path)
  File "/app/.heroku/python/lib/python3.9/site-packages/PIL/Image.py", line 3068, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: '/app/django_project/media/default.jpg'

Here's my project structure:这是我的项目结构:

│   .gitignore
│   Pipfile
│   Pipfile.lock
│   Procfile
│   README.md
│
├───.idea
│   │   .gitignore
│   │   BlogProject.iml
│   │   discord.xml
│   │   misc.xml
│   │   modules.xml
│   │   vcs.xml
│   │   workspace.xml
│   │
│   └───inspectionProfiles
│           profiles_settings.xml
│           Project_Default.xml
│
└───django_project
    │   db.sqlite3
    │   manage.py
    │   posts.json
    │
    ├───blog
    │   │   admin.py
    │   │   apps.py
    │   │   models.py
    │   │   tests.py
    │   │   urls.py
    │   │   views.py
    │   │   __init__.py
    │   │
    │   ├───migrations
    │   │   │   0001_initial.py
    │   │   │   __init__.py
    │   │   │
    │   │   └───__pycache__
    │   │           0001_initial.cpython-39.pyc
    │   │           __init__.cpython-39.pyc
    │   │
    │   ├───static
    │   │   └───blog
    │   │           main.css
    │   │
    │   ├───templates
    │   │   └───blog
    │   │           about.html
    │   │           base.html
    │   │           home.html
    │   │           post_confirm_delete.html
    │   │           post_detail.html
    │   │           post_form.html
    │   │           user_posts.html
    │   │
    │   └───__pycache__
    │           admin.cpython-39.pyc
    │           apps.cpython-39.pyc
    │           models.cpython-39.pyc
    │           urls.cpython-39.pyc
    │           views.cpython-39.pyc
    │           __init__.cpython-39.pyc
    │
    ├───django_project
    │   │   asgi.py
    │   │   settings.py
    │   │   urls.py
    │   │   wsgi.py
    │   │   __init__.py
    │   │
    │   └───__pycache__
    │           settings.cpython-39.pyc
    │           urls.cpython-39.pyc
    │           wsgi.cpython-39.pyc
    │           __init__.cpython-39.pyc
    │
    ├───media
    │   │   default.jpg
    │   │
    │   └───profile_imgs
    │           10.jpg
    │           depositphotos_39258143-stock-illustration-businessman-avatar-profile-picture.jpg
    │           perfil_face02.jpg
    │
    ├───staticfiles
    │   ├───admin
    │   │   ├───css
    │   │   │   │   autocomplete.css
    │   │   │   │   base.css
    │   │   │   │   changelists.css
    │   │   │   │   dashboard.css
    │   │   │   │   fonts.css
    │   │   │   │   forms.css
    │   │   │   │   login.css
    │   │   │   │   nav_sidebar.css
    │   │   │   │   responsive.css
    │   │   │   │   responsive_rtl.css
    │   │   │   │   rtl.css
    │   │   │   │   widgets.css
    │   │   │   │
    │   │   │   └───vendor
    │   │   │       └───select2
    │   │   │               LICENSE-SELECT2.md
    │   │   │               select2.css
    │   │   │               select2.min.css
    │   │   │
    │   │   ├───fonts
    │   │   │       LICENSE.txt
    │   │   │       README.txt
    │   │   │       Roboto-Bold-webfont.woff
    │   │   │       Roboto-Light-webfont.woff
    │   │   │       Roboto-Regular-webfont.woff
    │   │   │
    │   │   ├───img
    │   │   │   │   calendar-icons.svg
    │   │   │   │   icon-addlink.svg
    │   │   │   │   icon-alert.svg
    │   │   │   │   icon-calendar.svg
    │   │   │   │   icon-changelink.svg
    │   │   │   │   icon-clock.svg
    │   │   │   │   icon-deletelink.svg
    │   │   │   │   icon-no.svg
    │   │   │   │   icon-unknown-alt.svg
    │   │   │   │   icon-unknown.svg
    │   │   │   │   icon-viewlink.svg
    │   │   │   │   icon-yes.svg
    │   │   │   │   inline-delete.svg
    │   │   │   │   LICENSE
    │   │   │   │   README.txt
    │   │   │   │   search.svg
    │   │   │   │   selector-icons.svg
    │   │   │   │   sorting-icons.svg
    │   │   │   │   tooltag-add.svg
    │   │   │   │   tooltag-arrowright.svg
    │   │   │   │
    │   │   │   └───gis
    │   │   │           move_vertex_off.svg
    │   │   │           move_vertex_on.svg
    │   │   │
    │   │   └───js
    │   │       │   actions.js
    │   │       │   autocomplete.js
    │   │       │   calendar.js
    │   │       │   cancel.js
    │   │       │   change_form.js
    │   │       │   collapse.js
    │   │       │   core.js
    │   │       │   inlines.js
    │   │       │   jquery.init.js
    │   │       │   nav_sidebar.js
    │   │       │   popup_response.js
    │   │       │   prepopulate.js
    │   │       │   prepopulate_init.js
    │   │       │   SelectBox.js
    │   │       │   SelectFilter2.js
    │   │       │   urlify.js
    │   │       │
    │   │       ├───admin
    │   │       │       DateTimeShortcuts.js
    │   │       │       RelatedObjectLookups.js
    │   │       │
    │   │       └───vendor
    │   │           ├───jquery
    │   │           │       jquery.js
    │   │           │       jquery.min.js
    │   │           │       LICENSE.txt
    │   │           │
    │   │           ├───select2
    │   │           │   │   LICENSE.md
    │   │           │   │   select2.full.js
    │   │           │   │   select2.full.min.js
    │   │           │   │
    │   │           │   └───i18n
    │   │           │           af.js
    │   │           │           ar.js
    │   │           │           az.js
    │   │           │           bg.js
    │   │           │           bn.js
    │   │           │           bs.js
    │   │           │           ca.js
    │   │           │           cs.js
    │   │           │           da.js
    │   │           │           de.js
    │   │           │           dsb.js
    │   │           │           el.js
    │   │           │           en.js
    │   │           │           es.js
    │   │           │           et.js
    │   │           │           eu.js
    │   │           │           fa.js
    │   │           │           fi.js
    │   │           │           fr.js
    │   │           │           gl.js
    │   │           │           he.js
    │   │           │           hi.js
    │   │           │           hr.js
    │   │           │           hsb.js
    │   │           │           hu.js
    │   │           │           hy.js
    │   │           │           id.js
    │   │           │           is.js
    │   │           │           it.js
    │   │           │           ja.js
    │   │           │           ka.js
    │   │           │           km.js
    │   │           │           ko.js
    │   │           │           lt.js
    │   │           │           lv.js
    │   │           │           mk.js
    │   │           │           ms.js
    │   │           │           nb.js
    │   │           │           ne.js
    │   │           │           nl.js
    │   │           │           pl.js
    │   │           │           ps.js
    │   │           │           pt-BR.js
    │   │           │           pt.js
    │   │           │           ro.js
    │   │           │           ru.js
    │   │           │           sk.js
    │   │           │           sl.js
    │   │           │           sq.js
    │   │           │           sr-Cyrl.js
    │   │           │           sr.js
    │   │           │           sv.js
    │   │           │           th.js
    │   │           │           tk.js
    │   │           │           tr.js
    │   │           │           uk.js
    │   │           │           vi.js
    │   │           │           zh-CN.js
    │   │           │           zh-TW.js
    │   │           │
    │   │           └───xregexp
    │   │                   LICENSE.txt
    │   │                   xregexp.js
    │   │                   xregexp.min.js
    │   │
    │   └───blog
    │           main.css
    │
    └───users
        │   admin.py
        │   apps.py
        │   forms.py
        │   models.py
        │   signals.py
        │   tests.py
        │   urls.py
        │   views.py
        │   __init__.py
        │
        ├───migrations
        │   │   0001_initial.py
        │   │   __init__.py
        │   │
        │   └───__pycache__
        │           0001_initial.cpython-39.pyc
        │           __init__.cpython-39.pyc
        │
        ├───templates
        │   └───users
        │           login.html
        │           logout.html
        │           password_reset.html
        │           password_reset_complete.html
        │           password_reset_confirm.html
        │           password_reset_done.html
        │           profile.html
        │           register.html
        │
        └───__pycache__
                admin.cpython-39.pyc
                apps.cpython-39.pyc
                forms.cpython-39.pyc
                models.cpython-39.pyc
                signals.cpython-39.pyc
                urls.cpython-39.pyc
                views.cpython-39.pyc
                __init__.cpython-39.pyc

An image of it, just in case it's more readable:它的图像,以防它更具可读性:

在此处输入图像描述

On my settings.py file I have:在我的settings.py文件中,我有:

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = 'static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'blog/static'),
]

Edit:编辑:

users/models.py用户/模型.py

class Profile(models.Model):
    # One to one relationship
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    image = models.ImageField(default='default.jpg', upload_to='profile_imgs')

    def __str__(self):
        return f"{self.user.username} Profile"

    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

        img = Image.open(self.image.path)

        # Resizing the image if larger than wanted:
        if img.height > 300 or img.width > 300:
            output_size = (300, 300)
            img.thumbnail(output_size)
            img.save(self.image.path)

users/signals.py用户/信号.py

@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)


@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
    instance.profile.save()

users/apps.py用户/apps.py

class UsersConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'users'

    def ready(self):
        import users.signals

What is happening and how can I fix it?发生了什么,我该如何解决? Thank you.谢谢你。

OBS: Stackoverflow won't let me post my question because it's mostly code, but I don't know what else to add that will contribute to the understanding of the question... OBS:Stackoverflow 不会让我发布我的问题,因为它主要是代码,但我不知道还有什么要添加的内容有助于理解问题......

您可以在您的用户应用程序中发布您的models.pysignals.pyapps.py文件的内容吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM