Django is loading template from the wrong app - python

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.

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.

Template file not found Django

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.

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')
]

Template doesnot exist error in Django

Am developing an angular frontend and Django backend app.I don't know where i am going wrong but Django cant seem to locate the template and displays a template doesn't exist message.The project directory looks like this.The backend server is in the "django project" folder
base.py(settings)
import environ
project_root = environ.Path(__file__) - 3
env = environ.Env(DEBUG=(bool, False),)
CURRENT_ENV = 'dev' # 'dev' is the default environment
# read the .env file associated with the settings that're loaded
env.read_env('./mysite/{}.env'.format(CURRENT_ENV))
#Database
DATABASES = {
'default': env.db()
}
SECRET_KEY = env('SECRET_KEY')
DEBUG = env('DEBUG')
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Django Packages
'rest_framework',
'mysite.applications.games',
]
ROOT_URLCONF = 'mysite.urls'
STATIC_URL = '/static/'
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATICFILES_DIRS = [
env('FRONTEND_ROOT')
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [env('FRONTEND_ROOT')],
'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',
],
},
},
]
Environment variables file(dev.env)
DATABASE_URL=sqlite:///mysite.db
DEBUG=True
FRONTEND_ROOT= ('C:/downloads/mysite/frontend/')
SECRET_KEY= '##########################'
urls.py
from django.contrib import admin
from django.conf.urls import include, url
from mysite.applications.api.v1.routes import api_router
from django.views.generic import TemplateView
urlpatterns = [
url(r'^admin/', admin.site.urls),
# Web App Entry
url(r'^$', TemplateView.as_view(template_name="/app/index.html"), name='index'),
]
I changed template DIRS setting to point to 'C:/downloads/mysite/frontend/' and also the template name to point to "/app/index.html".app is a folder inside frontend.

Categories