friends.
I try to repeat by doing the example of project in book: "Learning Django Web Development" by Jaiswal, Sanjeev.
Running the server i get such exception: TemplateDoesNotExist at /base.html
TemplateDoesNotExist at /
base.html
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.8.3
Exception Type: TemplateDoesNotExist
Exception Value: base.html
Exception Location: C:\Python34\lib\site-packages\django\template\loader.py in get_template, line 46
Python Executable: C:\Python34\python.EXE
Python Version: 3.4.3
Python Path:
['C:\\dj\\mytweets',
'C:\\WINDOWS\\system32\\python34.zip',
'C:\\Python34\\DLLs',
'C:\\Python34\\lib',
'C:\\Python34',
'C:\\Python34\\lib\\site-packages']
Server time: Tue, 14 Jul 2015 14:01:27 +0300
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
C:\Python34\lib\site-packages\django\contrib\admin\templates\base.html (File does not exist)
C:\Python34\lib\site-packages\django\contrib\auth\templates\base.html (File does not exist)
My settings.py file:
import os
SETTINGS_PATH = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_PATH, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)
TEMPLATE_PATH = os.path.join(PROJECT_PATH, "templates")
SECRET_KEY = 'khcr3h6u+ghi+rtb+g_(mvgq!mtn9u4&%=hu20vt2*u(p8-kde'
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'tweets',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'mytweets.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(PROJECT_PATH, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'mytweets.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(PROJECT_PATH, 'db.sqlite3'),
}
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(
os.path.dirname(__file__),
'static',
),
)
TEMPLATE_DIRS = (
TEMPLATE_PATH,
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
I tried to change settings.py in such way too:
changed settings.py:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(BASE_DIR, "templates")
SECRET_KEY = 'khcr3h6u+ghi+rtb+g_(mvgq!mtn9u4&%=hu20vt2*u(p8-kde'
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'tweets',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'mytweets.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'mytweets.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(
os.path.dirname(__file__),
'static',
),
)
My project structure:
views.py:
from django.views.generic import View
from django.shortcuts import render
class Index(View):
def get(self, request):
params = {}
params['name'] = 'Django'
return render(request, 'base.html', params)
urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from tweets.views import Index
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', Index.as_view()),
url(r'^admin/', include(admin.site.urls)),
)
Traceback:
Template Loader Error:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
C:\Python34\lib\site-packages\django\contrib\admin\templates\base.html (File does not exist)
C:\Python34\lib\site-packages\django\contrib\auth\templates\base.html (File does not exist)
Traceback:
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
132.response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python34\lib\site-packages\django\views\generic\base.py" in view
71.return self.dispatch(request, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\views\generic\base.py" in dispatch
89.return handler(request, *args, **kwargs)
File "C:\dj\mytweets\tweets\views.py" in get
9.return render(request, 'base.html', params)
File "C:\Python34\lib\site-packages\django\shortcuts.py" in render
67.template_name, context, request=request, using=using)
File "C:\Python34\lib\site-packages\django\template\loader.py" in render_to_string
98.template = get_template(template_name, using=using)
File "C:\Python34\lib\site-packages\django\template\loader.py" in get_template
46.raise TemplateDoesNotExist(template_name)
Exception Type: TemplateDoesNotExist at /
Exception Value: base.html
Please, give an advice, what should i change to get rendered page?
I'm not familiar with the book you are using, so I can't give you any advice based on that. If the book is for Django 1.7, you will find it easier to use Django 1.7 instead of Django 1.8, at least when you are beginning with Django.
If you want to stick with Django 1.8, here's how to fix the error you are currently seeing:
Your settings.py file has a mixture of old templates settings, like TEMPLATE_DIRS and TEMPLATE_LOADERS (Django <= 1.7), and the new settings under TEMPLATES (Django 1.8+).
First, remove the old settings TEMPLATE_DIRS and TEMPLATE_LOADERS.
Secondly, it looks as if DIRS is incorrect in your TEMPLATES setting.
Define BASE_DIR, which should be included in settings.py by default when you run ./manage.py startproject
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Then change TEMPLATES to
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
...
I had same problem first I tried to solve it with adding path to shortcuts of HTML templates {% extends "base.html" %} to {% extends "foldername/template/base.html" %} it did not work.
After that I add to 'base.html' file in every projects templates folder. It worked for me.
In installed apps add mytweets instead of tweets .
I had exactly that problem, base.html not found in my_project/templates/base.html.
I fixed it moving the directory templates just after the app that calls it
(before buggy)
--my_project
--templates
--app_a
(after fixed)
--my_project
--app_a
--templates
Set your template path in DIR [] of settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [**'C:\\Python27\\Lib\\site-packages\\django\\contrib\\admin\\templates\\admin'**],
'APP_DIRS':True,
'OPTIONS': {
Related
After i uploaded my django project from my local machine to a remote server this error appears
TemplateDoesNotExist at /
registration/login.html
Request Method: GET Request URL: http://ip:8000/ Django Version: 1.8.6 Exception Type: TemplateDoesNotExist Exception Value:
registration/login.html
Exception Location: /home/market/local/lib/python2.7/site-packages/django/template/loader.py in get_template, line 46 Python Executable: /home/market/bin/python Python Version: 2.7.12 Python Path:
['/home/market/src', '/home/market/lib/python2.7', '/home/market/lib/python2.7/plat-x86_64-linux-gnu', '/home/market/lib/python2.7/lib-tk', '/home/market/lib/python2.7/lib-old', '/home/market/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/home/market/local/lib/python2.7/site-packages', '/home/market/lib/python2.7/site-packages']
Server time: Sun, 2 Jul 2017 08:35:49 +0000
and this error appears in django server console
(1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_DEBUG.
TEMPLATE in Settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Settings.py
"""
Django settings for blackmarket project.
Generated by 'django-admin startproject' using Django 1.8.6.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'ijpwl*f+r5ewyo6-3eorp)&natbb7nh5oh13h&wllhb0o#)gxu'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
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.core.context_processors.request",
"django.core.context_processors.static",
)
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'products',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'blackmarket.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'blackmarket.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
)
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
# login redirect
LOGIN_REDIRECT_URL = '/detail'
LOGIN_URL = '/login/'
LOGOUT_URL = '/logout/'
You have one of these situations:
You have deployed the files from templates/registration on your local machine to /home/market/src/templates instead of /home/market/src/templates/registration.
The same problem exists on your local machine
So you need to figure out which one it is and adjust accordingly by either creating the registration directory below templates/ and move the files there or change the code that calls for registration/login.html to use login.html.
im a begginer in DJANGO.. i was creating my first template and trying it to load into my view through get_template(). But it shows an error "TemplateDoesNotExist at /time/". i don't know what im doing wrong. these are my files.
------------settings.py-----------
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'cg#&q^y)&nvn=te*h!)ax#t4#=_t#phjr_4cr)+8xs$s7iwtir3'
# SECURITY WARNING: don't run with debug turned on in production!
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.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'pr1.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'pr1.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
TEMPLATE_DIRS=[
'/home/sidharth/Desktop/projects/project1/pr1/pr1/templates'
]
-----------urls.py-------------
from django.conf.urls import patterns, include, url
from django.contrib import admin
from views import current_date_time
from views import hours_ahead
urlpatterns = patterns('',
(r'^admin/', include(admin.site.urls)),
(r'^time/$',current_date_time),
(r'^time/(\d)/$', hours_ahead ),
)
------------views.py-----------
from django.shortcuts import render
from django.template import Template, Context
from django.template.loader import get_template
# Create your views here.
from django.http import HttpResponse
import datetime
def current_date_time(response):
now=datetime.datetime.now()
t=get_template('time.html')
time=t.render(Context({'time':now}))
return HttpResponse(time)
def hours_ahead(response,offset):
offset=int(offset)
final_time=datetime.datetime.now()+ datetime.timedelta(hours=offset)
final="<html><body>the time after %s hours will be %s</body></html>" %(offset,final_time)
return HttpResponse(final)
---------------time.html------------
<html><body>It is now {{ time }}.</body></html>
As you can see in the official release django docs, in version 1.8 things have changed regarding how templates parameters are configured.
TEMPLATE_DIRS has been deprecated, so you can remove it from settings.py.
Instead, in TEMPLATES, set the DIRS key correctly (it is empty now).
For example:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates'), ], # check the path depending on your prj structure
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
I am attempting to setup translation using Django's built in translation system. I followed Marina Mele's excellent tutorial at:
http://www.marinamele.com/taskbuster-django-tutorial/internationalization-localization-languages-time-zones#inter-settings
I am using Django 1.8-1.
After following the tutorial, I have the following settings:
settings.py
import os
from .confidential import KEY
from django.utils.translation import ugettext_lazy as _
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = KEY
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',
# third party useful apps
'crispy_forms',
# my apps
'plan_b_profile',
'django_countries',
'captcha',
)
CRISPY_TEMPLATE_PACK = 'bootstrap3'
CONFIRMATION_URL = 'http://127.0.0.1:8000/en/confirm/'
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.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'younityb.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.i18n',
],
},
},
]
WSGI_APPLICATION = 'younityb.wsgi.application'
# internationalization settings
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/Montreal'
LANGUAGES = (
('en', _('English')),
('fr-ca', _('French (Canada)')),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/Montreal'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static_in_env","static_root")
STATICFILES_DIRS =(
os.path.join(BASE_DIR,'static_in_pro'),
)
MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(os.path.dirname(BASE_DIR),"static_in_env","media_root")
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls.i18n import i18n_patterns
urlpatterns = [
url(r'^captcha/', include('captcha.urls')),
]
urlpatterns += i18n_patterns(
url(r'^$', 'plan_b_profile.views.home', name='home'),
url(r'^confirm/','plan_b_profile.views.confirm', name='confirm'),
url(r'^admin/', include(admin.site.urls)),
)
The .po and .mo files are being generated and translations are being shown in my admin but I do not see my translations on my home view. In my templates I put the appropriate {% trans %} fields and also put {% load i18n %} at the top of the template. Any way to fix this?
Finally got the solution! Be careful with the names of your folders in your locale folder (folder with translation files). Make sure that for the filename, hyphens are underscored as follows:
French-Canada : fr-ca but the filename is fr_ca
I am getting a TemplateDoesNotExist error on my django project and it is very frustrating because I have tried to make the app over and over again to but no avail. I was trying to see if I have made any noticeable errors but I cannot seem to find anything at all. I am making this project to help me learn django off of a website called OneMonth.
GitHub Project Files Here
settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MAIN_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
SECRET_KEY = 'c#16w5r9r2)^n1%7#n4yp*l#itoq_&nw^%c(0f)(_0k0p2vs#w'
DEBUG = True
ALLOWED_HOSTS = []
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.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'coffeedapp.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'coffeedapp.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
import dj_database_url
DATABASES['default'] = dj_database_url.config()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
ALLOWED_HOSTS = ['*']
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATIC_URL = '/static/'
TEMPLATE_DIRS = (
os.path.join(MAIN_DIR, 'templates'),
)
STATICFILES_DIRS = (
os.path.join(MAIN_DIR, 'static'),
)
urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'coffeedapp.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
(r'', include('core.urls')),
)
core/urls.py
from django.conf.urls import patterns, include, url
import core.views as coreviews
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'coffeedapp.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^$', coreviews.LandingView.as_view()),
)
Found that your DIRS in TEMPLATES setting is blank.
Have you tried to declare your templates folder inside the TEMPLATES instead of using TEMPLATE_DIRS = (
os.path.join(MAIN_DIR, 'templates'),
)?
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(MAIN_DIR, 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.i18n',
'django.contrib.messages.context_processors.messages',
],
},
},
]
I want to attach pictures in my application django 1.8.
When I attach a file, I have a link to it in the admin panel. After I click on the link I get error code 404.
My url: http://127.0.0.1:8000/media/paint/2015/04/15/1.png
My settings
"""
Django settings for website project.
Generated by 'django-admin startproject' using Django 1.8.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '6tmda(mjlwf%kl1*r#=6s0*#ozpvu&89#hlkcj9r2+(euk4kq#'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'suit',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'www',
'tinymce',
'crispy_forms',
'bootstrap_pagination',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'website.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.request",
)
WSGI_APPLICATION = 'website.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'public_assets')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
My model
class Paint(models.Model):
AVAILABLE = "Available"
NOT_AVAILABLE = "Not available"
STATUS_PAINT = (
(AVAILABLE, "Available"),
(NOT_AVAILABLE, "Not available")
)
title = models.CharField(max_length=200)
gallery = models.OneToOneField(Gallery)
paint = models.ImageField(upload_to='paint/%Y/%m/%d')
price = models.CharField(max_length=50, blank=True, null=True)
status = models.CharField(choices=STATUS_PAINT, default=AVAILABLE, max_length=50)
class Meta:
verbose_name = "Picture"
verbose_name_plural = "Images"
def __unicode__(self):
return "{}".format(self.title)
You must configure your development server to serve uploaded files:
For example, if your MEDIA_URL is defined as /media/, you can do this by adding the following snippet to your urls.py:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# ... the rest of your URLconf goes here ...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Please note that this is only suggested for development use. For production, you should configure your web server (Apache, NginX etc) to handle static and media folders.