TemplateDoesNotExist at /board/boards - python

I got this error during the python django project
I do not understand why the template does not connect.
Please let me know which part of the error it is and let me know how to fix it.
How can I to do?
Attach an error picture.
enter image description here
settings.py
INSTALLED_APPS = [
--- skip ---
board.apps.BoardConfig',
]
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',
],
},
},
]
These are my project sub-lists
/mysite
/board
/migrations
/templates
/board
board_list.html
board_detail.html
search.html
__init__.py
admin.py
apps.py
forms.py
models.py
tests.py
urls.py
views.py
/mysite
__init__.py
settings.py
urls.py
views.py
wsgi.py
/static
css
js
image
/templates
base.html
main.html
db.sqlite3
manage.py
/mysite/mysite/urls.py
from django.contrib import admin
from django.conf.urls import url, include
from .views import MainHome
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', MainHome.as_view(), name='main'),
url(r'^board/', include('board.urls', namespace='board'),
]
/mysite/board/urls.py
from django.conf.urls import url
from .views import *
from mysite.views import MainHome
app_name = 'board_app'
urlpatterns = [
url(r'^$', MainHome.as_view(), name='main'),
url(r'^search/$', SearchFormView.as_view(), name='search'),
url(r'^boards/$', BoardList.as_view(), name='board_list'),
url(r'^boards/(?P<slug>[-\w]+)/$', BoardDetail.as_view(), name='board_detail'),
]
/mysite/borad/views
from .models import Board
from django.views.generic import ListView
--- skip ---
class BoardList(ListView):
model = Board
template_name = 'board_list.html'
content_object_name = 'boards'
paginate_by = 10
--- skip ---

In /mysite/borad/views, you have:
template_name = 'board_list.html'
Replace it with:
template_name = 'board/board_list.html'
Because board_list.html is inside templates/board.
Edit
In settings.py, you have:
'DIRS': [os.path.join(BASE_DIR, 'templates')],
which means that Django will look for templates in your mysite/templates folder. Remove it like this:
'DIRS': []
so Django will look by default inside templates/ directory for every installed app.

Your template is inside board directory. And your model template name is not referencing to that path.
In your class BoardList add template_name = 'board/board_list.html'

I'd try a systematic approach:
in settings.py, define TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')
Given your directory structure, your app name is board not board_app. So, set INSTALLED_APPS = [..., 'board']. Also, I don't understand in your code why you refered to board.apps.BoardConfig instead of board, maybe am I missing something
TEMPLATES = [{ .... 'DIRS': [TEMPLATE_DIR], ... }] will make things more visible
in urls.py of board app, set app_name=board
in views.py, if you use Django naming conventions for file / directories, you don't need to specify the template name for class based views. Remove template_name = 'board_list.html provided your template is in board/templates/board/board_list.html
Also, reboot the server after performing those changes.
Does this help?

Related

I am getting Template Does Not Exist at / when I'm running my server in Django

When I run server I get Template Does Not Exist at / for my index.html page. Same problem occurs for other templates as well.
I've tried the solutions for previously asked similar questions like checking the template directory and installed apps in settings.py file but nothing seems working.
Also, when I create a new project folder at some other location and copy all the code there, it usually works.
This is my folder tree:
I'm currently learning Django and new at stack overflow. Any help would be appreciated?
Here is my urls.py code for project folder:
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from basic_app import views
urlpatterns = [
url(r'^$',views.index,name='index'),
path('admin/', admin.site.urls),
url(r'basic_app/',include('basic_app.urls')),
]
Here is my urls.py file under basic_app:
from django.conf.urls import url
from basic_app import views
app_name= 'basic_app'
urlpatterns=[
url(r'^register/$',views.register,name='register')
]
Here is my views.py file code:
from django.shortcuts import render
from basic_app.forms import UserForm, UserProfileInfoForm
def index(request):
return render(request,'basic_app/index.html')
def register(request):
registered= False
if request.method=='POST':
user_form= UserForm(data= request.POST)
profile_form= UserProfileInfoForm(data= request.POST)
if user_form.is_valid() and profile_form.is_valid():
user= user_form.save()
user.set_password(user.password)
user.save()
profile= profile_form.save(commit=False)
profile.user=user
if 'profile_pic' in request.FILES:
profile.profile_pic= request.FILES['profile_pics']
profile.save()
registered= True
else:
print(user_form.errors ,profile_form.errors)
else:
user_form= UserForm()
profile_form= UserProfileInfoForm()
return render(request,'basic_app/registration.html',{'user_form':user_form,'profile_form':profile_form,
'registered':registered})
Here is my settings.py file code for templates:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR=os.path.join(BASE_DIR,'templates')
STATIC_DIR=os.path.join(BASE_DIR,'static')
MEDIA_DIR=os.path.join(BASE_DIR,'media')
This is my Installed app list:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'basic_app',
]
Also this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR,],
'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 think in admin just do this
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',
],
},
},
]
and your view should be something like this
return render(request,'basic_app/registration.html',{'user_form':user_form,'profile_form':profile_form,
'registered':registered})
hope this helps
In your settings.py check the directory pointed by TEMPLATE_DIR so that you can configure where exactly it is ponting to.
print(TEMPLATE_DIR)
Then in views.py:
def index(request):
return render(request,'folder/my_html_file.html')
So django renders the template from.
TEMPLATE_DIR + "folder/my_html_file.html"
If you are using the default configuration settings, the templates are supposed to be in learning_users/basic_app/templates/basic_app/ and accessed like the following:
return render(request, `basic_app/index.html`, {})
where index.html is your template name.
The directory structure here may seem a little redundant at first but you can also change it using the settings.py configuration file.
In your directory, the templates folder is placed directly inside the project folder (i.e. learning_users). Unless you have modified the configuration files to change the templates' path, this would result in an Error.
but perhaps you can do this sure it works in your settings.py file add this line to the installed apps
'basic_app.apps.Basic_appConfig'

Why console is showing templates not defined error in django

I am new to django , I am facing one while django running server , I have copy and paste my code please tell me what is wrong with my code
'DIRS': [templates],
NameError: name 'templates' is not defined
in settings.py file , I have put templates in [] brackets
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',
],
},
},
]
this is my views.py
from django.http import HttpResponse
from django.shortcuts import render
def index(response):
return render(request,'index.html')
this is urls.py
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('about', views.about, name='about'),
]
templates should be string. so ['templates'] instead of [templates]
You need to change DIRS here
'DIRS': [os.path.join(BASE_DIR, 'templates')],
Here, Django expects it has a template path but you provide templates variable which does not have any path.
While others have already given right answers on how to fix it, but I would like to point out a minor thing that you are getting the NameError because you haven't defined the name templates.
The NameError is raised when the variable you are using is not defined anywhere in the program (if you defined it outside the current scope you will be getting UnboundLocalError).
If you define the name templates as a string for the absolute path to your template folder, this will work. Still, never use absolute path in your Django application cause it will become a headache while deployment.
You have to change your line to this
'DIRS': [os.path.join(BASE_DIR, 'templates')],
In the begging of the settings files you can see the BASE_DIR
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Changing DIRS to
'DIRS': [os.path.join(BASE_DIR, 'templates')],
Will help the app to find the templates folder.
Just a personal advice in each app store the templates/appname folder for the specific app. It will help you to have a more robust Django app.
for example you have the polls app, inside the polls app it will be like this
pollsApp/templates/pollsApp/index.html etc.
Also here is the official guide, it might help you

TemplateDoesNotExist at /accounts/login/ error

I got an error:
TemplateDoesNotExist at /accounts/login/ registration/login.html.
I think I should create login.html file but probably it is not required in Django for default beahivor.
After I placed login.html in accounts/templates/accounts the error has not disappeared.What should I do next?
I wrote in urls.py of accounts,
from django.conf.urls import url
from django.contrib.auth.views import login, logout
urlpatterns = [
url(r'^login/$', login,
name='login'),
url(r'^logout/$', logout, name='logout')
]
in urls.py of parent app,
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^accounts/', include('accounts.urls')),
url(r'^api/', include('UserToken.urls')),
]
in TEMPLATES of settings.py of parent app
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',
],
},
},
]
I found in blowser,
emplate-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/admin/templates/registration/login.html (Source does not exist)
django.template.loaders.app_directories.Loader: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/auth/templates/registration/login.html (Source does not exist)
I think maybe I should make templates folder.
Now,I made a directory like accounts/registration/accounts/login.html .
I cannot understand why error shows 2 ways directory to admin&auth.Should I make admin&auth directory?
For me worked adding:
'DIRS': [os.path.join(BASE_DIR, 'templates')],
Take care to delete browser cache.
The pathnames in your error messages are showing a space character just after login/ - this is probably not contained in the actual filenames, thus the files can't be found. In one case it's a normal space, in the other it's a Unicode 'IDEOGRAPHIC SPACE' (U+3000). I don't see where this is coming from in your source code.
check all your folders name what you put same Error I also receiving.
I mistakenly put template instead of templates.
Django default takes templates folder rather than just template or any other name.
This resolves my problem.

Django 1.10 404.html template not found

As far as I have seen in the Django documentation, to show a custom 404 page all you have to do is put a 404.html in the root templates directory.
So my project structure is:
django_project
|_ config
| |_ settings.py
| |_ urls.py
|
|_ templates
|_ base.html
|_ index.html
|_ 404.html
In settings.py I have the following settings for the templates:
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',
'contact.views.contact_info',
],
},
},
]
for the 'DIRS' parameter I also used os.path.join(BASE_DIR, "templates"). This had the exact same outcome.
I also used
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, "templates"),
)
This lead to a deprecation warning.
In urls.py I also did not really do anything special:
from django.conf.urls import url, include
from django.contrib import admin
from django.views.generic import TemplateView
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name="index.html"), name="index"),
url(r'^admin/', admin.site.urls),
]
Template inheritance is working perfectly in other apps, so the templates directory is found.
In settings: DEBUG=False
When I enter a wrong url I get the Django default 'NOT FOUND' page.
What am I missing here?
you need override 'handler404' variable, add this to urls.py
from django.conf.urls import handler404
handler404 = 'your_app.views.404'
https://docs.djangoproject.com/en/1.10/topics/http/views/#customizing-error-views
I guess you have to specify route for your 404.html template, or use from django shortcuts 'get_object_or_404' to handle error 404.

TemplateDoesNotExist at / blog/index.html

I am trying to create my own project with Django version 1.9.x, but I can't make the index page at / to work.
This is my folder structure:
rxe/
urls.py
wsgi.py
settings.py
blog/
migrations/
static/
blog/
css/
js/
img/
templates/
blog/
index.html
admin.py
models.py
tests.py
urls.py
views.py
manage.py
rxe/urls.py:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'$', include('blog.urls')),
]
blog/urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.IndexView.as_view(), name = 'index'),
]
blog/views.py:
from django.http import HttpResponse, HttpResponseRedirect
from django.views.generic import TemplateView
class IndexView(TemplateView):
template_name = 'blog/index.html'
When accessing localhost:8000/, the error:
TemplateDoesNotExist at /
blog/index.html
And right below it, in Template-loader postmortem:
django.template.loaders.app_directories.Loader: /usr/local/lib/python2.7/dist-packages/Django-1.9.4-py2.7.egg/django/contrib/admin/templates/blog/index.html (Source does not exist)
django.template.loaders.app_directories.Loader: /usr/local/lib/python2.7/dist-packages/Django-1.9.4-py2.7.egg/django/contrib/auth/templates/blog/index.html (Source does not exist)
It works if I return an HttpResponse('Hello'), like in the tutorial, but this same tutorial does not have any view for / path, and I want one there.
Edit
rxe/settings.py, how it was created:
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',
],
},
},
]
You must add the blog app to your project by including it in your INSTALLED_APPS setting:
INSTALLED_APPS = [...,
'blog']
Otherwise the template loader will not process the blog directory since it is not seen as an app.

Categories

Resources