I am using AWS to host my media files from my django app. I am following this tutorial, and when I use the following code,
from storages.backends.s3boto import S3BotoStorage
class MediaStorage(S3BotoStorage):
location = 'media'
MEDIA_URL = "https://%s/media/" % (AWS_S3_CUSTOM_DOMAIN)
DEFAULT_FILE_STORAGE = 'MediaStorage'
I get this error:
MediaStorage doesn't look like a module path
I have know idea why. Any thoughts?
You have to set the full module path for DEFAULT_FILE_STORAGE. See sample from documentation
DEFAULT_FILE_STORAGE
Default: 'django.core.files.storage.FileSystemStorage'
So put MediaStorage class in a storage.py module (path project/storage.py) and point DEFAULT_FILE_STORAGE='project.storage.MediaStorage'
Related
Hope you're doing well. I've got a django view that needs to check to see if a static file with a known path exists.
I'm currently trying to use os.path.isfile(), but I have a feeling this is the wrong approach.
views.py:
import os
def knighthood_ceremony(warrior):
narration_path = static("polls/narrations/{}/{}.mp3".format(queen_name,warrior.name))
is_to_play_narration = os.path.isfile(narration_path)
settings.py:
STATIC_URL = '/static/'
Structure:
mysite
mysite
settings.py
polls
static
polls
narrations
5
1.mp3
views.py
otherproject
This gives local vars:
narration_path = '/static/polls/narrations/5/1.mp3'
is_to_play_narration = False
But, there is a file at:
C:\Users\{USER}\mysite\polls\static\polls\narrations\5\1.mp3
Why can't os.path.isfile see it? Any advice will be greatly appreciated!
Are you trying to read it via the URL or the STATIC_ROOT? If you are reading from the harddrive. Might be the error there
(Django 2.0, Django Rest Framework 3.8, Python 3.6, Django Storages 1.7, Dropbox 9.1)
I'm trying to upload a file to the Dropbox App Folder I've created, but I run into the same error at every attempt:
C:/TrainerPics/UI_4.png' did not match pattern '(/(.|[\r\n])*|id:.*)|(rev:[0-9a-f]{9,})|(ns:[0-9]+(/.*)?)'
On the Dropbox dashboard from desktop, the folder I want to upload to is shown as:
Dropbox > Apps > DjangoAppNameHere
Here's my setup in settings.py:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
DEFAULT_FILE_STORAGE = 'storages.backends.dropbox.DropBoxStorage'
DROPBOX_OAUTH2_TOKEN = 'some_token_here'
where storages is also listed in installed apps. Here's the model field I'm trying to upload:
trainer_profile_pic = models.ImageField(upload_to="TrainerPics/", null=True, blank=True)
I've tried this both with and without the / character, and tried this using upload_to=DjangoAppNameHere both with FileField and ImageField with no success. The documentation for Dropbox is rather sparse in the django-storages package, and doesn't describe how to set up a field to get it working.
That error message is coming from the Dropbox API, and it's indicating that the supplied path doesn't match the expected format. That is, when uploading to Dropbox via the Dropbox API, you're expected to supply the desired path for the uploaded file, in a format that matches the supplied pattern.
The most commonly used format for that is the first portion of that pattern, which is just a "/"-delimited path for the remote path relative to the root in Dropbox. For example: '/TrainerPics/UI_4.png'.
Based on the output, you're instead supplying a path that looks like a local Windows filesystem path: 'C:/TrainerPics/UI_4.png'.
You'll need to update the app/configuration to supply the remote path instead.
This is an old question, just to help people who might have similar problems.
There are two options that I used.
Use a linux machine when uploading your files. This will allow the API to upload your image as it's probably the same filesystem as their servers.
Only use the DROPBOX API on the server, and use local filestorage on local development. Servers run on unix and it does not produce the error.
Until not long ago, I used to do this in many places in my Django app:
from MyApp import settings
This required that I put settings inside my MyApp directory. I realized that is wrong, so I started using:
from django.conf import settings
But now I can't figure out how to find the path to settings.py from withing my code. Earlier I could use settings.__file__.
As a workaround I defined inside settings.py:
PATHTOSELF = os.path.dirname(__file__)
Any ideas how to find the path to settings.py?
Try This:
import os
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))
This should work for you:
import os
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
Place it on top inside of your settings.py. You can then import PROJECT_ROOT at any point in your code like this:
from myapp.settings import PROJECT_ROOT
I am using s3boto, django-pipeline and django-storages with this setting:
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = 'utils.classutils.S3PipelineStorage'
MEDIA_ROOT = "/"
MEDIA_URL = '//s3.amazonaws.com/%s/' % AWS_STORAGE_BUCKET_NAME
STATIC_ROOT = '/static/'
STATIC_URL = '//s3.amazonaws.com/%s/static/' % AWS_STORAGE_BUCKET_NAME
STATICFILES_DIRS = (
os.path.join(SITE_ROOT, 'assets'),
)
and this custom storage for django-pipeline
from django.contrib.staticfiles.storage import CachedFilesMixin
from pipeline.storage import PipelineMixin
from storages.backends.s3boto import S3BotoStorage
class S3PipelineStorage(PipelineMixin, CachedFilesMixin, S3BotoStorage):
pass
but I keep on getting:
ValueError: The file 'project/head.png' could not be found with <utils.classutils.S3PipelineStorage object at 0x2163510>.
but this file does not exist anywhere! not in plugins, I tried finding it, I checked my static dirs, tried finding it in admin, and I don't even remember working with a file named like this! I tried findstatic and the file can't be found.
What could I be missing?
You certainly have a css file that point to a non-existing 'project/head.png' file, remove this reference, and it should work.
Short tip. I encountered this when I was setting up S3 for serving static files.
I wanted to serve static content from s3 and keep handling uploaded files to the media folder on the local machine. What I had then was one image that was referenced in a css-file on s3 that was pointing to an image in the media folder like this background: url(../media/some/image.png).
Obviously that file could not be found in s3 with the relative path that was set in the css and the upload crashed. It works locally but not when running staticfiles from s3.
In my django application structure is as follows:
__init__.py
__init__.pyc
models.py
tests.py
usage.rrd
views.py
views.pyc
I am trying to access the usage.rrd file in views.py as follows:
from django.http import HttpResponse
import rrdtool
def hello(request):
info = rrdtool.info('usage.rrd')
return HttpResponse(info)
but I am getting:
opening '/usage.rrd': No such file or
directory
despite being in the current directory.
The application starts from the root directory of your project. So you you need to fix your path, ('app-name/usage.rrd') .
As mentioned in thebwt's answer it's a path issue. The working directory for your django environment will not be that of you app, hence the failure to locate your file.
You will have to specify either a full path to your rrd file (not ideal) or a path relative to the working directory.
Alternatively, a common trick to deal with issues like this is to extract the directory a particular python module is in (using __file__), and using that to translate a paths (relative to the current file) to an absolute path. For example:
FILE_DIR = os.path.realpath(os.path.dirname(__file__))
rel_to_abs = lambda *x: os.path.join(FILE_DIR, *x)
In your case:
# in view.py
import os
import rrdtools
FILE_DIR = os.path.realpath(os.path.dirname(__file__))
rel_to_abs = lambda *x: os.path.join(FILE_DIR, *x)
def hello(request):
info = rrdtool.info(rel_to_abs('usage.rrd'))
return HttpResponse(info)
p.s. you might have seen this method being used in settings.py to specify paths to sqlite3 DATABASE_NAME, MEDIA_ROOT, etc. such that the absolute path is obtained at runtime rather than being hardcoded.