Can't write to a file with Django in Nginx - django

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

Related

Write permission nginx gunicorn

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()

django-admin.py startproject returns CommandError

I've just installed django and I'm having trouble creating a project.
Running django-admin.py startproject test_project returns:
CommandError: '/usr/local/mysql-5.6.13-osx10.7-x86_64/docs/test_project' already exists
I also looked in the path above and there is no test_project folder.
Anybody come across this one?
EDIT
I restarted the terminal and tried again. The error has disappears but it doesn't seem to be creating the test_project folder on my desktop.
You seem to have found your solution through gersande's comment (update your question if you haven't with the new problem); so I'm just going to formalize it here:
I restarted the terminal and tried again. The error has disappears but it doesn't seem to be creating the test_project folder on my desktop.
I'm not sure why restarting the terminal helped (and it's probably a heisenbug), but I do notice an incongruence in your question: you've (seemingly, as the command isn't outputting any errors; but you could confirm by checking if running echo $? is 0 right after) created you new Django project directory in /usr/local/mysql-5.6.13-osx10.7-x86_64/docs/, but you seem to be looking for it on your Desktop (which is mapped to a different folder, probably ~/Desktop.
Simply navigate to the above path in your file explorer, or run cd <directory> to view it in your terminal.

Target WSGI script not found or unable to stat

Target WSGI script not found or unable to stat: /opt/python/current/app/application.py
I contain my app in a file called application.py, and my application's configuration looks like this:
I also tried uploading the sample app that AWS provides, which only contains 'application.py`, and yet I still get this error.
What could be causing the error?
For me, it was this silly thing. In my mac, I compressed by right-clicking on the folder/repository and compressing it to zip. However, a zip like that extracts to open another folder within it which contains the application. As a result, EBS is unable to locate application.py.
The simple fix hence was to select all the individual files inside the folder to create the zip file for uploading (or using the EB CLI to upload).
I had a similar issue. You should put your application.py in root directory as your WSGIPath suggests, or change your WSGIPath in .elasticbeanstalk/optionsettings.yourappname-env.
For me, I had my app instance stored in a variable called app, which wasn't recognised by Elastic Beanstalk. As soon as I changed the variable to application, it started working.
# In application.py or manage.py, after initialising the app
application = app
should do the trick.
Use application instead of app or any other variable you are using.
application = Flask(__name__)

django mkdir permission in apache

I have a django app containing a model with a file upload field. the upload field takes the targeted file and uploads a copy to either an existing directory in the media root or, if the directory hasn't been created, it creates the directory and drops the file inside of it.
The app works beautifully in dev, utilizing the built-in django server, but when I move it to a production server (my OSX machine running an apache2 instance with mod_wsgi) I get "[Error 13] Permission denied" thrown from the mkdir function in django's storage.py whenever I try to upload a file. I strongly suspect there is permission syntax that needs to be added to my apache httpd.conf. I don't know why else the django server has no trouble with the code but apache gags. Does anyone know?
Permissions issues are described in mod_wsgi documentation at:
http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Access_Rights_Of_Apache_User
I guess sometimes an error message is exactly what it says it is. In this case "[Error 13] Permission denied" was being thrown because apache didn't have write access to the directories the django app was attempting to upload to. I simply navigated to the the directories I set as file upload directories, and gave write permissions on them systemwide. This probably wasn't the most secure solution, but it was the most practical as, it works and I don't know how to explicitly set write permissions for apache2 without just opening the directory systemwide.
Also, I didn't post the question at serverfault because I didn't know whether it was a django config issue or an apache issue.

Django App not working: "Error: No module named app_name"

I'm new to Django, and I can't figure out why my app isn't loading. I've created all the files: models.py, views.py, urs.py with default code in. But when I run python manage.py runserver, it says the following:
Error: No module named app_name
What am I doing wrong?
Did you remember to include __init__.py in your folder?
If you did, make sure the permissions on the file are also correct. There's also a similar question here.
Just an additional hint: Instead of manually creating the files you can use django-admin.py startapp APPNAME to automatically create a directory with all necessary files for a new app.
I got your point the init file thing also dosen't work out for me as well. Just ensure that are you writing the proper command inside the proper directory while creating the app, using the terminal.
Like while writing the command "django-admin startapp APP_NAME", ensure that the command is written inside your root configuration directory (which gets created after you typein command "django-admin startproject PROJECT-NAME"), not anywhere else. Then mention the app name inside the settings.py file, under the INSTALLED_APPS[ ] list. Then finally run the command "python manage.py runserver" in the same root configuration directory. I assure you, this will work for sure and the "No modules found" thing will disappear. Have a try and tell me if it don't. Thank you.