'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:
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:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = 'static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'blog/static'),
]
Edit:
users/models.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
@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
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...
Solution 1:[1]
Can you post the contents of your models.py, signals.py, and apps.py file in your users app.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

