I am new to django. I am using Google App-engine and Google CloudSQL to serve a django1.2 project. I've been following the tutorial here and have hit a snag.
I enabled the admin back end in my settings.py, but when I go to http://www.examplesite.com/admin/ the css files aren't found.
The admin html page loads fine, but the static files give a 404.
Here's my app.yml:
application: myapp
version: 1
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: django
version: "1.2"
builtins:
- django_wsgi: on
I left these as default in settings.py:
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = ''
# 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 = ''
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'
I know the files are located at google_appengine/lib/django_1_2/django/contrib/admin/media/
Do I need to copy those static resources into my project directory somewhere? Or do I have a setting wrong?
In GAE you need to make the static files accessible in your app.yaml
https://developers.google.com/appengine/docs/python/config/appconfig#Static_Directory_Handlers
Related
using Django 3
I followed the Django Doc
https://docs.djangoproject.com/en/3.0/ref/contrib/staticfiles/#manifeststaticfilesstorage
to export my static files with a hash appending.
settings.py production
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
static_root folder (output)
static_root/
staticfiles.json
static_root/css/
project_styles.87c2920e7bc3.css
project_styles.css
everything is collected correctly.
Afterwards i uploaded everything to my apache static server.
And i set off / comment the STATICFILES_STORAGE . That is how i understand the Doc´s? If i leave this setting on in production i get an 500 Error.
settings.py production
# STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
After restarting my Django app in production, my site is still loading project_styles.css but not the hash Version project_styles.87c2920e7bc3.css in my browser. Even if i delete project_styles.css Django will not serve the hash version.
Question
Did i miss some settings in the settings.py in production mode?
In the Doc´s they mention to set STATICFILES_STORAGE = django.contrib.staticfiles.storage.StaticFilesStorage but it shows no difference. And as it is mentioned it´s only for testing.
What i have to do to load the correct static hash version in production? do i have to set something in my templates, so that django will look into the json file for the correct hash version? Or do i have to name the hash file?
Alright, the Problem was that i wanted two different STATIC_ROOT Paths. One for Development and one for Production, because i want all my Development stuff in one Project folder. Because if you collectstatic with the STATIC_ROOT of your apache server, django will export it for instance into c:/var/www/your/server/static while i wanted it to c:/webprojects/myproject_1/static_root_exports and later upload these files on my server separately.
So i set two different Path depending on DEV_STATIC off / on in my django-environ file. Django will set the correct Path.
.env
DEBUG=off
# --- applies media server & sets MEDIA_ROOT & STATIC_ROOT
DEV_STATIC=on
<...>
STATIC_ROOT_DEV=static_root_exports
STATIC_ROOT_PROD=/var/www/myUserName/html/myproject_assets/static
<...>
setting.py
# -- Set for Hash
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
# --- STATIC_ROOT
if DEV_STATIC == True:
STATIC_ROOT = SERVER_DIR.joinpath(env('STATIC_ROOT_DEV'))
else:
STATIC_ROOT = env('STATIC_ROOT_PROD')
I have started to develop a Django project with currently 3 apps (registration, monitor and randomization)
I wand to set parameters for statics files.
I am using bootstrap 4.0 and I have currently only one css files for sticky footer
as this css is for all template of the project, I have created a static folder at the project's root:
-project
- settings.py
- monitor
- randomization
- registration
- static
- css
- styles.css
- images
and in my settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),
)
STATIC_ROOT = 'D:/Users/jl3/DevSpace/intenseTBM_eTool/static/'
I have run the commande collectatic but I have an error:
ERRORS:
?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.
how can I set 'global' static files for my project?
how should I set parameters?
Well the error is pretty clear.
Also, if the STATIC_ROOT is the same as in STATICFILES_DIRS, there is no need to set it in your settings. STATICFILES_DIRS is the list of folders where Django will search for additional static files, in addition to each static folder of each app installed. STATIC_ROOT is the folder where every static files will be stored after a manage.py collectstatic.
I deployed my django app to pythonanywhere.com
and my admin css not working
http://directdirect.pythonanywhere.com/admin/login/?next=/admin/
what do I do?
I use django 2.0
settings.py:
Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'django_direct/main_app/static'),
os.path.join(BASE_DIR, 'django_direct/main_app/static'),
http://directdirect.pythonanywhere.com/static/admin/css/dashboard.css
]
Check your settings.py file.
DEBUG = True # debug true mod working admin css
DEBUG = False # debug false mod not working css
You can fix it using a proxy server or by running Django with the insecure parameter.
Use NGINX Proxy Server: django-with-nginx
Use Insecure Parameter: run-django-with-insecure-argument
I assume you followed similar instructions to this page from help.pythonanywhere.com.
There it says:
Go visit your site, it should be live! But it probably won't be using your CSS stylesheets, JavaScript and other things that are loaded from static files. To get those set up, check out the page configuring static files with Django.
And here is the link to get those static files up and running.
This should point you in the right direction.
Just move the admin folder from static_cdn to static folder
My django application works well except for one application, all the static files are not loaded.
That happens on this url: http://localhost/db_mgmt/add/dg/. The template is loaded, but no css, no js.
When I look at one of the errors, the browser tries to load the page http://localhost/db_mgmt/add/static/jquery.min.js but the link should be: http://localhost/static/jquery.min.js
These files are loaded in base.html and work everywhere else...
Example of inclusion in base.html:
<script type="text/javascript" src="{{ STATIC_URL }}jquery.min.js"></script>
Here is my settings.py if it helps:
# Django settings for europolix project.
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP
import os
#root of the project
PROJECT_ROOT=os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
#root of templates
TEMPLATE_ROOT=os.path.join(PROJECT_ROOT, 'templates')
#root of media files (import / export files)
MEDIA_ROOT=os.path.join(PROJECT_ROOT, 'media')
#root of static files (css, js, jquery...)
STATIC_ROOT=os.path.join(PROJECT_ROOT, 'static')
#WEB_ROOT=url of WSGIScriptAlias given in the apache configuration file (/etc/apache2/apache2.conf in Lubuntu 13.04)
#example: WSGIScriptAlias /europolix /var/www/europolix/europolix/wsgi.py -> WEB_ROOT="/europolix"
#in the apache configuration file, you must update the alias for static files as well
#ex: Alias /europolix/static /var/www/europolix/static -> WEB_ROOT="/europolix"
WEB_ROOT="/europolix"
#local
WEB_ROOT=".."
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.actrence.com/media/", "http://example.com/media/"
MEDIA_URL=WEB_ROOT+'/media/'
# URL prefix for static files.
# Example: "http://media.actrence.com/static/"
STATIC_URL=WEB_ROOT+'/static/'
# Additional locations of static files
STATICFILES_DIRS=(
STATIC_ROOT,
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
TEMPLATE_DIRS=(
TEMPLATE_ROOT
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
If I print those variables in my view I have the following paths:
PROJECT_ROOT /var/www/europolix
STATIC_ROOT /var/www/europolix/static
STATIC_URL ../static/
WEB_ROOT ..
Thanks in advance for your help.
Edit :
The variable WEB_ROOT is relative in local. Could it be the problem?
The variable searches in the parent directory (am I right?). So the applications with only one "child" in the url work (http://localhost/act or http://localhost/export) but not the applications with many "children" (e.g.: the one above, http://localhost/db_mgmt/add/dg/)
urls.py of the export app:
urlpatterns=patterns('export.views',
url(r'^/?$', 'export', name='export'),
)
urls.py of the db_mgmt app that does not work well:
urlpatterns=patterns('db_mgmt.views',
url(r'^add/(?P<field>\w+)/$', 'add', name='add'),
url(r'^form_add.html/(?P<field>\w+)/$', 'form_add', name='form_add'),
)
We would need to know more about the differences between the applications that work and the application that is giving you trouble
As I thought, the problem, one of the problems was about the WEB_ROOT variable.
Here is how I fixed it:
WEB_ROOT="http://127.0.0.1:8000"
Another problem was the path of the template of my application db_mgmt:
url="/db_mgmt/form_add.html/"+field+"/"
I had to remove the first slash to have it work:
url="db_mgmt/form_add.html/"+field+"/"
Solved :).
urlpatterns = patterns('',
# Examples:
url(r'^$', 'core.views.homepage', name='homepage'),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
That's my urls.py
The static file works if I disable the DEBUG, and doesn't work If I turn it back on.
Part of my settings
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static')
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
ADMIN_MEDIA_PREFIX = '/static/admin/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
Really strange to me, can anyone help?
No, STATIC_ROOT is not served by Django ever. In production (debug off), it is expected that your web server will serve this directory directly. In development (debug on), you shouldn't have this directory or anything in it, anyways.
Let me say that again for emphasis. You are never supposed to directly save any assets in STATIC_ROOT. This directory is solely for the output from the collectstatic management command. All assets in your project are supposed to be saved in the static directory of the particular app it belongs to.
Now, of course, you'll often have assets that are not directly related to a single app, but rather your entire project as a whole. For this scenario, you create a separate directory in your project and place all common assets there. You then add this directory to the STATICFILES_DIRS setting.
In development, Django will serve anything in that directory, and in production, the collectstatic management command will pull assets from that directory into STATIC_ROOT.