my file uploads missing/deleted

243 views
Skip to first unread message

omar ahmed

unread,
Jun 7, 2019, 11:32:21 PM6/7/19
to Django users
hii ..
i deployed my first django app on Heroku https://arena3.herokuapp.com/
but my media files ( like club logo ) disappear or deleted ...
i found this article on Heroku https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted 
that means The Heroku filesystem is ephermal and i should use aws but it's not free ?
is there any solution ?
how can i use Heroku postgres (addons) to upload my files permanently .. thanks in advance

Joe Reitman

unread,
Jun 8, 2019, 12:59:20 AM6/8/19
to Django users
Omar,

I use Dropbox which is free up to 2GB. I installed django-storages library to make setting up Dropbox easy.  

omar ahmed

unread,
Jun 8, 2019, 1:23:17 PM6/8/19
to Django users
ok joe .... how can i use Dropbox for this issue ..

Joe Reitman

unread,
Jun 8, 2019, 6:23:08 PM6/8/19
to Django users
Follow the django-storages documentation. https://django-storages.readthedocs.io/en/latest/backends/dropbox.html

It's easy to setup.

omar ahmed

unread,
Jun 9, 2019, 2:07:01 PM6/9/19
to Django users
i installed Dropbox for django environment
but how to link an account and adjust settings
tutorial is not clear
thanks

Joe Reitman

unread,
Jun 9, 2019, 9:16:19 PM6/9/19
to Django users
Omar,

Looking at your original request I misunderstood what you wanted to do. I thought you wanted to configure media files which are uploaded by the user. 

For static files you have many free options to store them and put the links in your img tags. If you have a codepen.io account, you can upload your images to codepen and use the links generated by codepen. Another option is to install surge.sh and upload images to a surge static site and then use the links to the images. If that makes sense.

I have used both options to store my static files in various projects. These options are good for testing and development.

omar ahmed

unread,
Jun 10, 2019, 2:17:42 AM6/10/19
to Django users
this is explanation for my problem
my site is for football news My Media Files are uploaded to the app but then disappear .... like clubs logo here
https://arena3.herokuapp.com/8/clubpage/
when i created any club logo appear but after minutes disappear
i don't mean files uploaded by the users ( but by me in admin dashboard)
Message has been deleted

Joe Reitman

unread,
Jun 10, 2019, 8:57:23 AM6/10/19
to Django users
A couple of things to check.
<img src="/media/media/core/sevilla.png" alt="إشبيلية logo" height="100" width="100">
1. Is the photo located in directory above?
2. Did you run collectstatic?
3. In your settings.py are STATIC_ROOT, STATIC_URLS and STATICFILES_DIRS configured
4. In your template you have setup like this example:
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image">

Also, it looks like you have DEBUG set to True. When DEBUG is set to True, Django will use it's development static file server to serve static files. However, setting DEBUG true in production is not recommended. 

Note that when you set DEBUG to False, the Django static file server is disabled. Hence the need to host static files on a CDN or using a static file server like Whitenoise. You can serve static files within your Django app using Whitenoise and is recommended by Heroku. The other option is to serve static files from external sources.

If you only have one photo, you can do what I suggested using codepen.io or surge.sh or almost any other remote storage that allows you to 'hotlink' image files.

omar ahmed

unread,
Jun 10, 2019, 11:58:04 AM6/10/19
to Django users
hiii Sagar ..
what is your Error ?
i followed this tutorial
https://medium.com/@BennettGarner/deploying-django-to-heroku-procfile-static-root-other-pitfalls-e7ab8b2ba33b
if you want to connect heroku postgres continue with this
https://medium.com/@BennettGarner/deploying-django-to-heroku-connecting-heroku-postgres-fcc960d290d1
start by the first article and don't forget to use suitable version of python
if you have errors just type
heroku logs -tail
good luck

On Monday, June 10, 2019 at 7:18:08 AM UTC+2, sagar ninave wrote:
omar ahmad i want to know how you have deployed your site on herokuapp, because i am getting application error

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/62dc15f9-3e2f-4681-b29c-873b8dd040b8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

omar ahmed

unread,
Jun 10, 2019, 7:37:58 PM6/10/19
to Django users
almost i did all of these instructions
collectstatic - DEBUG = True  - install and use whitenoise in my app
the problem is heroku is ephemeral ( that's mean image clubs appear and after one hour it disappear )
i want to use something like Dropbox (permenant) but i don't know how to  make it my media folder
r1.png
r2.png

Joe Reitman

unread,
Jun 11, 2019, 2:10:59 PM6/11/19
to Django users
Omar,

You need to put the image in your 'static' files directory. 

<img src="/media/media/core/sevilla.png" alt="Seville logo" height="100" width="100">

James Farris

unread,
Jun 11, 2019, 4:14:10 PM6/11/19
to Django users
He is saying these aren’t static files. It sounds like he uploads them using the Django admin, which of course is like an end user on a client uploading files to the media directory.

Since that is the case it sounds like the Dropbox option is the best. Documentation was provided earlier. So being that it seems like he read through the documentation, my question is where is he stuck in the Dropbox implementation?

omar ahmed

unread,
Jun 11, 2019, 9:16:43 PM6/11/19
to Django users
yes James you are right.... i need to upload images using the Django admin, ... how can i use Dropbox for this issue ?
i need a good tutorial

James Farris

unread,
Jun 11, 2019, 11:13:11 PM6/11/19
to django...@googlegroups.com

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/8O8BS7DhF30/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Joe Reitman

unread,
Jun 12, 2019, 2:34:23 PM6/12/19
to Django users
Omar,

To upload images from the Admin panel, you will have to configure your apps' admin.py file. I use the django-imagekit package to manipulate user uploaded images. This package also provides the ability to upload images via the Admin panel with a couple lines of code. Here is my Admin.py code. 

from django.contrib import admin
from imagekit.admin import AdminThumbnail
from .models import MapData


class PhotoAdmin(admin.ModelAdmin):

 list_display
= ('__str__', 'admin_thumbnail')
 admin_thumbnail
= AdminThumbnail(image_field='image')


admin
.site.register(MapData, PhotoAdmin)



It displays thumbnails of all the images stored in my database and presents them in a list view in the Admin panel alongside the other model data.

omar ahmed

unread,
Jun 12, 2019, 10:03:56 PM6/12/19
to Django users
can django-imagekit upload my images without (Dropbox or AWS s3 ) ?

omar ahmed

unread,
Jun 12, 2019, 10:09:53 PM6/12/19
to Django users
i opened the documentation for django-storages ... and i installed Dropbox in my environment and i created my app folder  but i didn't complete docs .
somethings not clear ... like :

Link an account
dbx = dropbox.Dropbox('YOUR_ACCESS_TOKEN')
dbx.users_get_current_account()

Try some API requests

for entry in dbx.files_list_folder('').entries:
    print(entry.name)
where should i write these commands ?
thanks


On Wednesday, June 12, 2019 at 1:13:11 AM UTC+2, James Farris wrote:
On Tue, Jun 11, 2019 at 2:17 PM omar ahmed <omark...@gmail.com> wrote:
yes James you are right.... i need to upload images using the Django admin, ... how can i use Dropbox for this issue ?
i need a good tutorial

On Tuesday, June 11, 2019 at 6:14:10 PM UTC+2, James Farris wrote:
He is saying these aren’t static files. It sounds like he uploads them using the Django admin, which of course is like an end user on a client uploading files to the media directory.

Since that is the case it sounds like the Dropbox option is the best. Documentation was provided earlier. So being that it seems like he read through the documentation, my question is where is he stuck in the Dropbox implementation?

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/8O8BS7DhF30/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django...@googlegroups.com.

James Farris

unread,
Jun 12, 2019, 10:38:11 PM6/12/19
to django...@googlegroups.com
You would add this code to the views.py file, which is where the business logic goes. 


Likely would add a class with properties and methods or a definition (a function) like below:

For example in your apps views.py file:

def connect_to_dropbox():
    dbx = dropbox.Dropbox(‘your access token)
    dbx.users_get_current_account()

    # this stuff likely would go in a separate function
    for entry in dbx.files_list_folder(‘’).entries:
        print(entry.name)


At some point in your code you would call the function:
connect_to_dropbox()

To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.
--
James Farris
Web Application Developer | Web and Digital Solutions
External Relations & Advancement Marketing Communications
University Development and Alumni Relations | UC Berkeley


omar ahmed

unread,
Jun 12, 2019, 10:52:57 PM6/12/19
to Django users
i added this function at the end of views.py file
def connect_to_dropbox():
    dbx = dropbox.Dropbox(‘your access token)
    dbx.users_get_current_account()
and i put given access token from dropbox
then what about call
do you mean in any view function that will show page included images add 
connect_to_dropbox()
and what about

for entry in dbx.files_list_folder('').entries:
    print(entry.name)



James Farris

unread,
Jun 12, 2019, 11:03:10 PM6/12/19
to django...@googlegroups.com
This python package may be a better choice for you, since I assume you want all user generated content (images) to be stored in Dropbox. 

This has pretty good documentation. 
If you use this package, remove the sample code I sent


To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

omar ahmed

unread,
Jun 13, 2019, 2:07:47 PM6/13/19
to Django users
i followed the steps
when i typed
python manage.py test [--settings=test_settings]
i found this error
ModuleNotFoundError: No module named '[--settings=test_settings]'
and when i tried to open my site (club page that has image) i foun this error
No module named 'StringIO'
i searched at Google but no similar issues

James Farris

unread,
Jun 13, 2019, 2:43:23 PM6/13/19
to django...@googlegroups.com
That means it wasn’t installed or it wasn’t added to your settings.py file in the installed apps section 

Sent from my mobile device
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

omar ahmed

unread,
Jun 13, 2019, 3:11:37 PM6/13/19
to Django users
i added django_dropbox_storage in installed apps in settings.py
what should i add else ?
thanks

omar ahmed

unread,
Jun 13, 2019, 3:22:36 PM6/13/19
to Django users
and this was Traceback
Environment:


Request Method: GET
Request URL: https://arena3.herokuapp.com/8/clubpage/

Django Version: 2.2.1
Python Version: 3.6.8
Installed Applications:
['core.apps.CoreConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django_social_share',
 'django_dropbox_storage']
Installed Middleware:
('whitenoise.middleware.WhiteNoiseMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Template error:
In template /app/templates/base.html, error at line 10
   No module named 'StringIO'
   1 : {% load static %}
   2 :
   3 :
   4 : <!DOCTYPE html>
   5 : <link href="https://fonts.googleapis.com/css?family=Bitter" rel="stylesheet">
   6 : <link href="https://fonts.googleapis.com/css?family=Merriweather" rel="stylesheet">
   7 : <html>
   8 :     <head>
   9 :         <title>{% block title %}{% endblock %}</title>
   10 :         <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integr ity="sha384-ggOyR0i XCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
   11 :         <!--<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">-->
   12 :         <link rel="stylesheet" type="text/css" href="{% static 'core/basepage.css'%}" />
   13 :     </head>
   14 :     <body>
   15 :
   16 :    <nav class="navbar" id="navbar">
   17 :     
   18 :       {% if user.is_authenticated %}
   19 :        <h4>logged in as {{ user.username }}</h4>
   20 :      {% endif %}


Traceback:

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in _resolve_lookup
  829.                     current = current[bit]

During handling of the above exception ('ImageFieldFile' object is not subscriptable), another exception occurred:

File "/app/.heroku/python/lib/python3.6/site-packages/django_dropbox_storage/storage.py" in <module>
  6.     from cStringIO import StringIO

During handling of the above exception (No module named 'cStringIO'), another exception occurred:

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/app/core/views.py" in clubpage
  86.     return render(request, 'core/clubpage.html', {'club':club, 'news':news})

File "/app/.heroku/python/lib/python3.6/site-packages/django/shortcuts.py" in render
  36.     content = loader.render_to_string(template_name, context, request, using=using)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/loader.py" in render_to_string
  62.     return template.render(context, request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/backends/django.py" in render
  61.             return self.template.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  171.                     return self._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in _render
  163.         return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  150.             return compiled_parent._render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in _render
  163.         return self.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/loader_tags.py" in render
  62.                 result = block.nodelist.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  937.                 bit = node.render_annotated(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render_annotated
  904.             return self.render(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in render
  987.             output = self.filter_expression.resolve(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in resolve
  671.                 obj = self.var.resolve(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in resolve
  796.             value = self._resolve_lookup(context)

File "/app/.heroku/python/lib/python3.6/site-packages/django/template/base.py" in _resolve_lookup
  837.                         current = getattr(current, bit)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/fields/files.py" in url
  62.         return self.storage.url(self.name)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/functional.py" in inner
  256.             self._setup()

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/files/storage.py" in _setup
  363.         self._wrapped = get_storage_class()()

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/files/storage.py" in get_storage_class
  358.     return import_string(import_path or settings.DEFAULT_FILE_STORAGE)

File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/module_loading.py" in import_string
  17.     module = import_module(module_path)

File "/app/.heroku/python/lib/python3.6/importlib/__init__.py" in import_module
  126.     return _bootstrap._gcd_import(name[level:], package, level)

File "<frozen importlib._bootstrap>" in _gcd_import
  994. <source code not available>

File "<frozen importlib._bootstrap>" in _find_and_load
  971. <source code not available>

File "<frozen importlib._bootstrap>" in _find_and_load_unlocked
  955. <source code not available>

File "<frozen importlib._bootstrap>" in _load_unlocked
  665. <source code not available>

File "<frozen importlib._bootstrap_external>" in exec_module
  678. <source code not available>

File "<frozen importlib._bootstrap>" in _call_with_frames_removed
  219. <source code not available>

File "/app/.heroku/python/lib/python3.6/site-packages/django_dropbox_storage/storage.py" in <module>
  8.     from StringIO import StringIO

Exception Type: ModuleNotFoundError at /8/clubpage/
Exception Value: No module named 'StringIO'

James Farris

unread,
Jun 13, 2019, 3:24:56 PM6/13/19
to django...@googlegroups.com
When you run the server does it give you any errors?

To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

James Farris

unread,
Jun 13, 2019, 3:36:00 PM6/13/19
to django...@googlegroups.com
A quick google search took me here


To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

Joe Reitman

unread,
Jun 13, 2019, 4:15:02 PM6/13/19
to Django users
Omar, 

You should not be configuring views for Dropbox. DJANGO-STORAGES does all the back end coding for you.

Follow these steps from DJANGO-STORAGES documentation after removing the code changes you made to views. I'm assuming your using pip.

1. pip install django-storages
2. pip install dropbox
3. In your settings.py set these variables
    DEFAULT_FILE_STORAGE = 'storages.backends.dropbox.DropBoxStorage'
    DROPBOX_OAUTH2_TOKEN = your token from dropbox
    DROPBOX_ROOT_PATH = your root path to store media
FROM THE DROPBOX DOCUMENTATION TUTORIAL YOU ONLY NEED TO REGISTER YOUR APP AND GENERATE YOUR OAUTH2 TOKEN. DISREGARD THE 'LINK YOUR ACCOUNT'. DJANGO STORAGES DOES THAT FOR YOU.

4. pip install django-imagekit and add it to your settings.py 'installed apps' -read the docs on this one

Now you need to have an imagefield in your model and config your admin.py as I explained earlier. 
I highly recommend creating an app to do this functionality. Call it something like 'logo upload'. Django best practices says an app does one thing and does it well.

omar ahmed

unread,
Jun 13, 2019, 8:44:20 PM6/13/19
to Django users
i did first and second steps
but what about
DROPBOX_OAUTH2_TOKEN = your token from dropbox
did you mean Generate access token ??

DROPBOX_ROOT_PATH = your root path to store media
did you mean link to Dropbox app folder ?
thanks

Joe Reitman

unread,
Jun 14, 2019, 10:31:31 AM6/14/19
to Django users
Yes. Generate an access token from your Dropbox App Console and put it in your settings.py. The token is used to authenticate your Django app with your Dropbox app.

DROPBOX_ROOT_PATH is the folder in your Dropbox App where you want to store your photos. You need to define it with this variable in settings.py.
 Typically I call it 'images' like this:  
DROPBOX_ROOT_PATH = '/images/'

omar ahmed

unread,
Jun 14, 2019, 12:50:41 PM6/14/19
to Django users
ok i did all steps but it still doesn't work .. this is my settings.py file :
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

DEFAULT_FILE_STORAGE = 'storages.backends.dropbox.DropBoxStorage'
DROPBOX_OAUTH2_TOKEN = 'hSRexFBHo-AAAAAAAAAAWVBZOhZJo6jrIP3NgMK0L_7aWHkb0mJs-z4IWO6b_EBk'
DROPBOX_ROOT_PATH = '/arenaimages/'

and this is admin.py :
from django.contrib import admin
from imagekit.admin import AdminThumbnail
from .models import MapData
# Register your models here.

from .models import League, LeagueNews, Comment, Club, Match
admin.site.register(League)
admin.site.register(LeagueNews)
admin.site.register(Comment)

class ClubAdmin(admin.ModelAdmin):
fields = ['league_names', 'name', 'year_of_establishment', 'logo', 'won', 'draw', 'lost', 'total_points', 'goal_for', 'goal_against', 'goal_diff']
logo = AdminThumbnail(image_field='image')
readonly_fields = ('total_points', 'goal_diff',)

admin.site.register(Club, ClubAdmin)
admin.site.register(Match)
thanks joe

Joe Reitman

unread,
Jun 15, 2019, 2:45:09 PM6/15/19
to Django users
Did you create a processed image field in your model?
Reply all
Reply to author
Forward
0 new messages