I'm running a data crawler on a Windows 7 machine. I'm inserting the results remotely to my Django 1.10 project on my CentOS 7 server. I have a copy of the Django project on both machines. This works fine for all fields in the model, except the ImageField.
Here is the part of my script that does the saving.
m = Object(strings=strings)
m.save()
image_content = ContentFile(requests.get(image_url).content, id + '.jpg')
m.image_file.save("C:\\Users\\Me\\Documents\\mysite.com\\imgs\\" + id + ".jpg", image_content)
m.save()
The image field is declared as:
image_file = models.ImageField(upload_to='avatars/', null=True, default=None)
My settings.py file on the Windows machine has the line:
MEDIA_ROOT = "/var/www/mysite.com/myproj/images/"
On the first run, there are no errors but the image_feild on the server is set to "."
On the second run, the error is:
IOError: C:\var\www\mysite.com\myproj\images exists and is not a directory.
So this is being created on the Windows machine, but I want the MEDIA_ROOT to be used as the destination directory on the server.
Either use os module.
import os
if os.name == 'nt':
# Windows code.
else:
# Unix code.
Or user relative paths.
# In settings.
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(BASE_DIR, '../media_root')
# In other places.
mage_file.save(os.path.join(MEDIA_ROOT, '/dir_name/file_name.jpg'), image_content)
Related
I have deployed my django project on google cloud. One module of my app involves uploading files. When I am uploading my files on local server the files are successfully uploading but when I am trying to upload the files from the production server it is giving me the following error:
OSError at /uploadrecords
[Errno 30] Read-only file system: '/workspace/media/test.pdf'
Following is my code for uploading files and the settings:
#views.py
image = request.FILES['filerecord']
fs = FileSystemStorage()
filename = fs.save(image.name, image)
obj.upfile = fs.url(filename)
obj.save()
#setting.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
#MEDIA_ROOT = BASE_DIR / "media"
Kindly suggest me a solution
I assume you're using App Engine standard.
The file system of hosted apps is constrained:
https://cloud.google.com/appengine/docs/standard/python3/using-temp-files
You can use /tmp for temporary storage of uploads.
You can want to consider e.g. Google Cloud Storage for persistent storage of files.
The io module provides Python’s main facilities for dealing with various types of I/O.
Try to use it
import io
io.BytesIO(some_data)
I'm programming in a MacOS and using Python + Django. I must to get some files in our private network (Windows network) and move them to our server. There, Python/Django will read these files and save the data in a database. How can I do that?
What I have tried
source_path = "smb://server-name/GRUPOS/TECNOLOGIA_INFORMACAO/Dashboard Diretoria/"
dest_path = "./static/reports/". # This is my static folder where I want to move the file
file_name = "general_reports.csv"
shutil.copyfile(source_path + file_name, dest_path + file_name)
It gives the follow error:
[Errno 2] No such file or directory:
'smb://server-name/GRUPOS/TECNOLOGIA_INFORMACAO/Dashboard
Diretoria/general_reports.csv'
This path (source_path) I just copied and past from the Finder, so... I think that it's correct. I have already searched at StackOverflow and I have tried other methods like put "r" before the path... Nothing....
Technologies used
Python 3.6;
Django 3.0.5;
Mac OSX;
Windows Network.
Thank you for your help and patience.
You need to import an SMB client libary for python or you mount that drive before you work there
First of all, thank you #Van de Wack.
This is the complete solution:
Install the pysmb library (https://pypi.org/project/pysmb/):
pip install pysmb
Import the library to your code:
from smb.SMBConnection import SMBConnection
The follow code is a example to list all directories:
server_ip = "10.110.10.10" # Take your server IP - I have put a fake IP :)
server_name = 'myserver' # The servername for the IP above
share_name = "GRUPOS" # This is the principal folder of your network that you want's to connect
network_username = 'myuser' # This is your network username
network_password = '***' # This is your network password
machine_name = 'myuser#mac-mc70006405' # Your machine name
conn = SMBConnection(network_username, network_password, machine_name, server_name, use_ntlm_v2 = True)
assert conn.connect(server_ip, 139)
files = conn.listPath(share_name, "/TECNOLOGIA_INFORMACAO/Dashboard Diretoria/")
for item in files:
print(item.filename)
I need to upload an image file to a certain folder. It works fine on localhost but if I push the app to heroku it tells me:
IOError: [Errno 2] No such file or directory: 'static/userimg/1-bild-1.jpg'
Which means it cant find the folder?
I need to store the image files there for a few seconds to perform some actions on theme. After that they will be sent to AWS and will be deleted from the folder.
Thats the code where I save the images into the folder:
i = 1
for key, file in request.files.iteritems():
if file:
filename = secure_filename(str(current_user.id) + "-bild-"+ str(i) +".jpg")
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
i = i + 1
Later I get the files from the folder like this:
for item in os.listdir(os.path.join(app.config['UPLOAD_FOLDER'])):
if item.startswith(str(current_user.id)) and item.split(".")[0].endswith("1"):
with open(os.path.join(app.config['UPLOAD_FOLDER'], item), "rb") as thefile:
data = base64.b64encode(thefile.read())
upload_image_to_aws_from_image_v3('MYBUCKET', "userimg/", data, new_zimmer, "hauptbild", new_zimmer.stadt, new_zimmer.id)
os.remove(str(thefile.name))
That is the upload folder in app config:
UPLOAD_FOLDER = "static/userimg/"
Everything works fine on localhost.
I had to add the absolute path to the directory, because the path on the server is different.
You can run heroku run bash for your app through the CLI and check the directories with dir. Use then cd .. to go back or cd *directory name* to go into the directory.
I had to add
MYDIR = os.path.dirname(__file__)
and replace all
os.path.join(app.config['UPLOAD_FOLDER']
with
os.path.join(MYDIR + "/" + app.config['UPLOAD_FOLDER']
Some informations on the topic also here:
Similar question
I am developing a flask application under Linux, and i'm suffering when i make any changes to template files.
Actually i well configured my app to reload on template changes using
TEMPLATES_AUTO_RELOAD = True
PS: when i develop under Windows templates are reloading normally.
EDIT
I am using the built in server, and i run my app like this :
app = create_app()
manager = Manager(app)
#manager.command
def run():
"""Run in local machine."""
app.run(threaded=True)
Here is my configuration class
class DefaultConfig(object):
# Project name
PROJECT = "***"
# Turns on debugging features in Flask
DEBUG = True
# secret key
SECRET_KEY = "**************"
# Configuration for the Flask-Bcrypt extension
BCRYPT_LEVEL = 12
# Application root directory
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
# Application email
MAIL_FROM_EMAIL = "**********"
# Upload directory
UPLOAD_DIR = "static/uploads/"
# Avater upload directory
UPLOAD_AVATAR_DIR = os.path.join(UPLOAD_DIR, 'avatars/')
ALLOWED_AVATAR_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
# Instance folder path
INSTANCE_FOLDER_PATH = os.path.join('/home/karim/OpenXC/Dashboard/Flask', 'instance')
# Cache configuration
CACHE_TYPE = 'null'
CACHE_DEFAULT_TIMEOUT = 60
TEMPLATES_AUTO_RELOAD = True
# ToolbarExtention Configuration
DEBUG_TB_ENABLED = False
DEBUG_TB_INTERCEPT_REDIRECTS = False
DEBUG_TB_TEMPLATE_EDITOR_ENABLED = True
DEBUG_TB_PROFILER_ENABLED = True
About cache i am using the cache extension by it's disabled. Please check the config file.
Thanks,
I managed to fix my issue by adding my template folder to extra_files parameter of Flask app
Here is how :
extra_dirs = [
'/home/karim/flak_app/templates',
]
extra_files = extra_dirs[:]
for extra_dir in extra_dirs:
for dirname, dirs, files in os.walk(extra_dir):
for filename in files:
filename = os.path.join(dirname, filename)
if os.path.isfile(filename):
extra_files.append(filename)
app.run(threaded=True, extra_files=extra_files)
Hope this will help someone someday :)
I am attempting to run a python script from Xcode in the Build Pre-actions:
#! /usr/bin/env python
import shutil
import os
app_name = 'name'
def skinfunction(root_src_dir, root_dst_dir):
for src_dir, dirs, files in os.walk(root_src_dir):
print(str(files))
dst_dir = src_dir.replace(root_src_dir, root_dst_dir)
if not os.path.exists(dst_dir):
os.mkdir(dst_dir)
for file_ in files:
src_file = os.path.join(src_dir, file_)
dst_file = os.path.join(dst_dir, file_)
if os.path.exists(dst_file):
os.remove(dst_file)
shutil.copy(src_file, dst_dir)
root_src_dir = '${PROJECT_DIR}/skins/'+str(app_name)+'/authorize_bg.imageset'
root_dst_dir = '${PROJECT_DIR}/iDetail/Images.xcassets/authorize_bg.imageset'
skinfunction(root_src_dir,root_dst_dir);
Nearing the end of the script I am trying to get the PROJECT_DIR Xcode environment variable, but it is just not working properly. Either the value is invalid or my formatting is off.
If I hard code the PROJECT_DIR value (the full url to where the project resides) and the script runs successfully.
Am I missing something in trying to get the environment variable.
Ok, I figured it out, instead of trying to get the environment variable directly by using ${PROJECT_DIR} you need to call os.getenv() function. I was able to get the environment variable by calling:
proj_dir = os.getenv('PROJECT_DIR')