In my django/python code I have the following lines:
f = open(os.path.join(DIRECTORY, filname), 'w')
f.write(filecontent)
f.close()
when I have DIRECTORY='/tmp/' it all works fine, the file.txt is saved with owner=root and group=www-data. However, when I want to save the file in a subdirectory of my django-project, for example DIRECTORY='/project/subfolder' the file doesn't appear, although I have that subfolder set to the same owner/group.
I suspect that it is a permission problem with nginx or gunicorn. Any suggestions?
I tried to solve my problem by just mounting the /tmp/ directory to the docker container where I use the file afterwards. But I ran into the problem, that files in the /tmp/ directory do not appear in docker, while as when I mount another folder like /project/subfolder/, these files do appear in docker. So either way, half works, but never both.
Did you check what kind of permissions your subfolder has?
I also tried saving some files in my project subdirectory.
For example i had a "log" subfolder in my project folder and i had to change permissions using:
chown -R www-data:www-data /var/www/test/myproject/log
Update:
Because of your relative path, django can't save it. You must specify the absolute path.
Therefore in you settings create a variable:
BASE_DIR = Path(__file__).resolve().parent.parent
This is the absolute path location to you project.
You have to import BASE_DIR to your py file and than you can use it like before:
f = open(os.path.join(BASE_DIR, filname), 'w')
f.write(filecontent)
f.close()
Related
I have deployed a Django web app using uWSGI and Nginx, but the app runs into an error when trying to create a file inside a specified folder.
Inside my views file, I specify a function which writes to a file using "with open". This function writes the file to a folder named 'output' inside the Django project folder. When I run the Django server, all the functionality works well, but, when I run the Nginx server, it gives me an error exactly where the "with open" is.
I have already tried to switch the ownership of the 'output' folder, but it did not work. Also tried to run 'sudo chmod -R www-data:www-data' with my whole project folder as an attribute, but I had no success.
I would please like to have more insight on how to fix this issue. The problem is likely about my permissions, but I have tried everything in my knowledge to fix it but it did not work.
In attachment, I am sending an image of the output of the 'ls -la' command inside my Django project folder.
Also, the guide I have followed can be found here: https://tonyteaches.tech/django-nginx-uwsgi-tutorial/.
# views.py
filename = 'Memorial_LaTeX.tex'
filepath = os.path.join(os.getcwd(), 'output', filename)
memorial = open(filepath, 'w')
It is right at this last line that the exception occurs.
output of the ls -la command inside my Django project folder
I have an Django(10.5) WSGI application running on Ubuntu(16.04). The application works completely except for one piece of functionality.
The functionality is located in the views.py file and it tries to get a list of file names in a directory but is unable too.
apache2 is running under the user www-data and the directory is owned by another user. I have tried to chown and chmod the directory so that it is owned by www-data but this didn't make a difference.
I have also added the following to the sites-available file:
<Directory /home/other_user/backup>
Require all granted
</Directory>
NOTE: The functionality works if I run it in Django's dev server:
python3 ./manage.py runserver 0:80
I am wondering where you are trying to get the list of file names. Because Django project recognize that the directory where the manage.py located is the root directory. So The other directories outside of this root directory can't be recognized.(Due to BASE_DIR option in settings). It's nothing related with apache.
If you are trying to get the list of files in the directory which is inside root directory, the please check the permission.
I use Apache Superset for data exploration. I followed the installation instructions and had no problems using the app.
However, after I installed the community maintained docker image I tried to upload a CSV file for visualization and got the following error:
([Errno 13] Permission denied: '/usr/local/lib/python3.5/site-packages/superset/app')
I use sqlite as DB backend, and mounted the DB volume as suggested.
Other users had the same problem with different setup and configurations. The issues they opened (#4576, #4287) have not been resolved yet.
The problem does not seem to be related to DB access permissions as evident by the different DB backend and configurations the users are using.
Solution
Add the following lines to your superset_config.py file, rebuild and run your docker image:
import os
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
# The file upload folder, when using models with files
UPLOAD_FOLDER = BASE_DIR + '/app/static/uploads/'
# The image upload folder, when using models with images
IMG_UPLOAD_FOLDER = BASE_DIR + '/app/static/uploads/'
You can also change the path to wherever you want to save the uploaded files and images in your docker image.
Cause of the problem:
Superset is trying to upload the CSV file to the path shown in the error message. The path is owned by the root user, and Superset does not have right permission to it.
To fix this, you need to change the path where Superset uploads the CSV files. This can easily be done by setting a couple of configurations as shown above.
This should also solve the problem when uploading a photo to use in a Superset user profile.
This error like said above is related to folder permissions mostly.
You can get this to run by executing with root permissions.
For example in my case I got this error after running superset runserver -d -p8080.
Use the command sudo superset runserver -d -p8080 instead and you will be able to upload your csv files.
Note: The other flags and port number specified can be changed according to your needs.
Also Note: This permission error arises only if you installed superset with root privileges i.e instead of pip install superset you probably used sudo pip install superset
I'm trying to get a fresh installation of Django 1.5 running on an Apache-Server. The WebServer is situated on a shared hosting platform called uberspace.de
which means I have no access to the Apache configuration itself I can however write .htaccess files if that's any help at all. Django is deployed via fast-cgi which is working as expected.
Whats not working however is the access to static files on the server like the .css files and graphics for the Django administration interface.
As mentioned in the official docs I used the following command to copy the static Files into my ~/html/static directory.
manage.py collectstatic
And these are the values from my settings.py:
STATIC_ROOT = '/home/bier/html/static/'
STATIC_URL = '/static/'
All I get is the infamous django 404 page when I try to access any of these Files.
I also followed the 'How to install and deploy Django' guide on my Webhosters Website to the letter. (sorry its only available in german I believe)
I already contacted the webhosters support but they don't know whats wrong.
All the solutions I've come up with so far suggest setting some sort of Alias in the Apache configuration. Which I can not do.
I'm thankful for any ideas you might have.
Try using a full address instead.
STATIC_ROOT = '/home/bier/html/static/'
STATIC_URL = 'http://www.mysite.com/static/'
Edit: Perhaps you could ask your host to setup /static/ in your Apache config:
sudo nano /etc/apache2/sites-enabled/mysite.com and add:
Alias /static/ /home/bier/html/static/
I've had a situation before where I've had to upload /static/ files manually because of a highly restrictive host (permissions). Perhaps you need to download a copy of django to your desktop and then upload the static admin file set into your /static/ directory manually?
Lastly, have you added the static files to your urls?
url(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root': '/home/bier/html/static'}),
I have more simpler solution. you need to create a directory named, let's say 'x' in "public_html" or similar location from which server serves the files by default.
Then upload all static files in directory x. (this can be done by running collectstatic locally and then upload all contents of directory STATIC_ROOT to x)
Then, change your STATIC_URL and STATIC_ROOT as follows:
STATIC_URL = '/x/'
STATIC_ROOT = os.path.join(BASE_DIR, '../public_html/x') # Path to folder
I want to enable my users to upload photos. Because of local development issues (I'm on windows), I've used FileFields for the time being rather than ImageFields. On my local server, everything was working just fine. I've also uploaded images remotely in the past, so I know the drill.
For some reason, it's now failing to write the files to the specified directory. I pulled the directory as a part of my general git pull. Could that be the issue.
I've done all the following
Model:
class Post_Photo(models.Model):
post=models.ForeignKey(Post,blank=True,null=True)
photo=models.FileField(upload_to="post_photos")
Settings file:
MEDIA_ROOT = '/path..../public_html/media/' (tried with and w/o trailing slash)
I've changed both the /media and /media/post_photos permissions so that they are writable by apache (www-data).
chgrp www-data post_photos
chmod g+w post_photos
I guess I can try to make it permissions 777. Has anybody run into this problem?
Apparently your MEDIA_URLpoints to example.com. Change it to the address of your site.
file settings.py, look for:
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = ''