Django Static Folder Forbidden (403) on Nginx Digital Ocean - django

I've successfully followed this tutorial on digital ocean: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04
on deploying a django app - the app works fine but one thing I noticed is that the app can't render styles in the static folder - it returns a 403. Am I missing something? Is it a server issue or do I need to configure something additionally in Django.
Please note if I run python manage.py runserver 0.0.0.0:8000 the styles render fine.

I ran into a problem where my js modules imported another module and the imported module was not loading with a 403 forbidden message. This was because in my django's settings.py I did not include AWS_DEFAULT_ACL = 'public-read'```` So they were private resources. Once the permssion is set to public-read, the 403 Forbidden``` error goes away. See the docs here

Related

In production Page not Found Error when trying to access Django /admin on deployed Heroku app

Friends -
I have a django app (build with cookiecutter) and I have it deployed on Heroku, following these steps here:
https://cookiecutter-django.readthedocs.io/en/latest/deployment-on-heroku.html
Everything works fine and I can also create instances in the database. Now, when I try to enter the admin-page with /admin I get an 404 Page not found error.
When deploying I had this error: (which I ignored)
remote: Invalid input of type: 'CacheKey'. Convert to a byte, string or number first.
remote: --> Continuing using default collectstatic.
I ignored this error because I everything was working. Now, could that be connected or am I just missing something here?
I assume I after deployment I should be able to login to the django admin section, or am I mistaken? I set my admin url in my .env file, but still, I get this problem. Locally everything runs perfectly....
Any help or hints are very much appreciated!

Swagger in Django rest function based api does not show properly on ubuntu server (production)

I cannot get my swagger to display properly on ubuntu django setup. And I am using function based API just like here https://github.com/m-haziq/django-rest-swagger-docs
This is the outcome which doesnt display swagger properly - ubuntu 16.04 (in AWS)
https://imgur.com/TnTNExa <-- this is the problem, how to fix this ?
But on my development environment mac pc
https://imgur.com/E1Zst0E <--- its good on PC (Mac)
Here is my swagger schema. As you can see I have some logging:
https://gitlab.com/firdausmah/railerdotcom/blob/master/railercomapp/swagger_schema.py
Here are some logging:
2017-11-30 06:06:57,367 DEBUG xxxx home hello
2017-11-30 06:07:25,131 DEBUG get(self, request)
2017-11-30 06:07:25,132 DEBUG Check and load if the function has __doc__
2017-11-30 06:07:25,132 DEBUG swagger try yaml_doc
2017-11-30 06:07:25,134 DEBUG if yaml_doc
My Django/NGINX/Ubuntu setup is based on this:
https://jee-appy.blogspot.my/2017/01/deply-django-with-nginx.html
Feel free to look through my code,
https://gitlab.com/firdausmah/railerdotcom/tree/master
what could be the problem with swagger? On development its working. There is nothing different how I setup development & production. On production its using nginx, gunicorn, supervisor. on PC its running on python manage.py runserver.
This error is because your swagger static files are inaccessible or at unknown location. You can see the error shown in your (http://railer.com/swagger/) console, which is:
To fix error try adding following lines to your settings.py:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
If this doesnt work then add your static files directory to it. For more info about it, look at this answer:
How to use a remote static file server while developing with django manage.py runserver

Heroku Server 500 error on particular page

Just deployed a Django app on Heroku. Everything works except for one page of my site which creates a Server 500 error (even though it works fine on my local development server).
The page raising the error doesn't do anything unusual. It makes some database calls, renders some forms, implements JQuery, etc. Any clue what this could be or how I can debug it?
Also, I thought this might be a data issue since my data in Dev doesn't match my data in production, but I checked and this doesn't seem to be the cause.
enable DEBUG=TRUE in your django settings.py file or type in console heroku logs --app your_app to get heroku server logs.
This was because I didn't include a runtime.txt telling Heroku to use Python 3 instead of 2 which subsequently raised an error in one of my views where I called super() with no args.

Why won't newly installed Django app with NGINX serve static assets properly?

I have a Mac running OS X 10.9.3. I am trying to setup a Django application backed by a PostgreSQL database served by gunicorn, with static assets served by NGINX. I'm an old hand at Django with MySQL running with the developement server (manage.py runserver). But I'm new to setting it up with virtualenv, gunicorn and NGINX. So I'm following the instructions here.
My Django Project is being served successfully at localhost:3026. As a test of the database connectivity, I wanted to take a look at the Django Admin interface. I visited localhost:3026/admin/
I have included a screenshot below.
Why does this admin page look so ugly? It lacks the neccessary graphical interface and css that it is supposed to have? It looks like NGINX is not properly serving up those static assets. How can I troubleshoot and fix this issue?
EDIT:
After I posted this question, I did python manage.py collectstatic. That went and successfully copied all the static files to where they were supposed to (I think?) live in /opt/myenv/static. You can see the output of that command here. I then re-started gunicorn and nginx. I thought that would fix it. But unfortunately it didn't. The issue remains. In my Django settings.py file, I have configured the STATIC variables as follows:
STATIC_ROOT = "/opt/myenv/static/"
STATIC_URL = '/static/'
Try run command,
python manage.py collectstatic
If the commands executes successfuly, the static file would be generated in your project path, and then if you config the right static path, the web page will be correct.

Issue with Heroku, configuration variables and django using S3

I'm having a very strange issue.
I have my django project running in Heroku using S3 to store my static assets.
I wanted to use the Heroku enviroment variables by setting them as follows:
heroku config:add AWS_S3_TOKEN=my_s3_token
heroku config:add AWS_S3_SECRET=my_s3_secret
And using them with python's os module:
import os
token = os.getenv('AWS_S3_TOKEN')
secret = os.getenv('AWS_S3_SECRET')
But heroku keeps throwing me the following error:
NoAuthHandlerFound: No handler was ready to authenticate. 1 handlers were checked. ['HmacAuthV1Handler'] Check your credentials
So, I ended up writing those parameters in my settings.py file and it works fine
Why is this happening?
If I run
heroku config
I can see all my seted variables and if i do
heroku run python manage.py shell
and then
import os
print os.getenv('AWS_S3_TOKEN')
For example, it prints the variable's value.
any clue on this???
Thank you in advance
I'm not familiar with python or Django, but I'm curious.
If you try your authentication with blank strings, do you get the same error?
If so, then I suspect it's about when you're doing this authentication dance. When are you doing it? At compile time, or at runtime? (If at compile time, look at this)
The app's environment variables aren't usually available while Heroku is compiling the slug and setting up the application, they're only available once the app is running. For a Django app, Heroku will run collectstatic as part of slug compilation, which is probably why you're seeing this error.
You can make the environment variables available during compilation by enabling a Heroku Labs feature:
heroku labs:enable user-env-compile
There's more information in this Heroku dev centre article: https://devcenter.heroku.com/articles/labs-user-env-compile