Template file not found Django - python

I am trying to make a home page of a new website via Django.
My app name is 'blog', home page is home.html
I still receive the error template does not exist when I go to http://127.0.0.1:8000/blog/home/
I made sure I added 'blog' to my templates in settings.py and that I added the folder templates in the main directory as well as through blog/templates/blog/home.html
myproject/blog/views.py
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request, 'blog/home.html')
myproject/blog/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('home/', views.home, name='home'),
]
myproject/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
]
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',
],
myproject/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
]
Do you see anything in my code which causes a problem? I receive the message in blog/views.py that "Template file 'blog' not found" on the line
return render(request, 'blog/home.html')

You are doing it wrong. You need to read the Django documentation carefully and try to understand whatever you read and implement the same step by step.
The url you have to hit is
http://127.0.0.1:8000/blog/home/
home.html will be rendered at this url.
You don't put html page name in the url

I have been looking for answers too and I tried everything but this worked for me on my windows machine. Adding 'r' before "templates" in os.path.join(BASE_DIR, 'templates') to look like this os.path.join(BASE_DIR, r'templates') resolved the error issue. Also my templates directory was in my project root along side the parent app and sub-apps.

Related

Django does not find the Urls.py in the app

I've been working with Django for 3 months but I haven't had anything like that until now. my thoughts are by '' at the beginning of the urlspattern to make the home template the main template
main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('inventory.urls')),
app urls.py
from . import views
from django.urls import path
urlpatterns = [
path('', views.home, name='home'),
path('base/', views.BaseTemplateView.as_view(), name='base'),
# Menu Item URLs
path('menu/', views.MenuItemListView.as_view(), name='menu_item_list'),
path('menu/<int:pk>/', views.MenuItemDetailView.as_view(), name='menu_item_detail'),
views.py
def home(request):
return render(request, 'home.html')
class BaseTemplateView(TemplateView):
template_name = 'base.html'
class MenuItemListView(ListView):
model = MenuItem
template_name = 'menu_item_list.html'
context_object_name = 'menu_items'
class MenuItemDetailView(DetailView):
model = MenuItem
template_name = 'menu_item_detail.html'
settings.py
ROOT_URLCONF = 'djangodelights.urls'
TEMPLATE_DIR = os.path.join(BASE_DIR,"templates")
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',
],
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'inventory.apps.InventoryConfig',
I was expecting ,when a start the server e get the home.html template.no matter which path I enter manually, I always get the same message
Using the URLconf defined in djangodelights.urls, Django tried these URL patterns, in this order:
admin/
The current path, home, didn’t match any of these.

How to send html-styled password reset email with django

I created an html-styled version of a password reset email to be set with django 4.0 located at 'registration/html_password_reset_email.html'. From other stackoverflows, I learned I needed to add the html_email_template_name parameter for the html version of the email to be sent. However, even with the below code, it is just the text version of the file that is being sent ('registration/password_reset_email.html'). It is definitely fining the registration/password_reset_email.html file, because edits I make to it are successfully emailed, but I can't get it to send the html version. Hints on what I'm doing wrong?
from django.contrib import admin
from django.urls import include, path
from django.contrib.auth import views as auth_views
from django.views.generic.base import RedirectView
from django.urls import reverse
from . import views
urlpatterns = [
path('', views.homeview, name="homeview"),
path('dashboard/', include('dashboard.urls')),
path('admin/', admin.site.urls),
path('accounts/', include('django.contrib.auth.urls')),
path("register/", views.register_request, name="register"),
path('reset_password/', auth_views.PasswordResetView.as_view(
template_name='registration/password_reset_form.html',
html_email_template_name='registration/html_password_reset_email.html',
email_template_name='registration/password_reset_email.html',
), name="reset_password"), # Submit email form
path('reset_password_sent/', auth_views.PasswordResetDoneView.as_view(), name="password_reset_done"), # Email sent success message
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name="password_reset_confirm"), # Link to password reset form in email
path('reset_password_complete/', auth_views.PasswordResetCompleteView.as_view(), name="password_reset_complete"), # Password successfully changed message
]
This is part of my settings.py file
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent
# Take environment variables from .env file
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))
# Application definition
INSTALLED_APPS = [
'dashboard.apps.DashboardConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'crispy_forms',
'mysite',
'users',
'corsheaders',
]
AUTH_USER_MODEL = 'users.User'
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',
],
},
},
]
BASE_DIR resolves to: /Users/MYNAME/Documents/repos/begolden/mysite/templates
and my template files are at:
/Users/MYNAME/Documents/repos/begolden/mysite/templates/registration/html_password_reset_email.html
I tried setting EMAIL_BACKEND to django.core.mail.backends.console.EmailBackend and the text version in registration/password_reset_email.html (not the html version) prints out to console. I also tried changing the html file to just say "Hello World", in case the original html was malformed. The text registration/password_reset_email.html still prints to console (not the html file location).
I think it isn't actually using the file locations I'm providing, because when I change the email_template_name like below, it doesn't read the new text 'registration/test.html'. It still reads the text at the default location 'registration/password_reset_email.html '. I find this confusing though, because it IS finding my custom text at 'registration/password_reset_email.html', which seems to imply that my folder structure is correct but PasswordResetView just isn't using the argument names I am giving it??
path('password_reset/', auth_views.PasswordResetView.as_view(
html_email_template_name='registration/html_password_reset_email.html',
email_template_name='registration/test.html'
), name="password_result"),
You named your URL reset_password, while the name used by Django is password_reset. You have to use the same name, as it will be called by Django with reverse('reset_password'):
urlpatterns = [
...,
path('accounts/', include('django.contrib.auth.urls')),
...,
path('reset_password/', auth_views.PasswordResetView.as_view(
template_name='registration/password_reset_form.html',
html_email_template_name='registration/html_password_reset_email.html',
email_template_name='registration/password_reset_email.html',
), name="password_reset"), # <-
...,
]
Also, beware to keep this URL pattern after path('accounts/', include('django.contrib.auth.urls')), as the URL name will clash and the last one has the precedence.
When naming URL patterns, choose names that are unlikely to clash with other applications' choice of names. If you call your URL pattern comment and another application does the same thing, the URL that reverse() finds depends on whichever pattern is last in your project's urlpatterns list.

Django is loading template from the wrong app

I have a view under my elearning app named home(), which should load index.html from within the app's directory. Instead it loads an instance of index.html from a different app (symposium/templates/index.html). It should be loading it from (elearning/templates/index.html).
Can someone please explain why this is happening and how to fix it?
# Settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Machina dependencies:
'mptt',
'haystack',
'widget_tweaks',
# Machina apps:
'machina',
'machina.apps.forum',
'machina.apps.forum_conversation',
'machina.apps.forum_conversation.forum_attachments',
'machina.apps.forum_conversation.forum_polls',
'machina.apps.forum_feeds',
'machina.apps.forum_moderation',
'machina.apps.forum_search',
'machina.apps.forum_tracking',
'machina.apps.forum_member',
'machina.apps.forum_permission',
# SCORM apps:
'accounts',
'symposium',
'elearning']
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates'), MACHINA_MAIN_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',
'machina.core.context_processors.metadata',
],
},
},
]
# root urls.py
from django.contrib import admin
from django.urls import path, include
from machina import urls as machina_urls
from accounts.views import CreateUser
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
path('forum/',include(machina_urls)),
path('admin/', admin.site.urls),
path('createuser',CreateUser.as_view()),
path('symposium/', include('symposium.urls')),
path('elearning/', include('elearning.urls')),
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# elearning app urls.py
from django.urls import include, path
from . import views
app_name = 'elearning'
urlpatterns = [
path('', views.home, name='home'),
]
# app: elearning/views.py
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request,'index.html',{})
# index.html location
elearning templates : ./elearning/templates/index.html
Django searches for a templates folder within each app for the relevant template to render. Looks like the first it found was index.html from within the symposium app.
The convention of directory structure for templates is to put your templates within another folder with the name of the app in order to avoid this exact situation with templates of the same name.
Move your index.html file from
/elearning/templates/index.html
to
/elearning/templates/elearning/index.html
and then change your home view function to
def home(request):
return render(request,'elearning/index.html',{})
and similarly do the same for the location of the index.html file in the symposium app.

Trying to set up urls.py to direct to index.html

As the question implies, I am trying to set up my urls.py file to point towards my index.html file.
Here is the structure of my project:
-->mysiteX
---->.idea
---->mysite
------->migrations
__init__
admin.py
apps.py
models.py
test.py
views.py
---->mysiteX
-------->templates
index
---->css
---->fonts
---->js
---->vendors
__init__
settings
urls
wsgi
----->venv
db.sqlite3
manage
This is what my urls.py file looks like
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^index/', admin.site.urls),
]
my views.py file
from __future__ import unicode_literals
def index_file(request):
return render(request, "index.html")
settings.py:
import os, sys
abspath = lambda *p: os.path.abspath(os.path.join(*p))
PROJECT_ROOT = abspath(os.path.dirname(__file__))
sys.path.insert(0, PROJECT_ROOT)
TEMPLATE_DIRS = (
abspath(PROJECT_ROOT, 'templates'),
)
When I run manage.py I get this page
Update after making changes to my urls.py file to this extent:
from django.conf.urls import url
from django.contrib import admin
from gradientboost import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^/$',views.index_file,name='index')
]
This is what I am getting
Second Update
changed my settings.py file according to an answer given
import os, sys
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'mysiteX/templates')
...
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',
],
},
},
]
However still getting this error
You Need To Change Your Urls.py
Because You Are Changing Admin Url Admin Url Is For Admin Login And For Website Control!
and if you add index/ than you need to type in your brower like that domainname.com/index
So if you Want Your index.html show on website home page try my code:
Try This If You browsing this link 127.0.0.1:8000
from django.conf.urls import url
from django.contrib import admin
from mysite import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.index_file, name='YOUR NAME')
]
And if you trying this link 127.0.0.1:8000/index Try This:
from django.conf.urls import url
from django.contrib import admin
from mysite import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/$', views.index_file, name='YOUR NAME')
]
Also Add This In Your Settings.py
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__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,'mysiteX/templates')
And Also Add This In your Settings.py
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',
],
},
},
]
create a html template index.html and your url should be redirected the view.
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^index/', your_view, name = 'index'),
]
You don't reference templates from your urls.py. That is for matching routes up to view functions, and it is the view's job to tell Django to render a template, if appropriate. You already have a simple view that renders index.html, you just need to reference it in your urlconf.
If you want your index.html rendered at the root of your site, then you need to add this line to your urls.py (EDIT: fixed the route):
url(r'^$', index_file)
You will also need from mysite.views import index_file at the top of your urlconf.

I want to load a template from the templates folder but I get an error that says the included URLconf does not appear to have any patterns in it

I am trying to load a template in my templates/pages folder and get the error: django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'pages.urls' from 'D:\\django\\pages\\pages\\urls.py'>' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
I have tried putting the templates folder in both the project and the app directory but still get the same error.
In my settings.py I have:
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',
],
},
},
]
and:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'pages',
]
my urls.py file in the root project folder named pages_project looks like:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
]
and my urls.py in my app folder named pages looks like:
from django.urls import path
from . import views
path('', views.HomePageView.as_view(), name='home')
my views.py looks like:
from django.shortcuts import render
from django.views.generic import TemplateView
class HomePageView(TemplateView):
template_name= 'home.html'
I have a template file named home.html in the path pages/templates/pages/home.html and looks like:
<h1>Homepage</h1>
This doesn't have anything to do with templates.
As the error says, the included URLconf doesn't have any patterns in it. As you can see from the main urls.py, you need to define a list named urlpatterns which contains your patterns. So your pages urls.py should be:
urlpatterns = [
path('', views.HomePageView.as_view(), name='home')
]

Categories