I believe I have all the prerequisites correct to get the admin page to appear, i have double checked with a bunch of other stack overflow posts and cannot figure out my error. Any help is greatly appreciated.
So I have Django setup on my Apache server. I go to http://localhost/ and i get the IT WORKED! Page. However, as SOON as I uncomment the admin lines in settings.py and urls.py, I get the Django 404 Error. I think i am doing something wrong with the Aliases and links to the django admin folder. Here is my relevant code
httpd.conf (wsgi loadmodule is included)
Alias /media/ "c:/xampp/htdocs/django_ngs/media"
Alias /static/ "c:/Python27/Lib/site-packages/django/contrib/admin/static"
<Directory c:/xampp/htdocs/django_ngs>
Order deny,allow
Allow from all
</Directory>
WSGIScriptAlias / "c:/xampp/htdocs/django_ngs/mod.wsgi"
settings.py (relevant parts)
ADMINS = (
# ('user', 'myemail#gmail.com'),
)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'djangodb', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'root',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
urls.py
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
)
Any ideas? Thanks in advance
For anyone also looking for a solution, I found the kink. In urls.py it should have been url(r'/admin/', include(admin.site.urls)) instead of url(r'^admin/', include(admin.site.urls)), (switching the carrot for the forward slash). This was not apparent in all of the other example url.py files I have looked at... Anyway thank you for your attention and attempt to help, i really appreciate your time. Make sure the directory to your admin content is correct!
Related
I'm using Django version 1.10. Project work fine on Debug = True, but when I set it to False is not. Django just can't find static files.
My Django settings looks like:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'master',
'update',
]
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
And the urls.py
from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^master/', include('master.urls')),
url(r'^update/', include('update.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
uwsgi.ini file
[uwsgi]
chdir = %v
virtualenv = %v/py
module = go_conf.wsgi
master = true
http = :8000
vacuum = true
buffer_size = 64k
max-requests = 100
daemonize = %v/log.txt
I aslo used python manage.py collectstatic, and it collected everything but still not working.
I tried to solve this by reading other articles on this site, but nothing really worked for me.
Hope, that someone will at last help.
This is the Django desing. A quote from the docs for Static file development view:
This view will only work if DEBUG is True.
That’s because this view is grossly inefficient and probably insecure. This is only intended for local development, and should never be used in production.
If you are setting DEBUG=False you are probably going to make it production. If so, your static files must be served by a webserver (eg. Nginx, Apache etc).
check the whitenoice library,
it works great for development and production environment
Radically simplified static file serving for Python web apps
I am using python 2.7 and django 1.8 and django-non-rel to make my project run on google app engine for django non-rel.
When i open http://127.0.0.1:8000/ it works fine and takes me to the google app engine sdk page..
But when i go to http://127.0.0.1:8000/admin/ , I get error -
404 Not Found
The resource could not be found.
Please help me to set up the admin page.
My urls.py file is this -
from django.conf.urls import *
from django.contrib import admin
import dbindexer
handler500 = 'djangotoolbox.errorviews.server_error'
# django admin
admin.autodiscover()
# search for dbindexes.py in all INSTALLED_APPS and load them
dbindexer.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'bookncart.views.home', name='home'),
# url(r'^bookncart/', include('bookncart.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url('^_ah/warmup$', 'djangoappengine.views.warmup'),
url(r'^admin/', include(admin.site.urls)),
)
The settings.py file is this -
# Django settings for bookncart project.
# Initialize App Engine and import the default settings (DB backend, etc.).
# If you want to use a different backend you have to remove all occurences
# of "djangoappengine" from this file.
from djangoappengine.settings_base import *
import os
ADMINS = (
# ('Your Name', 'your_email#example.com'),
)
MANAGERS = ADMINS
# Activate django-dbindexer for the default database
DATABASES['native'] = DATABASES['default']
DATABASES['default'] = {'ENGINE': 'dbindexer', 'TARGET': 'native'}
AUTOLOAD_SITECONF = 'indexes'
# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = []
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'America/Chicago'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True
# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = False
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
MEDIA_ROOT = ''
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = ''
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = ''
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'
# 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',
)
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'y*ik#rc7+ifd5&_^hc(yk-ca15ze^%ooltm_s)po^zyjzj$t9b'
# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
# 'django.template.loaders.eggs.Loader',
)
MIDDLEWARE_CLASSES = (
# This loads the index definitions, so it has to come first
'autoload.middleware.AutoloadMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.request',
'django.core.context_processors.media',
)
ROOT_URLCONF = 'bookncart.urls'
TEMPLATE_DIRS = (
# 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.
)
# This test runner captures stdout and associates tracebacks with their
# corresponding output. Helps a lot with print-debugging.
TEST_RUNNER = 'djangotoolbox.test.CapturingTestSuiteRunner'
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
'django.contrib.admindocs',
'djangotoolbox',
'autoload',
'dbindexer',
# djangoappengine should come last, so it can override a few manage.py commands
'djangoappengine',
)
# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
DEBUG = True
settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
)
urls.py
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
)
I can't get tiny-mce to work with django flatpages.
I am using Python 2.7 and django 1.5.2
I am following the instructions at http://django-tinymce.readthedocs.org/en/latest/installation.html
The following diagram shows my directory structure https://lh3.googleusercontent.com/-QtdsQE8WOak/UhZlsKIigRI/AAAAAAAADFw/9j4oLT6v1Nw/w764-h354-no/cms.png
The flatfiles are working but I get none of the rich text editor features.
I also don't get any error message.
Below I show how I have my files.
My settings.py file is as follows:
Django settings for cms project.
import os
PROJECT_DIR = os.path.dirname(__file__)
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_email#example.com'),
)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'cms.db', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': '',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = []
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'Greenwich'
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-gb'
SITE_ID = 1
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True
# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/var/www/example.com/media/"
MEDIA_ROOT = ''
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://example.com/media/", "http://media.example.com/"
MEDIA_URL = ''
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = ''
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'
# 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',
)
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'tbbge^_ak_4h*#sm&0_%%ys)hgf3d*nc&%ac_r0zy)!wi(lo14'
# List of callables that know how to import templates from various sources.
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',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'cms.urls'
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'cms.wsgi.application'
TEMPLATE_DIRS = (
# 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.
os.path.join(PROJECT_DIR, '../templates'),
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.flatpages',
'tinymce',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
TINYMCE_JS_URL = 'http://localhost:8080/tiny_mce/tiny_mce_src.js'
TINYMCE_JS_ROOT = 'http://localhost:8080/tiny_mce'
TINYMCE_DEFAULT_CONFIG = {
'plugins' : "table,spellcheck,paste,searchreplace",
'theme' : "advanced",
'cleanup_on_startup': True,
'custom_undo_redo_levels' : 10,
}
TINYMCE_SPELLCHECKER = True
TINYMCE_COMPRESSOR = True
# A sample logging configuration. The only tangible logging
# performed by this configuration is to send an email to
# the site admins on every HTTP 500 error when DEBUG=False.
# See http://docs.djangoproject.com/en/dev/topics/logging for
# more details on how to customize your logging configuration.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
}
},
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django.request': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': True,
},
}
}
My urls.py file is as follows:
from django.conf.urls import patterns, include, url
from django.contrib.flatpages.models import FlatPage
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'cms.views.home', name='home'),
# url(r'^cms/', include('cms.foo.urls')),
# (r'', include('django.contrib.flatpages.urls')),
(r'tinymce/', include('tinymce.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
My models.py file is as follows:
from django import forms
from django.contrib.flatpages.models import FlatPage
from tinymce.widgets import TinyMCE
class FlatPageForm(forms.ModelForm):
content = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows':30 }))
class Meta:
model = FlatPage
My default.html is as follows:
<html>
<head>
<title>{{ flatpage.title }}</title>
</head>
<body>
<h1>{{ flatpage.title }}</h1>
{{ flatpage.content }}
</body>
</html>
I think you need to register the new form with django admin. Try replacing your models.py with this:
from django.contrib import admin
from django import forms
from django.contrib.flatpages.admin import FlatPageAdmin, FlatpageForm
from django.contrib.flatpages.models import FlatPage
from tinymce.widgets import TinyMCE
class MyFlatpageForm(FlatpageForm):
content = forms.CharField(widget=TinyMCE(attrs={'cols': 80,
'rows':30 }))
class MyFlatPageAdmin(FlatPageAdmin):
form = MyFlatpageForm
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, MyFlatPageAdmin)
There are a couple of other things that might need to be fixed. The models.py won't be discovered if you don't have "cms" in your installed apps. Also, I think the extension you want is "spellchecker" not "spellcheck".
I am building an application in Django 1.4.5 with neo4django (github version). I am currently trying to enable the admin interface as instructed on https://neo4django.readthedocs.org/en/latest/admin.html
With the following settings when i hit /admin I get the error
settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
My settings.py includes the following
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.dummy',
}
}
NEO4J_DATABASES = {
'default' : {
'HOST':'localhost',
'PORT':7474,
'ENDPOINT':'/db/data'
}
}
AUTHENTICATION_BACKENDS = ('neo4django.auth.backends.NodeModelBackend',)
SESSION_ENGINE = ('django.contrib.sessions.backends.file')
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.request',
'django.core.context_processors.i18n',
'django.contrib.messages.context_processors.messages',
'django.core.context_processors.static',
)
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',
)
INSTALLED_APPS = (
'neo4django.auth',
'django.contrib.sessions',
'django.contrib.messages',
'neo4django.admin',
'neo4django.contenttypes',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.auth',
'django.contrib.sites',
'django.contrib.staticfiles',
'my_app',
'users',
)
my urls.py
from django.conf.urls import patterns, include, url
from neo4django import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', 'my_app.views.MainHomePage', name='home'),
url(r'^admin/', include(admin.site.urls)),
)
my models.py
from neo4django.db import models
from neo4django.auth.models import User
class Person(models.NodeModel):
email = models.EmailProperty(required = True, unique = True, indexed = True)
# Neo4J RelationShips
user = models.Relationship(User, rel_type = 'is_user')
def __unicode__(self):
return u'%s' % (self.full_name.strip())
and my admin.py (as simple as it gets)
from django.contrib import admin
from neo4django.auth.models import User
from users.models import Person
class UserAdmin(admin.ModelAdmin):
pass
admin.site.register(User, UserAdmin)
class PersonAdmin(admin.ModelAdmin):
pass
admin.site.register(Person, PersonAdmin)
I have tried to either use a mysql together with neo4j, but I will either get the no such table: django_site error, where I have to first run syncdb, at least for the django_site table to be created or I just keep being redirected back to admin login page after successful log in.
Can someone point me to the right direction or at least tell me if using only neo4j is possible or not?
For the database settings, I typically just use
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': '',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': ''
}
}
I know it's annoying, but as long as you don't run syncdb, no sqlite file is created (and neo4django doesn't require syncdb anyway).
As far as the admin.py goes, I noticed you're importing from django- you need to be importing from neo4django. Try
from neo4django import admin
from users.models import Person
class PersonAdmin(admin.ModelAdmin):
...
admin.site.register(Person, PersonAdmin)
EDIT:
One more tip - you'll probably want to set single=True on the user relationship, since a Person should only have one user. If a User should only have one person, I'd also set related_single=True, related_name='person' so you can access both as objects instead of managers.
There you go:
https://docs.djangoproject.com/en/1.5/intro/tutorial01/#database-setup
You have bad ENGINE for database.
I've been all through the documentation, and it just doesn't make sense to me. I ran collectstatic, I set up /static/ directories in both my app and my project directories, I added STATIC_URL and STATIC_ROOT to my settings.py file (but I have no idea how to know if they're set correctly) and {{ STATIC_URL }} still isn't rendering out to anything. It all seems like a heck of a lot of overkill just to connect html to css.
I think I'm lost in the details; could anyone supply a high-level breakdown of this static files idea? I'm afraid I may have mixed instructions for both production and development setups.
MORE: Here's the relevant bit from my settings.py file:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'django.contrib.staticfiles',
'dashboard.base',
)
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',
)
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'C:/Users/Sean/Desktop/Work Items/dashboard/base/static/',
)
And this is the code I'm trying to use in my template:
<link rel="stylesheet" href="{{ STATIC_URL }}css/960.css" />
OK. I made the changes everybody recommended. Here's my new urls.py:
from django.conf.urls.defaults import *
from base.views import show_project
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^dashboard/', include('dashboard.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# (r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
('^show_project/$', show_project),
)
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True }),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': True }))
urlpatterns += staticfiles_urlpatterns()
Am I missing something? Usually my problems turn out to be something really basic that CS pros take for granted but I miss.
Here's how mine is setup. It sounds like you might be missing the static context processor?
STATIC_ROOT and STATIC_URL
In the settings.py used in development:
STATIC_ROOT = ''
STATIC_URL = '/static/'
And the settings.py used on my production server:
STATIC_URL = '//static.MYDOMAIN.com/'
STATIC_ROOT = '/home/USER/public_html/static.MYDOMAIN.com/'
So, all the static files are located in static/. On the production server, all these files in static/ are collected to /home/USER/public_html/static.MYDOMAIN.com/ where they are served by a different web server (nginx in my case) and not Django. In other words, my django application (running on Apache) never even receives requests for static assets in production.
CONTEXT PROCESSOR
In order for templates to have the STATIC_URL variable available to them, you need to use the django.core.context_processors.static context processor, also defined in settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
# other context processors....
'django.core.context_processors.static',
# other context processors....
)
SERVER STATIC ASSETS IN DEVELOPMENT
Django doesn't get requests for static assets in production, however, in development we just let Django serve our static content. We use staticfiles_urlpatterns in urls.py to tell Django to serve requests for static/*.
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# .... your url patterns are here ...
urlpatterns += staticfiles_urlpatterns()
Have a look at Serving static files in development. You need to define the STATIC_URL and STATICFILES_DIRS to let django.contrib.staticfiles know where to look for files.
The idea behind the static files idea is that you can distribute your development related media file (css/js etc.) on a per-app basis, and allow the static files application to manage and collect all these resources from their various places.
So you tell the static files app where to look for static files (by settings STATICFILES_DIRS), where to copy to them (STATIC_ROOT) and what path to access them (STATIC_URL). When you run collectstatic, it search through the directories and copies all the files it finds into the static root.
The benefit of this is that you can manage your static files on a finer leve:
project/app1/static/css/ # These are css/js for a particular app
project/app2/static/css/
project/app3/static/css/
project/static/css # These might be general css/js for the whole project
static/ # This is where the collectstatic command will copy files to
and after you collectstatic them you will have:
project/app1/static/css/
project/app2/static/css/
project/app3/static/css/
project/static/css
static/app1/css/
static/app2/css/
static/app3/css/
static/css/
When you put your app/site on a production server, you let the webserver (apache, nginx) deal with serving the files by telling it to serve media files at /static/ or /media/ directly, while passing all other requests to the application. When developing though, it's easier to let the development server do this for you.
To do this, you have explicitly tell is server any request for media under /static/ (your STATIC_URL). In your urls.py, put the following (or similar)
from django.conf import settings
...
if settings.DEBUG:
urlpatterns += patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.MEDIA_ROOT, 'show_indexes': True }),
url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT, 'show_indexes': True }))
I have the same problem, and search many answers, but no one give me right answer.
The problem is you don't use RequestContext I think.
You should make a RequestContext as the parameter of Template like
c = RequestContext(request, {
'foo': 'bar',
})
In my views is:
return render_to_response('parts/test2.html', RequestContext(request, locals()))