GAE and django: static files - django

My static files are not being served. What I have verified:
settings.STATIC_ROOT is properly defined: STATIC_ROOT = os.path.join(BASE_DIR, '..', 'collectstatic/')
settings.STATIC_URL is properly defined: STATIC_URL = '/static/'
I have done python manage.py collectstatic
The static files are indeed collected: my collectstatic/ directory has the right assets
The collectstatic/ directory is tracked in my git repo
I have pushed to the gae repo: git push google (I am not sure if gae is using the gae repo copy for staging, or my local repo)
When pushing to the gae repo, the assets are indeed present
I have redeployed the app: gcloud preview app deploy app.yaml --promote -q
My app.yaml is properly configured, as explained here
Specifically:
handlers:
- url: /static
static_dir: collecstatic
- url: .*
script: myproj.wsgi.application
Still I get 404 for:
https://my-site.appspot.com/static/admin/css/base.css
Why could this be? What else can I verify?
EDIT
On The logging tab of the Google Cloud Platform I see warning messages:
Static file referenced by handler not found:
collecstatic/admin/css/base.css
I would say all my settings are correct though.
How can I verify if my app instance has the right assets in the right places? Is there a way of checking the filesystem structure of a running instance?

Embarassing, but the problem was a spelling error. I had:
handlers:
- url: /static
static_dir: collecstatic
And should be:
handlers:
- url: /static
static_dir: collectstatic
I'll keep this. Maybe helps somebody else.

Related

Receiving error 502 BAD CONNECTION nginx when deploying django app on Google app engine

Ive been trying to my django website but I've been having the same 502 BAD CONNECTION nginx error every time and I'm not sure how to troubleshoot it.
When I run the program using py.manager runserver, it works perfectly fine, but but I only receive an error once I run gcloud app deploy, looking at the logs I haven't received any errors, just one warning stating that Container called exit(1).
I've tried one solution from the Google's website: https://cloud.google.com/endpoints/docs/openapi/troubleshoot-response-errors and I added resources: memory_gb: 4 to my app.yaml file and I still end up with an error.
I am not sure this is of much help my app.yaml currently looks like this:
runtime: python39
handlers:
# This configures Google App Engine to serve the files in the app's static
# directory.
- url: /static
static_dir: static/
# This handler routes all requests not caught above to your main app. It is
# required when static routes are defined, but can be omitted (along with
# the entire handlers section) when there are no static files defined.
- url: /.*
script: auto
resources:
memory_gb: 4
And my gcloudignore looks like this:
# This file specifies files that are *not* uploaded to Google Cloud Platform
# using gcloud. It follows the same syntax as .gitignore, with the addition of
# "#!include" directives (which insert the entries of the given .gitignore-style
# file at that point).
#
# For more information, run:
# $ gcloud topic gcloudignore
#
.gcloudignore
# If you would like to upload your .git directory, .gitignore file or files
# from your .gitignore file, remove the corresponding line
# below:
.git
.gitignore
secret_variables.py
.venv
Pipfile
Pipfile.lock
# Python pycache:
__pycache__/
# Ignored by the build system
/setup.cfg
Thank you in advance for the help
Cloned and tried running the code locally after running pip install -r requirements.txt. This is the error I got:
ModuleNotFoundError: No module named 'django_filters'
This means that this requirement needs to be added before the code is deployed to the GAE.
add django-filter to the requirements.txt file to fix it.

Django Admin Page is looking very Ugly(without CSS I think)

Recently I hosted my django website on EC2 ubuntu Instance with nginx server on AWS. When I open my admin page of this website IT is looking very ugly and there is not css but on local server 127.0.0.0:8000 it works fine.
I also inspect on browser console, It is giving this error:
GEThttp://jassem.in/static/admin/css/dashboard.css
[HTTP/1.1 404 Not Found 51ms]
The resource from “http://jassem.in/static/admin/css/nav_sidebar.css” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
admin
The resource from “http://jassem.in/static/admin/css/base.css” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
admin
The resource from “http://jassem.in/static/admin/js/nav_sidebar.js” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
admin
The resource from “http://jassem.in/static/admin/css/responsive.css” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
admin
The resource from “http://jassem.in/static/admin/css/dashboard.css” was blocked due to MIME type (“text/html”) mismatch (X-Content-Type-Options: nosniff).
admin
How can I get back my previous django admin page. FYI I am a newbie on web technology and Django
if the problem apply on all the pages of your project try collectstatic,
Django provides a command to collect static files from all applications
into a single location. This simplifies the setup for serving static files in production.
Open the shell and run the following command:
python manage.py collectstatic
You will see this output:
165 static files copied to '/yourproject/static'.
Files located under the static/ directory of each application present in the
INSTALLED_APPS setting have been copied to the global /yourproject/static/ project
directory.
then, edit the config/nginx.conf file and add:
location /static/ {alias /home/projects/yourproject/static/;}
location /media/ {alias /home/projects/yourproject/media/;}
after the lines:
location / {
include /etc/nginx/uwsgi_params;
uwsgi_pass yourproject;
}
replace the /home/projects/yourproject/ path with the absolute path
to your project directory
You need to run the collectstatic command:
SSH into your EC2 instance
Run python manage.py collectstatic
Assuming you have set up your static files correctly, it should work after that.
Try running this command
python manage.py collectstatic
if it does not work, try this then
python manage.py collectstatic --no-input
This was my first deployment and I tried many things. Finally, this is what worked for me:
Running python manage.py collectstatic
Finding the nginx.conf file and within the server{} block, adding: location /static/ {alias /home/projects/yourproject/static/;} then runinng sudo service nginx restart from the bash terminal
In my settings.py file adding: STATIC_ROOT = os.path.join(BASE_DIR, 'static') and STATIC_URL = '/static/'
just turn on your DEBUG mode in the setting.py file and it will work just fine.
DEBUG = True

app.yaml is not pointing to the WSGI file properly in Django with Google App Engine

I have the following files structure:
And shown below, is the part of the code of my app.yaml that's pointing to the wsgi of my project:
handlers:
- url: /static
static_dir: static/
- url: .*
script: testproject.wsgi.application
Of course there are other files which I think is not necessary to be shown, such as the other apps. When I ran it in local, everything is working fine. But when I upload it to the cloud, it's not working and is showing a server error. In the error log, it says, "Import Error: No module named testproject"
I have been researching a lot but still no luck. Please help me. Thanks!
You forgot src/:
Change the path to the file:
handlers:
- url: /static
static_dir: static/
- url: .*
script: src.testproject.wsgi.application

Deploying django on app engine 500 server error

I am trying to deploy completed web-app (runs locally) on the app engine. The app has no databases and the web page is static. My primary problem is that I try to deploy the app and I get a server error. I suspect that the issue lies with my app.yaml file but I cannot seem to fix it. Here is my yml file:
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /static
static_dir: personal/static/
- url: /.*
script: mysite.wsgi.application
The directory is organized so that the 'personal' is an installed app and static, templates etc. are inside the personal folder.
In settings file, make DEBUG=False for short time to just check what is error, then return it back to True

How to use static files with django nonrel

I'm trying to use the Django nonrel project for google app engine. I setup the test project as described here. I added a new folder to the project named "static" for my static files. And for the app.yaml file i added the lines;
- url: /static
static_dir: static
I can't reach my static files. Do i have to do additional configuration?
Thx in advance.
As people already pointed out, you should put your static_dir directive before /.* pattern
However, that is not the only thing you should know about.
By putting this directive into app.yaml, you make AppEngine webserver (whether it's development or production server) handle the path /static, and you need all the static files to be inside static directory. This means you will have to run python manage.py collectstatic every time you change anything in your static files (especially if you have/use apps with static files -- like, say, admin or django-tinymce) just to test these changes on local server
So how to avoid that? By default staticfiles provides helpers to serve these files on development server without running collectstatic every time, the problem is the direcotry name conflict described in the previous paragraph: Django can't catch requests to your static files path, as they are handled by appserver. You can resolve it by using different paths on development and production server:
# in settings.py
if DEBUG:
STATIC_URL = '/devstatic/'
else:
STATIC_URL = '/static/'
(djangoappengine sets DEBUG to True on development server). You can leave ADMIN_MEDIA_PREFIX = '/static/admin/', but remember to run collectstatic at least once before using admin
Of course remember to use {{ STATIC_URL }}path/to.css in templates instead of /static/path/to.css
Oh, and I assume that you distinguish the directory for original static files you work on and the directory where static files should be collected. I use this in my settings.py:
STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'sitestatic')
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), 'static'),
)
This means: you put your static fiels into static dir (and into your apps' static dirs), collectstatic collects them into sitestatic dir. Appropriate app.yaml directive is
- url: /static
static_dir: sitestatic
Finally, you can configure app.yaml to ignore static and media directories when uploading your app, since all the static files will be collected into and served from sitestatic. However, you should set this only while uploading (otherwise these files will not be available in debug server)
app.yaml have nothing to do with Django, but it does configures App Engine front-end. The answer depends on whether you want to serve static files with Django or the front-end (which is, well, cheaper and faster).
If you just "added" your - url: /static mapping to the end, move it before the /.* wildcard. As all mappings processed from top to bottom — first matching mapping wins.
Well i just figured it out. Just use static_dir line before the main.py. So the app.yaml should look like this;
application: test
version: 1
runtime: python
api_version: 1
builtins:
- remote_api: on
inbound_services:
- warmup
handlers:
- url: /_ah/queue/deferred
script: djangoappengine/deferred/handler.py
login: admin
- url: /_ah/stats/.*
script: djangoappengine/appstats/ui.py
- url: /media/admin
static_dir: django/contrib/admin/media
expiration: '0'
- url: /static
static_dir: static
- url: /.*
script: djangoappengine/main/main.py