Django: TemplateDoesNotExist at / - django

I am using Django 3.0.7 and have a project named starling and an app named piggybank. I placed my index.html inside templates/starling inside the piggybank directory
The following urls.py is inside piggybank:
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
path("", views.index, name="index"),
]
The urls.py in starling (project) is:
"""starling URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("", include("piggybank.urls")),
path('admin/', admin.site.urls),
]
My full settings.py:
"""
Django settings for starling project.
Generated by 'django-admin startproject' using Django 3.0.7.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
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/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ')4^iro*2zhq%f9w2du33eu#ja%)&_cqltplq9b2lzu+qf#xz(6'
# 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 = [
'django.middleware.security.SecurityMiddleware',
'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 = 'starling.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 = 'starling.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/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/3.0/howto/static-files/
STATIC_URL = '/static/'
Template-loader post mortem:
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: /home/anna/starling/lib/python3.6/site-packages/django/contrib/admin/templates/starling/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/anna/starling/lib/python3.6/site-packages/django/contrib/auth/templates/starling/index.html (Source does not exist)
And lastly an image of my directories:

To fix the problem, add piggybank to INSTALLED_APPS in your settings.
In your TEMPLATES setting, you have:
'APP_DIRS': True,
This is the default, and means that Django will look at the templates directory for each app in INSTALLED_APPS. In the post mortem you can see:
django.template.loaders.app_directories.Loader: /home/anna/starling/lib/python3.6/site-packages/django/contrib/admin/templates/starling/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /home/anna/starling/lib/python3.6/site-packages/django/contrib/auth/templates/starling/index.html (Source does not exist)
It's not looking in the piggybank/templates directory because you haven't added piggybank to INSTALLED_APPS yet.

You need are missing TEMPLATES DIR
read this django templates
try this
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], # your dir template path

I got this error when I forgot to add the app in installed apps:
INSTALLED_APPS = [
...
'app.apps.AppConfig',
...
]
app is the name of you app, so if your apps name was myApp,
then it would be: 'myApp.apps.MyappConfig',

Related

Directory indexes are not allowed here. How to fix this?

I am using VueJS for fronend and Django for backend. I changed the conf of Vuejs such that when we run npm run build the compiled files goes to static/dist directory of Django. I also added the urls in urls.py as shown
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from django.views.generic import TemplateView
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
path('admin/', admin.site.urls),
path('users/',include('users.urls')),
path('accounts/',include('django.contrib.auth.urls')),
path('',TemplateView.as_view(template_name='index.html')),
]
urlpatterns = urlpatterns + staticfiles_urlpatterns()
when I go to http://127.0.0.1:8000 index .js is returned which contains the VueJS index file. So the above url goes to http://127.0.0.1:8000/static/dist and the server runs normally. But when I reload the page I am getting the below error.
Page not found (404)
Directory indexes are not allowed here.
Request Method: GET
Request URL: http://127.0.0.1:8000/static/
Raised by: django.contrib.staticfiles.views.serve
Any idea on how to resolve this error?
settings.py:
"""
Django settings for e_cell_server project.
Generated by 'django-admin startproject' using Django 3.2.5.
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-xbdu73yg0k)p%5$yx2%xa$bqsske-x&e!pljzdgvgeffr+dh+0'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS=[]
CORS_ORIGIN_ALLOW_ALL = True
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#3rd-party
'corsheaders',
'rest_framework',
# Own Apps
'users',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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',
'corsheaders.middleware.CorsMiddleware',
]
ROOT_URLCONF = 'e_cell_server.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR.joinpath('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 = 'e_cell_server.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': '/etc/mysql/my.cnf',
},
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/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/3.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR.joinpath('static')
]
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

want to protect views function from anonymous user using decorators

i have django website that require authentication and user login in order to use some functions and view templates.
i used the decorator #login_required but nothing is change and anyone still able to view html page and use any function.
what am i missing in my code ??
views.py
from django.contrib.auth.decorators import login_required
# Create your views here.
#login_required(login_url="/login/")
def create(request):
...
return render(request,'blog/create.html')
urls.py
"""ABdatabase URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include
from blog.views import *
from .views import *
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('',home),
path("usr/logMeIn", logMeIn),
path("usr/logMeOut", logMeOut),
path('mainpage',mainpage,name="main"),
path('blog/', include('blog.urls')),
]
blog.urls.py
# from django.contrib import admin
from django.urls import path
from .views import *
# from django.conf.urls.static import static
# from django.conf import settings
urlpatterns = [
path('create/', create),
path('list/', listANDsearch,name="list"),
path('details/<int:pk>/',get_details,name= 'result'),# using <int:id> in order to display the id
path('listdd',homeInputlineBottom),
]
settings.py
"""
Django settings for ABdatabase project.
Generated by 'django-admin startproject' using Django 2.2.2.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
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/2.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '5(%xarj8+c73-jmn*666gsmj1w5ix%vha8-c1vocevb=2#(()e'
# 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',
'blog',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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 = 'ABdatabase.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',
# 'blog.context_processors.home',
],
},
},
]
WSGI_APPLICATION = 'ABdatabase.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'canada'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS= [
os.path.join(BASE_DIR, 'static')
]
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'
even after i used the decorator nothing it change.
try this
def create(request):
user = request.user
if not user.is_authenticated:
return redirect('/') # Redirect whatever you want

How can I open Django app page within the domain name, not like domain/app_page_url?

Python == 3.6.5
Django == 1.8
Recently purchased domain name, http://www.favourite.uz. And placed my Django project to my hosting in cPanel. When I browse domain http://www.favourite.uz, it throws to default page of cPanel. But when try http://www.favourite.uz/news it opens my django app. I wanted to browse this http://www.favourite.uz/news page within the domain name, without /news.
I placed used server's namespaces in Domain registrator's page. Tried to create another simple project from this tutorial,https://www.youtube.com/watch?v=ffqMZ5IcmSY. But it still opens default page of cPanel.
Main favourite/urls.py
# from django.urls import path
from django.conf.urls import include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.views.static import serve
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^news/', include('news.urls')),
url(r'^i18n/', include('django.conf.urls.i18n')),
]
if settings.DEBUG is False:
urlpatterns += [
url(r'^media/(?P<path>.*)$', serve, {
'document_root' : settings.MEDIA_ROOT,}),
]
(news)app url, news/urls.py
from . import views
urlpatterns =[
url(r'^$', views.NewsView.as_view(), name='posts'),
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^index', views.search, name="search"),
url(r'^chaining/', include('smart_selects.urls')),
url(r'^league/(?P<pk>[0-9]+)/$', views.league_detail, name='league_detail'),
]
settings, favourite/settings.py
Django settings for futbik_version_7 project.
Generated by 'django-admin startproject' using Django 2.0.5.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""
import os
from django.utils.translation import ugettext_lazy as _
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = "/media/"
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/
STATIC_URL = '/static/'
STATIC_ROOT = 'news/static/'
STATICFILES_DIRS =(
os.path.join(BASE_DIR,'news/static/images'),
)
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'az=5ur80-1ge#!951w3(bxaqg1zwo1+a#6+*s*dw(sgkywhb3z'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1', 'localhost', 'www.favourite.uz', 'favourite.uz']
# Application definition
INSTALLED_APPS = [
'modeltranslation',
'news.apps.NewsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_userforeignkey',
'simplesearch',
'smart_selects',
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'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',
'django_userforeignkey.middleware.UserForeignKeyMiddleware',
'django.middleware.locale.LocaleMiddleware',
]
LANGUAGES = (
('en', _('Uzbek')),
('ru', _('Русский')),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
# MODELTRANSLATION_DEFAULT_LANGUAGE = 'en'
# MODELTRANSLATION_TRANSLATION_REGISTRY = 'news.translation'
ROOT_URLCONF = 'futbik_version_7.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# os.path.join(BASE_DIR, 'news/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',
'news.context_processors.add_variable_to_context',
],
},
},
]
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.i18n',
)
WSGI_APPLICATION = 'futbik_version_7.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# SOUTH_DATABASE_ADAPTERS = {
# 'default': 'south.db.sqlite3'
# }
# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = 'en'
TIME_ZONE = 'Asia/Tashkent'
USE_I18N = True
USE_L10N = True
USE_TZ = True
USE_DJANGO_JQUERY = False
# MODELTRANSLATION_DEBUG = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
I really appreciate, if someone encountered such situation and helps me to solve this little problem.
P.s. I am beginner in Django!
Try adding a redirect URL to your urls, so that / redirects to /news. In favourite/urls.py, add this:
from django.views.generic import RedirectView
url_patterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^news/', include('news.urls')),
url(r'^i18n/', include('django.conf.urls.i18n')),
url(r'^$', RedirectView.as_view(pattern_name='posts', permanent=True)) # <—- add this line
]
This should work.
I just find it strange that instead of showing a 404 not found, cPanel seems to hijack the missing page. If my method doesn't work, you should contact cPanel support and ask how to enable the default domain to be routed to your app.

Error at TemplateDoesNotExist at /

I am using django 1.10.2 version and I got an error which says TemplateDoesNotExist at /
I don't know why it's happening, I tried fixing it with:
'DIRS':
['C:\Users\rahul\django-tuts\cms\blog\templates\blog\post'],
or
'DIRS': [os.path.join(os.path.dirname(file), '..//',
'templates').replace('\', '/')],
Still encountered the same error.
Following are my files content:
settings.py
Django settings for cms project.
Generated by 'django-admin startproject' using Django 1.10.2.
For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'u2!61zgb-klaf=qh)bh#9brkxt%vy5!!esg5=uvs!ahh%#t$-4'
# 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',
'blog',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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 = 'cms.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
# 'DIRS': ['C:\\Users\\rahul\\django-tuts\\cms\\blog\\templates\\blog\\post'],
# 'DIRS': [os.path.join(os.path.dirname(__file__), '..//', 'templates').replace('\\', '/')],
'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 = 'cms.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.10/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.10/howto/static-files/
STATIC_URL = '/static/'
urls.py
"""cms URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.10/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', include('blog.urls', namespace='blog', app_name='blog')),
]
views.py
from django.shortcuts import render
from .models import Post
# Create your views here.
def list_of_post(request):
post = Post.objects.all()
template = 'blog/post/list_of_post.html'
context = {'post': post}
return render(request, template, context)
I think you should consider your project root:
Should be like this, considering your templates/blog folder:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates/blog'],
'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',
],
},
},
]
Then to use the template you have to specify only the relative path in the templates/blog folder:
from django.shortcuts import render
from .models import Post
# Create your views here.
def list_of_post(request):
post = Post.objects.all()
template = 'post/list_of_post.html'
context = {'post': post}
return render(request, template, context)
It should work.

Django AttributeError 'tuple' object has no attribute 'regex'

I'm using django 1.8 and having trouble with it. I am trying to import tinymce in my project. When I render it caught
AttributeError: tuple' object has no attribute 'regex'
When I remove the url in url.py it is working. Here is my codes.
url.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
# Examples:
# url(r'^$', 'hizlinot.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
(r'^tinymce/', include('tinymce.urls')),
]
settings.py
"""
Django settings for hizlinot 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 = 'b-4jipu5t(+)g(2-7g#s=1rs19dhpj-1-!x1b-*v7s85f-m%&q'
# 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',
'edebiyat',
'tinymce',
)
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 = 'hizlinot.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 = 'hizlinot.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/'
You forgot the 'url'
url(r'^admin/', include(admin.site.urls)),
url(r'^tinymce/', include('tinymce.urls')),
urlpatterns should be a list of url() instances
url returns RegexURLPattern but instead a tuple is found in your list.
https://docs.djangoproject.com/en/1.8/_modules/django/conf/urls/#url
I know it's not absolutely related to the question, but sometimes this error can be a little deeper than directly in a urls.py file.
I got this error and the cause of the problem wasn't in the error stack trace.
I had this problem while browsing the Admin with a custom Admin Class, and the problem was with the method get_urls() of this class, which was returning something like:
def get_urls(self):
from django.conf.urls import patterns
return ['',
(r'^(\d+)/password/$',
self.admin_site.admin_view(self.user_change_password))] + super(CompanyUserAdmin, self).get_urls()
To fix it:
def get_urls(self):
from django.conf.urls import patterns
return [
url(r'^(\d+)/password/$',
self.admin_site.admin_view(self.user_change_password))] + \
super(CompanyUserAdmin, self).get_urls()
Don't forget the import of 'url':
from django.conf.urls import url