Static files with Django are driving me insane. I'm successfully able to load my template from the directory specified in TEMPLATE_DIRS, but when I try to load my css and js files from the STATICFILES_DIRS it doesn't work.
Below are the relevant parts of my settings.py file:
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/home/jrubins/Documents/crowdcluster/crowdcluster/Design/websiteDesign/website/',
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
)
TEMPLATE_DIRS = (
'/home/jrubins/Documents/crowdcluster/crowdcluster/Design/websiteDesign/website/',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'items'
)
Please let me know if you need more info or if you see something wrong. Thanks in advance.
Looks like I didn't add RequestContext in my render_to_response() in my view. Solved the problem and should have looked closer at the django documentation.
I am having the same problem, but I found this website which has a lot of information that might be able to help
http://www.b-list.org/weblog/2006/jun/14/django-tips-template-context-processors/
Related
I'm trying to use Django Compressor, which seems pretty straight-forward, but for some reason I keep getting an error, specifically:
When using Django Compressor together with staticfiles, please add 'compressor.finders.CompressorFinder' to the STATICFILES_FINDERS setting
My settings.py file includes the following:
DEBUG = True
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
...
'compressor',
)
STATIC_URL = '/static/'
STATICFILES_FINDER = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder'
]
STATICFILES_DIRS = [
os.path.join(BASE_DIR, '_static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
COMPRESS_ENABLED = True
I'm not sure if this is a compatibility issue with Django 1.10 (Django Compressor changelog states it is compatible with 1.10), or if Django Compressor simply is broken, since I've clearly added 'compressor.finders.CompressorFinder' to the STATICFILES_FINDERS setting. I've also tried with DEBUG=False.
Any help is appreciated.
I'm using django-compressor for a project, but when I run the ./manage compress command I got a list of errors like:
Invalid template /home/somepath: 'some_template_tags' is not a valid tag library: ImportError raised loading postman.templatetags.some_template_tags: cannot import name SomeLibrary
I don't have any extra information. Moreover the application is added to INSTALLED_APPS and the template tags runs normally without django-compressor.
Update:
This is the settings.pyfile:
INSTALLED_APPS = (
'django.contrib.staticfiles',
'cms',
'mptt',
'menus',
'south',
'sekizai',
'classytags',
'postman',
# More apps
'compressor',
)
# More lines
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
)
The problem was postman. I replaced the function: get_user_model() with User on the postman/models.py file:
I am trying to configure Linkedin auth on my Django website. I use django-social-auth I follow the steps mentioned in the Docs. I have been stuck on this for quite some time.
My settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'social_auth',
'mytests'
)
AUTHENTICATION_BACKENDS = ( 'social_auth.backends.contrib.linkedin.LinkedinBackend',
'django.contrib.auth.backends.ModelBackend',)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.contrib.messages.context_processors.messages',
'social_auth.context_processors.social_auth_by_name_backends',
'social_auth.context_processors.social_auth_backends',
'social_auth.context_processors.social_auth_by_type_backends',
'social_auth.context_processors.social_auth_login_redirect',
)
SOCIAL_AUTH_ENABLED_BACKENDS = ('linkedin',)
LINKEDIN_CONSUMER_KEY = 'py5pspq52ypesv' #API Key
LINKEDIN_CONSUMER_SECRET = 'OyzsrC5GqIo85z9GsWc' #Secret Key
LOGIN_REDIRECT_URL = 'checkbox'
LOGIN_ERROR_URL = '/login-error/'
SOCIAL_AUTH_COMPLETE_URL_NAME = 'socialauth_complete'
SOCIAL_AUTH_ASSOCIATE_URL_NAME = 'socialauth_associate_complete'
urls.py
url(r'', include('social_auth.urls')),
And in my Template
linkedin
I get Error
Error!
Sorry but some error made you impossible to login.
Please try again Home
These are the settings i used for Linkedin Auth
SOCIAL_AUTH_PIPELINE = (
'social_auth.backends.pipeline.social.social_auth_user',
'social_auth.backends.pipeline.user.get_username',
'social_auth.backends.pipeline.user.create_user',
'social_auth.backends.pipeline.social.associate_user',
'social_auth.backends.pipeline.social.load_extra_data',
'social_auth.backends.pipeline.user.update_user_details',
'social_auth.backends.pipeline.associate.associate_by_email'
)
LOGIN_REDIRECT_URL = '/home/'
LOGIN_ERROR_URL = '/login_error/'
TEMPLATE_CONTEXT_PROCESSORS += (
'social_auth.context_processors.social_auth_by_name_backends',
'social_auth.context_processors.social_auth_backends',
'social_auth.context_processors.social_auth_by_type_backends',
'social_auth.context_processors.social_auth_login_redirect',
)
AUTHENTICATION_BACKENDS = (
'social_auth.backends.contrib.linkedin.LinkedinBackend',
'django.contrib.auth.backends.ModelBackend',
)
The SOCIAL_AUTH_ENABLED_BACKENDS, SOCIAL_AUTH_COMPLETE_URL_NAME SOCIAL_AUTH_ASSOCIATE_URL_NAME are not required
You should try testing on the same domain as application is configured.
Register API KEY for domain like test123.youworkserevr.com and put in you hosts file:
127.0.0.1 test123.youworkserevr.com
Try access for you app via test123.youworkserevr.com (instead of 127.0.0.1 or localhost)
am installing the django-admin-tools module to enhance the default django admin with custom dashboard and menus. I have read the docs like a bazillion times
Set up everything the way docs say, but am still merely seeing the Django Admin the SAME WAY is was before I'd even though of django-admin-tools.
What could be wrong?
Am running Django 1.3 and using OpenSuse 11.3
If it helps, the order of apps in my settings.py is like:
INSTALLED_APPS = (
'admin_tools',
'django.contrib.admin',
'admin_tools.theming',
'admin_tools.menu',
'admin_tools.dashboard',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
#'grappelli', #for a sweet django admin look
# 'django.contrib.admindocs',
'django_extensions',
'fpui', #the footprint front-end ui app
)
More Parts of my settings.py in relation to this problem:
ROOTDIR = os.path.abspath(os.path.dirname(__file__))
ADMIN_TOOLS_MEDIA_URL = ROOTDIR + '/media/admin_tools/'
MEDIA_ROOT = ROOTDIR + '/media/admin_tools/'
ADMIN_TOOLS_THEMING_CSS = 'css/theming.css'
MEDIA_URL = '/media/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
ADMIN_TOOLS_INDEX_DASHBOARD = 'footprint.dashboard.CustomIndexDashboard'
ADMIN_TOOLS_APP_INDEX_DASHBOARD = 'footprint.dashboard.CustomAppIndexDashboard'
ADMIN_TOOLS_MENU = 'footprint.menu.CustomMenu'
STATIC_ROOT = ROOTDIR + '/fpui/static/'
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
Had the same problem and finally managed to make it work.
I think your INSTALLED_APPS are ordered correctly - the admin_tools should be before django.contrib.admin. To be absolutely sure push django.contrib.admin below other admin_tools apps too.
The problem is probably somewhere in the other configurations; mine was that admin_tools couldn't access my templates and was running the regular admin instead.
Try the below:
ROOTDIR = os.path.realpath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(ROOTDIR,'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_ROOT,'media/static')
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
STATICFILES_DIRS = (
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
ROOT_URLCONF = 'your_project_name.urls'
TEMPLATE_DIRS = (
os.path.join( PROJECT_ROOT, 'templates' ),
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.contrib.messages.context_processors.messages',
'django.core.context_processors.request',
)
INSTALLED_APPS = (
'admin_tools',
'admin_tools.theming',
'admin_tools.menu',
'admin_tools.dashboard',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
#'grappelli', #for a sweet django admin look
# 'django.contrib.admindocs',
'django_extensions',
'fpui', #the footprint front-end ui app
)
Note that I used realpath for ROOTDIR so that symbolic links may be resolved if there are any. Replace the your_project_name with your project name in ROOT_URLCONF.
Other parameters such as ADMIN_TOOLS_INDEX_DASHBOARD, ADMIN_TOOLS_APP_INDEX_DASHBOARD and ADMIN_TOOLS_APP_INDEX_DASHBOARD are optional, remove them for now so they won't affect the default configuration.
Of course also make sure your urls.py is configured as in the admin_tools guide.
Good luck
Check out the order of your INSTALLED_APPS; It might help if you place the django.contrib.admin just below the django-admin-tools, the order here is used for choosing the right /templates/admin directory.
A little bit late, but I had the same problem, which turned out to be caused by the following circumstance: before installing admin-tools I had overridden one of the admin templates (base-site.html) by putting a modified version in my_project/templates/admin. So it would always use this template instead of the admin_tools one. After removing the custom template it worked.
I have stopped creating new project with django 1.1 and from that time was only working on already created applications. Since then I guess that static serving somehow changed (project is using Django 1.2.4). I'm struggling for few hours and with no results, so if somebody knows what I'm doing wrong please let me know.
My settings :
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
MEDIA_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/media/'
SECRET_KEY = '(d9bahjuyy_i-)4b(w9gc5a&s&5jemcn7&b^wrbuemah3p-6^#'
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
TEMPLATE_DIRS = (
os.path.join(PROJECT_PATH, 'templates'),
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'django.contrib.admindocs',
'project',
)
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.request",
"django.core.context_processors.media",
"django.core.context_processors.csrf",
"django.core.context_processors.i18n",
)
urls:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes' : True}),
)
And media files are located in ../project_path/media
In templates I get the {{ MEDIA_URL }} path as /static/ but my files are not loaded. Going to http://127.0.0.1:8000/static/ (with or without last slash) shows root page. Firebug shows the html code of page in place of javascript files. I would rather expect 404 error. Where's the problem ?
Swicthed to 1.3 . Problem remains.
Just in case if you are using render_to_response, you have to pass optional 3rd parameter:
context_instance=RequestContext(request)
soe the full code looks like:
return render_to_response('index.html',{'dict':'ionary'},context_instance=RequestContext(request))
{{ STATIC_URL }} works with RequestContext, while default for render_to_response is Context
If you are using Django's 1.3 django.contrib.staticfiles, the app will look for static files under the static sub-folder of all your installed apps. For instance, staticfiles will automatically pick up the following css:
yourproject/
appone/
static/
sample.css
If you are using django.contrib.staticfiles and STATIC_URL = '/static/' in your settings.py, you can easily access the css at:
http://localhost:8000/static/sample.css
For your case, it looks like you have a static folder under your project folder, I will assume the following:
yourproject/
static/
If you want staticfiles to pick the above, remove the following from urls.py:
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes' : True}),
Add the following to your settings.py:
import os
SITE_ROOT = os.path.realpath(os.path.dirname(__file__))
STATICFILES_DIRS = (
os.path.join(SITE_ROOT, 'static'),
)
In your template, you might want to start using STATIC_URL instead of MEDIA_URL unless they are both pointing to the same value.
You can read more about staticfiles in Django 1.3.