Django version 2.2.6
I have the following folder structure and was expecting Django to find index.html based as it is placed in the default location under templates.
Am I doing something wrong here? Did Django stop looking up the template paths by default?
-app
|
settings.py
urls.py
...
templates
|
base.html
index.html
views.py
from django.shortcuts import render
# Create your views here.
def home_view(request):
return render(request, 'index.html')
urls.py
from django.contrib import admin
from django.urls import path
from .views import home_view
urlpatterns = [
path('', home_view, name='index'),
path('admin/', admin.site.urls),
]
You need to set the path of your templates folder in settings.py.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Leave DIRS blank and try again.
you are importing the home view page wrongly in the urls.py
Related
I am trying to do a project for a blog by django 3.1.2 , but unable to link the templates.I tried fixing it with **'DIRS': [os.path.join(BASE_DIR, 'templates'),]** I cannot find the logic miss. The template does exist and when I review the code, the query is performing correctly. I'm at a loss.
my setting
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',
],
},
},
]
my blog urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('', include('posts.urls')),
path('admin/', admin.site.urls),
]
my posts urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
my posts views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return render(request,'posts/index.html')
If your templates folder is in your application's directory
eg: app_name / templates / app_name /.html, then you do not need to add
'DIRS': [os.path.join(BASE_DIR, 'templates'),]'.
So, if your application name is my_posts, create a folder named templates in your application directory. Then again create a folder named my_posts(same as application name) in templates directory, and then inside it create all your html files. But remember in your views.py when you are rendering, you have to do it as such, 'my_posts/index.html'
You only need to add 'DIRS': [os.path.join(BASE_DIR, 'templates'),]' , if you have you template folder in your main/project directory.
I need an extra pair of eyes, I just cannot see the mistake, below you can see all the necessary files, I think the error comes from there
views.py
from django.shortcuts import render
from django.views.generic import ListView
from .models import Entry
class HomeView(ListView):
model = Entry
template_name='entries/index.html'
urls.py
from .views import HomeView
from django.urls import path
urlpatterns = [
path('home/', HomeView.as_view(), name='blog-home')
]
base urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('entries.urls'))
]
templates settings
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 need to add the templates dirs in settings.py , change this
'DIRS': [],
to this
'DIRS': ['templates'],
in the settings.py file , template settings
Am using Django Authentication Model.
The problem is when i visit /accounts/login, i get TemplateDoesNotExist: registration/login.html error message. Now the problem is i already specified a template for the LoginView.
Here is my accounts/urls.py file
from django.urls import path
from django.contrib.auth import views as auth_views
from . import views
app_name = 'accounts'
urlpatterns = [
path('login/',auth_views.LoginView.as_view(template_name='accounts/login.html'), name='login'),
path('join/', views.SignupView.as_view(), name='join'),
path('logout/', auth_views.LogoutView.as_view(), name='logout'),
]
The login template is located at accounts/templates/accounts/login.html
And here is the TEMPLATES
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',
],
},
},
]
My projects urls.py file has these
path('accounts/', include('django.contrib.auth.urls')),
path('accounts/', include('accounts.urls')),
From your error message, it is expecting the login.html file is to be located within your accounts app templates/registration directory like this: accounts/templates/registration/login.html. That should fix your issue.
Also, add these two lines at the bottom of your settings.py file:
LOGIN_URL = 'login'
LOGOUT_URL = 'logout'
I just create a 'registration/login.html' in accounts
as
{% extends "admin/login.html" %}
Full path : 'accounts/templates/registration/login.html'
I have searched through all the questions here already on TemplateDoesNotExist.
I have also been troubleshooting this for about 12 hours.
There is something I am not understanding. I am new to django.
I am following the following Thinkster tutorial:
Building Web Applications with Django and AngularJS
Here is the error:
Exception Location: C:\Users\Workstation333\AppData\Local\Programs\Python\Python36\lib\site-packages\django\template\loader.py in select_template, line 47
Exception Type: TemplateDoesNotExist
Exception Value:
index.html
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: C:\Users\Workstation333\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\admin\templates\index.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Workstation333\AppData\Local\Programs\Python\Python36\lib\site-packages\django\contrib\auth\templates\index.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\Workstation333\AppData\Local\Programs\Python\Python36\lib\site-packages\rest_framework\templates\index.html (Source does not exist)
This IndexView code I do not understand, and I believe is where the problem lies. The tutorial doesn't go over this code. I think this is what changes the index.html or how to find it. This is inside my views.py and is same project folder where my urls.py is located.
views.py
from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.generic.base import TemplateView
from django.utils.decorators import method_decorator
class IndexView(TemplateView):
template_name = 'index.html'
#method_decorator(ensure_csrf_cookie)
def dispatch(self, *args, **kwargs):
return super(IndexView, self).dispatch(*args, **kwargs)
url.py
from django.contrib import admin
from django.urls import path, re_path, include
from rest_framework_nested import routers
from authentication.views import AccountViewSet
from HawkProject.views import IndexView
router = routers.SimpleRouter()
router.register(r'accounts', AccountViewSet)
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^api/v1/', include(router.urls)),
re_path(r'^.*$', IndexView.as_view(), name='index')
]
partial settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
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',
],
},
},
]
Please help, and Thank you.
Edit:
I added the following to the code, and I got it to not break right away,
but I don't think it is a solution, but a hack that will break, or is not maintainable. I added the index for the admin index inside Dirs like so:
'DIRS':
['C:/Users/Workstation333/AppData/Local/Programs/Python/Python36/Lib/site-
packages/django/contrib/admin/templates/admin'],
I couldn't find the other index files in the directories where it thinks they should have been. *notice the double admin in the directory above, not sure why that is. It also makes the "home" page take me directly to the admin page.
I haven't read through the tutorial you are doing but try doing this:
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',
],
},
},
]
As you can see, I think you are getting this error because your template directory 'DIRS' is blank and as such Django does not know where to search for 'index.html'. The 'templates' name can be changed to any name you want but just name it according to the folder which you keep your templates in within each of your apps.
Django Templates
I am new to Django. Getting this error (TemplateDoesNotExist ) when I refresh the page .
My code looks like this :
Project name : newsHtml
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', include('news.urls')),
]
I have created another directory called news where files looks like this :
views.py
from django.http import HttpResponse
from django.template import loader
def index(request):
template= loader.get_template('/home/index.html')
return HttpResponse(template.render(request))
# def index(request):
# return HttpResponse("<h1>its working<\h1>")
urls.py
from django.conf.urls import url
from. import views
urlpatterns = [
url(r'^$',views.index,name='index'),
]
when i uncomment the HttpResponse i am getting the output .
But not able to find why template is not working .
my directory structure looks like this :
First directory :
newsHtml->settings.py,urls.py,wsgi.py
Second directory:
news->templates/home/index.html,views.py,urls.py
I am not able to figure out what is missing . I am using Django 1.11.5.
For a start, why not use the Django shortcut.
from django.shortcuts import render
Your index view should look like so:
def index(request):
return render(request, "home/index.html")
I believe it's because you put a / before your home/index.html.
Also make sure this is in your settings:
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',
],
},
},
]
Furthermore, ensure your application is inside INSTALLED_APPS.