Using iframe in Django, keep getting 'TemplateDoesNotExist' - python

I am using iFrame with Django 2.0.13. I keep getting TemplateDoesNotExist error, and I don't see what I'm missing.
I've looked at other answers here on StackOverFlow and I seem to be doing everything.
settings.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
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',
],
},
},
]
urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.generic import TemplateView
from NatureApp import views
urlpatterns = [
path('', views.index, name="index"),
path('NatureApp/', include('NatureApp.urls')),
path('admin/', admin.site.urls),
path(r'Map2.html', TemplateView.as_view(template_name="Map2.html"), name='Map2'),
]
views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
# def index(request):
# return HttpResponse("Hello World!")
def index(request):
my_dict = {'insert_me':"Hello I am from views.py!"}
return render(request,'NatureApp/index.html',context=my_dict)
Map2.html should being showing. I see index.html fine, but inside the IFrame I see the TemplateDoesNotExist message.
I'm new to Django, I'm trying to include all code needed for troubleshooting.

Try creating Map2 view in views.py. There could be problem with TemplateView.as_view()

Related

Cant render HTML file on Django

Django 4.0.3 and Python 3.10.2
I cant render a html file on my project. What am i missing? Main parts of code below.
Settings.py at INSTALLED_APPS:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
]
Settings.py at TEMPLATES:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'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',
],
},
# 'DIRS': [r'project1\app1\templates\app1'],
'DIRS': [os.path.join(BASE_DIR, 'templates')],
},
]
Project-Urls:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('app1.urls')), # main will be the name of your app
]
App1-Urls:
from django.urls import path
from . import views
urlpatterns = [
path('', views.simple_function, name='simple_function'),
]
Views:
def simple_function(request):
print("Print executes correctly, but the render doesn't")
return render(request, r'project1\app1\templates\app1\home.html')
Html file path: app1/templates/app1/home.html
Github of this project
Had to post this bunch of information to clarify.
instead of
return render(request, r'project1\app1\templates\app1\home.html')
try to write
return render(request, 'app1/home.html')

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'

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.

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.

TemplateDoesNotExist at /board/boards

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?

Categories

Resources