I've went through the other answers here but can't grasp how to fix my version of this problem since this is my first project. I can get 127.0.0.1:8000/admin to show up just fine.
I'm getting this error:
TemplateDoesNotExist at /join/home.html.
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.5.1
Exception Type: TemplateDoesNotExist
Exception Value: /join/home.html.
home.html is located in /Users/user/Desktop/mvp_landing/static/templates/join
In my views.py I have this:
from django.shortcuts import render_to_response, RequestContext
from .models import Join
from .forms import JoinForm
def home(request):
form = JoinForm(request.POST or None)
if form.is_valid():
new_join = form.save(commit=False)
new_join.save()
return render_to_response('/join/home.html.', locals(), context_instance=RequestContext(request))
so I should be ok with what I have here in settings.py for TEMPLATE_DIRS, right?:
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(os.path.dirname(__file__)), "static", "templates"),
)
Here is the entire settings.py(with db info, etc removed):
import os
DEBUG = True
TEMPLATE_DEBUG = DEBUG
MEDIA_ROOT = os.path.join(os.path.dirname(os.path.dirname(__file__)), "static", "media")
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(os.path.dirname(__file__)), "static", "static-only")
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(os.path.dirname(os.path.dirname(__file__)), "static", "static"),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
SECRET_KEY = 'xxxxxxxxxxx'
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',
# Uncomment the next line for simple clickjacking protection:
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'mvp_landing.urls'
WSGI_APPLICATION = 'mvp_landing.wsgi.application'
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(os.path.dirname(__file__)), "static", "templates"),
)
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',
'south',
'join',
)
and urls.py is this:
from django.conf.urls import patterns, include, url
from django.conf import settings
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
(r'media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
url(r'^$', 'join.views.home', name='home'),
# 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)),
)
Any help is appreciated, thanks.
You should fix the path here:
return render_to_response('join/home.html', locals(), context_instance=RequestContext(request))
Notice how I removed the / at the beginning and also the trailing .
Hope this helps
Remove the first slash in the view template string.
Related
I've just set up a django project and got an error:
Request Method: GET
Request URL: http://127.0.0.1/hello/
Django Version: 1.6.6
Exception Type: ImproperlyConfigured
Exception Value:
The included urlconf <function hello at 0x7f665a1e0320> doesn't have any patterns in it
Exception Location: /usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py in url_patterns, line 369
Python Executable: /usr/local/bin/uwsgi
Python Version: 2.7.6
urls.py:
from django.conf.urls import patterns, include, url
import testsite.views
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^hello/', include(testsite.views.hello)),
)
views.py:
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello, world!")
settings.py:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# SECURITY WARNING: keep the secret key used in production sexbbiv*#q44x+jdawuchyu_!$wd#f-p(hid2r*zrjvy6a6#bsoxbbiv*#q44x+jdawuchyu_!$wd#f-p(hid2r*zrjvy6a6#bsocret!
SECRET_KEY = '##############&'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'testsite.urls'
WSGI_APPLICATION = 'testsite.wsgi.application'
# Database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
I've googled about setting DEBUG_TOOLBAR_PATCH_SETTINGS = False but it didnt help. What's the problem and how to fix it?
The error is clear: it's not a valid url configuration.
To correct the problem, you can do it like this:
# app/views.py
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello, world!")
Now create a file in that app called urls.py:
# app/urls.py
from django.conf.urls import url
from app import views
urlpatterns = [
url(r'^$', views.hello, name='hello'),
]
And now finally in site/urls.py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^hello/', include('app.urls')),
url(r'^admin/', include(admin.site.urls)),
]
Do not use include() for a view pattern:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^hello/', testsite.views.hello),
)
include() is for "including" url configuration in-place from another module/application.
I'm having 404 problems using Django CMS 3.0.0.beta3, Django 1.6.1 and i18n.
When I go to my site, everything is displayed, but when I try to click in the upper bar into Administration or whatever I get a 404 saying:
http://my_server/es-es/admin/
But instead, this URL is working!!
http://my_server/es/admin/
I don't understand why this es-es string puts itself in the middle. I have tried to change the LANGUAGE_CODE to es under settings.py but still nothing.
urls.py
from django.conf.urls import include, patterns, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
)
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
url(r'', include('django.contrib.staticfiles.urls')),
) + urlpatterns
Part of settings.py
SITE_ID = 1
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'cms', # django CMS itself
'mptt', # utilities for implementing a modified pre-order traversal tree
'menus', # helper for model independent hierarchical website navigation
'south', # intelligent schema and data migrations
'sekizai', # for javascript and css management
'djangocms_admin_style', # for the admin skin. You **must** add 'djangocms_admin_style' in the list before 'django.contrib.admin'.
'django.contrib.messages', # to enable messages framework (see :ref:`Enable messages <enable-messages>`)
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
'cms.middleware.language.LanguageCookieMiddleware',
)
LANGUAGE_CODE = 'es-es'
TIME_ZONE = 'Europe/Madrid'
USE_I18N = True
USE_L10N = True
USE_TZ = True
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.core.context_processors.i18n',
'django.core.context_processors.request',
'django.core.context_processors.media',
'django.core.context_processors.static',
'cms.context_processors.media',
'sekizai.context_processors.sekizai',
)
TEMPLATE_DIRS = (
# The docs say it should be absolute path: PROJECT_PATH is precisely one.
# Life is wonderful!
os.path.join(PROJECT_PATH, "templates"),
)
CMS_TEMPLATES = (
('template_1.html', 'Template One'),
('template_2.html', 'Template Two'),
)
LANGUAGES = [
('en', 'English'),
('es', 'Spanish'),
]
LANGUAGE_CODE should be 'es' and be sure to kill all cookies
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.
I have been wrangling with Django for some weeks now, putting it down, picking it up, and now I am convinced that it's what I want to use. I got a site up and running with django-cms, but have a small challenge with cmsplugin-news.
I can input news items, list latest news items, and all works fine. However, when I click on an individual news item to view the detail, I get a 404 page not found error.
Here is my urls.py and settings.py file, respectively.
URLS.PY
from django.conf.urls.defaults import *
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('cms.urls')),
url(r'^news/', include('cmsplugin_news.urls')),
)
if settings.DEBUG:
urlpatterns = patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
) + urlpatterns
SETTINGS.PY
# -*- coding: utf-8 -*-
import os
gettext = lambda s: s
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
DEBUG = True
TEMPLATE_DEBUG = DEBUG
ADMINS = (
# ('Your Name', 'your_email#domain.com'),
)
MANAGERS = ADMINS
LANGUAGES = [('en', 'en'),('jp','jp')]
DEFAULT_LANGUAGE = 0
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(PROJECT_DIR, 'anadacms.db'),
}
}
# 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.
# On Unix systems, a value of None will cause Django to use the same
# timezone as the operating system.
# If running in a Windows environment this must be set to the same as 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
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')
# 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 = "http://portal.workpapers.pro/media/"
# 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 = '/admin/media/'
# Make this unique, and don't share it with anybody.
SECRET_KEY = 'MYSECRETKEY'
# 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',
)
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',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
'cms.middleware.media.PlaceholderMediaMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.auth',
'django.core.context_processors.i18n',
'django.core.context_processors.request',
'django.core.context_processors.media',
'cms.context_processors.media',
)
CMS_TEMPLATES = (
('example.html', 'Basic Template'),
('template_1.html', 'Template One'),
('template_2.html', 'Template Two'),
)
ROOT_URLCONF = 'urls'
CMS_APPLICATIONS_URLS = (
('cmsplugin_news.urls', 'News'),
)
CMS_NAVIGATION_EXTENDERS = (
('cmsplugin_news.navigation.get_nodes', 'News navigation'),
)
TEMPLATE_DIRS = (
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.admin',
'cms',
'menus',
'mptt',
'appmedia',
'south',
'cms.plugins.text',
'cms.plugins.picture',
'cms.plugins.link',
'cms.plugins.file',
'cms.plugins.snippet',
'cms.plugins.googlemap',
'publisher',
'cms.plugins.teaser',
'cms.plugins.video',
'cms.plugins.twitter',
'cmsplugin_facebook',
'cmsplugin_news',
)
Here is a link to the urls.py file for the cmsplugin-news app:
https://bitbucket.org/MrOxiMoron/cmsplugin-news/src/03ba1b86624b/cmsplugin_news/urls.py
The problem is very simple: The cms.urls should always be last in your urlconf because it eats every request.
Changing your urls.py to this should fix it::
from django.conf.urls.defaults import *
from django.contrib import admin
from django.conf import settings
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^news/', include('cmsplugin_news.urls')),
# MUST BE LAST!!!
url(r'^', include('cms.urls')),
)
if settings.DEBUG:
urlpatterns = patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
) + urlpatterns
I'm using django-nonrel on google app engine.
I've got this problem when visiting http://localhost:8080/album
Could not import myapp.views. Error was: No module named myapp.views
my urls:
urlpatterns = patterns('',
('^_ah/warmup$', 'djangoappengine.views.warmup'),
('^$', 'django.views.generic.simple.direct_to_template', {'template': 'home.html'}),
(r'^album/$', 'myapp.views.view_albums'),
(r'^admin/', include(admin.site.urls)),
)
my views:
def view_albums(request):
return direct_to_template(request, 'album.html', locals())
Part of Settings:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.auth',
'django.contrib.sessions',
# 'django.contrib.sites',
'djangotoolbox',
# djangoappengine should come last, so it can override a few manage.py commands
'djangoappengine',
)
PROJECT_DIR = os.path.dirname(__file__)
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media/')
ADMIN_MEDIA_PREFIX = '/media/admin/'
TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), 'templates'),)
ROOT_URLCONF = 'urls'
I'm not using django's site framework, the app structure is
myapp
-\dbindexer
-\django
-\djangoappengine
-\djangotoolbox
-\media
-\templates
-__init__.py
-app.yaml
-views.py
-urls.py
-settings.py
-models.py
-manage.py
-cron.yaml
-dbindexes.py
...
I think you are using
from myapp.views import * in urlconf
please try
-- in urlconf
from views import *