i am trying to use some image in my views.py, i did this,
from django.conf import settings
image = settings.STATIC_ROOT + "images/test.png"
fp = open(image, 'rb')
but it is saying:
IOError at /
(2, 'No such file or directory')
in my settings.py, i have:
STATIC_ROOT = os.path.join(PROJECT_PATH, "static")
STATIC_URL = '/static/'
what do i miss here ? STATICFILES_DIRS is empty but in templates, i has been working till now, but now i want to get image in my views.py.
You could try:
from django.contrib.staticfiles.views import serve
serve(request, 'images/test.png')
If memory serves me correct tho this will only work if debug is true.
But it sounds like its not finding the path, to find te project path...
Print(normpath(join(dirname(__file__), '..')))
This should help you identify and correct the problem.
Related
I am using Python PIL library in Django Rest Framework for building qr code and saving it to static directory.
def generate_qr(self, name):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_M,
box_size=15,
border=5
)
qr.add_data(name)
qr.make(fit=True)
img = qr.make_image(fill='black', back_color='white')
img.save(settings.STATIC_ROOT+"/asset-tags/name.png")
return(name+ '.png')
settings.py for media and static urls:
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = "/mediafiles/"
MEDIA_ROOT = os.path.join(BASE_DIR, "mediafiles")
But while saving it throws an error saying /usr/app/static/name.png : no file or directory.
I am creating new file so how can it find the image in given folder.
Any Help will be appreciated. Thanks.
The qrcode package or Pillow package won't create a directory if it doesn't exist. So, as per your settings, the STATIC_ROOT is located at /staticfiles and make sure that the directory named staticfiles (and it's sub-directories) exists before runs your script.
In other words, the statement img.save(settings.STATIC_ROOT+"/asset-tags/name.png") supposed to be save the QR code image under this path /staticfiles/asset-tags/name.png and make sure the directory /staticfiles/asset-tags/ exists in your project path.
NOTE: Use settings.MEDIA_ROOT instead of settings.STATIC_ROOT would be more appropriate.
in my Django app I´m trying to open and show pdf files that wasn't loaded to a model, but can´t find the appropriate way.
I have a group of PDF invoices that I manually copied to media/my_folder (I use Dropbox as media host to be able to do that).
Then I want to show each invoice when requested.
File object option
After investigating several posts, I think the way to do it is creating a file object dynamically.
invoice_path = "my_folder/" + invoice_number + ".pdf"
f = open(invoice_path, 'w')
myfile = File(f)
Then I understand the in the template I could be able to access the file as it was loaded to the model.
<p>Ver factura AFIP</p>7
I get a FileNotFoundError, I guess I´m not setting the path to media files correctly.
Exception Type: FileNotFoundError at /catalog/perfilfactura/FA B
0003-00000220 Exception Value: [Errno 2] No such file or
directory: 'media/Facturas_Electronicas/FA-B-0003-00000220.pdf'
This happens when trying to open the file. The path I set it's supposed to be relative to MEDIA_ROOT, just add the subfolder "my_folder" and then the pdf file name.
Path in template option
I also tried to set the path directly in the template as in:
<p><a href="media/{{ archivo_factura }}.pdf" download>Ver factura AFIP</a></p>
<p><a href="{{ MEDIA_URL }}Facturas_Electronicas/{{ archivo_factura }}.pdf" download>Ver factura AFIP</a></p>
In both cases I get a downloaded ampty PDFs.
My settings:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
And my Dropbox settings:
DEFAULT_FILE_STORAGE = 'storages.backends.dropbox.DropBoxStorage'
DROPBOX_OAUTH2_TOKEN = 'my_token'
DROPBOX_ROOT_PATH = '/Kinemed APP/'
Thanks!
There doesn't seem to be any reason to either open the file or create a File object. If your file is inside the media directory, all you need to do to create a URL for it is to join the media root:
invoice_path = os.path.join(settings.MEDIA_ROOT, "my_folder/" + invoice_number + ".pdf")
and now pass invoice_path directly to the template and access it there.
I'm trying to upload images using Mongoengine ImageField.
But after uploading a test image,I got page not found by visiting http://127.x.x.x:xxxx/media/testimage.png.
Raised by: django.views.static.serve
"/Users/xxx/Documents/basedir/media/testimage.png" does not exist
myproject/settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
myproject/myapp/models.py
from mongoengine import Document, ImageField
class Image(Document):
image = ImageField(upload_to="")
filename = fields.StringField()
myproject/urls.py
from django.conf import settings
urlpatterns = [
#bla
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
What did I do wrong?
You may want to have a look at this. It is fairly thorough and provides every step needed to configure static files. 404 Error for django serving static files. How to setup django to serve static files?. It appears you are definitely missing STATICFILES_DIRS and also have you ran python manage.py collectstatic command?
It would also be good to confirm you upload_to of the ImageField is also correct. You can verify this in the shell. python manage.py shell
I have Django Grappelli == 2.6.5 with Filebrowser and Tinymce. I use Django ==1.7.5.
I have problem with uploading images. Image uploads in the browser, appears in the folder \media\uploads but Filebrowser doesn't see it and throws an error: 'media\uploads\img.jpg' could not be found
Here is my settings:
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(PROJECT_ROOT, STATIC_URL.strip("/"))
MEDIA_URL = STATIC_URL + "media/"
MEDIA_ROOT = os.path.join(PROJECT_ROOT, *MEDIA_URL.strip("/").split("/"))
DIRECTORY = getattr(settings, "FILEBROWSER_DIRECTORY", 'uploads/')
In the pyhon shell I did following:
from django.conf import settings
import filebrowser.settings
filebrowser.settings.MEDIA_ROOT
C:\\mysite\\static\\media
filebrowser.settings.DIRECTORY
uploads/
Why is this happened? How can I set the right upload directory to Django-Filebrowser? I've read documentation and searched for answears in google. But I don't understand how fix it and I'm confused. Can you help me please.
in your projects urls.py:
from django import views
....
urlpatterns = [
...
url(r'^media/(?P<path>.*)$', views.static.serve, {
'document_root': settings.MEDIA_ROOT})
...
]
So i've been googling this issue for the past hour and can't come up with a solution. Basically this is it: in my model.py i have a class that has this
class Case(models.Model):
zoomOutImage = models.ImageField('Label', upload_to="zoomOutImage")
and in my settings.py i have my media URL/ROOT set up like this
MEDIA_ROOT = os.path.join(os.path.abspath(''),'app/static/ds/')
MEDIA_URL = '/static/ds/'
which from the webserver should serve out like this:
http://127.0.0.1:8000/static/ds/zoomOutImage/actinic_granuloma_3.jpg
I've installed PIL (inside virtualENV) and there are no errors in uploading, the only issue is when i try uploading the file via the admin panel nothing happens. No errors nothing. The file just simply doesn't get uploaded to the zoomOutImage folder by the development server. Can anyone point me towards why?
I guess your file is in a subdir of your root, subdir named 'zoomOutImage'. Or even a file called like that in the root. I remember putting a function call in the upload to string. That function creates a path and filename, using os.join and the filename from the instance. Doing this by head, no example code available right now. But must be able to google this.
Look here https://stackoverflow.com/questions/1190697/django-filefield-with-upload-to-determined-at-runtime
And by the way, I totally disagree with your answer, you should NEVER use absolute paths in your settings! See this answer use css in django 1.4 development for how to use the correct settings and refer to your Project PATH
EDIT (after reading your own answer)
Guess you are missing this first step:
this is the path to your settings.py file:
SETTINGS_DIR = os.path.dirname(os.path.realpath(__file__))
and than this is the path to your project dir: (I Am using buildout, so call it buildout, but it's the root of your project):
BUILDOUT_DIR = os.path.abspath(os.path.join(SETTINGS_DIR, '..'))
and from there on you can define everything you want:
STATIC_ROOT = os.path.join(BUILDOUT_DIR, 'var', 'static')
STATIC_URL = '/static_media/'
MEDIA_ROOT = os.path.join(BUILDOUT_DIR, 'var', 'media')
MEDIA_URL = '/media/'
and in your template file refer to the image like:
<img src="{{MEDIA_URL}}{{ case.zoomOutImage }}" width="100%">
when your object given to the template is called case
about your question of the urls.
you should add this:
if settings.DEBUG:
urlpatterns += patterns('',
(r'', include('staticfiles.urls')),
)
and see the link above to the question about using css, it's the same problem, but there for finding the css files during development. It's all about the static file places.
import os
# get abspath
def rel(*x):
return os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
MEDIA_ROOT = rel('media')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = '' #if only your static files are in project folder
STATICFILES_DIRS = ( rel('static'),) #if only your static files are in project folder
use this settings, and everything will work
so i finally solved my problem. For anyone having this issue in the future do the following:
if you're trying to serve static media files locally on the development server use absolute paths for MEDIA_ROOT and MEDIA_URL.