Very strange behavior of Django template loader [duplicate] - python

I am not able to render any html pages in Django 1.7. My 'index.html' is in 'project/seatalloc/templates/index.html' and my view.py in project/seatalloc/views.py looks like:
def index(request):
return render(request, 'index.html', dirs=('templates',))
project/project/settings.py has templates dirs set:
TEMPLATE_DIRS = (
'/Users/Palak/Desktop/academics/sem3/cs251/lab11/project/seatalloc/templates',
)
urls.py:
urlpatterns = patterns('',
url(r'^seatalloc/', include('seatalloc.urls')),
url(r'^admin/', include(admin.site.urls)),
)
I have tried to follow the documentation strictly, yet can't figure out if Django detects the file, why am I getting TemplateDoesNotExist at /seatalloc/ error. I am new to Django, could someone please help.

If - as in your case - you get a TemplateDoesNotExist error and the debug page states "File exists" next to the template in question this usually (always?) means this template refers to another template that can't be found.
In your case, index.html contains a statement ({% extends %}, {% include %}, ... ) referring to another template Django cannot find. Unfortunately, as of Django 1.8.3, the debug page always names the base template, not the one Django can't find.

Try like this,
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates/'),
)

First of all don't use static path (fixed path) in template dirs in settings.py, use:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
TEMPLATE_DIRS = (
BASE_DIR +'/Templates',
)
And template directory should be in project directory i.e in which manage.py file exists.
And in view.py use render_to_response instead of just render.
return render_to_response("index.html")

Related

TemplateDoesNotExist at /hobbies/

This question is the sequel of my previous question -
Reverse for 'hobbieswithCSS.html' not found. 'hobbieswithCSS.html' is not a valid view function or pattern name (solved)
I have moved from one error (in the title of previous question) to another, (in the title of this question) but hopefully this one will not be that much difficult to solve.
I have this anchor tag on my homepage -
My Hobbies
I have this view in my views.py file -
def hobbieswithCSS(request):
return render(request,'hobbieswithCSS.html')
And these are my urlpatterns -
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
url(r'^basic_app/',include('basic_app.urls')),
url(r'^logout/$',views.user_logout,name='logout'),
url(r'^hobbies/$', views.hobbieswithCSS, name='hobbieswithCSS'),
]
these will solve it for sure.
1.if your saved your templates in a separate templates by app name in templates directory you have to put the name of the sub-folder before the name of the template like this hobbies/hobbieswithCSS.html .
2. set the root of the template directory in your settings.py for project and templates.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'),
)
also check this out Django TemplateDoesNotExist?
Change your views.py like this:
def hobbieswithCSS(request):
return render(request,'basic_app/hobbieswithCSS.html')

Django returns Page Not Found for mapped URL

I'm trying to render a basic html/css page in Django and I can't get it to work. It's set up seemingly the same as my index page which does work correctly and the debug explanation from the 404 response seems to show that it's pointing to the right url. Any thoughts on why this isn't working?
*I'm using django 2.1 so I'm using path instead of the old regex url mapping
*The html file exists and is located in templates\base_app\our-story.html
From views.py:
def OurStory(request):
return render(request, 'base_app/our-story.html')
From urls.py:
from base_app import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index, name='index'),
path('our-story', views.OurStory, name='our-story')
]
From settings.py:
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
INSTALLED_APPS = [
(standard django apps)
'base_app'
]
From debug message:
Request Method: GET
Request URL: http://127.0.0.1:8000/our-story.html
Using the URLconf defined in base_app.urls, Django tried these URL
patterns, in this order:
admin/
[name='index']
our-story [name='our-story']
The current path, our-story.html, didn't match any of these.
URL should be - http://127.0.0.1:8000/our-story
We cannot use our-story.html, As we are using framework and already have a route.
Use like this, You will definitely get rid of this error.
I guess path of html template would be only our-story.html If Your project name is base_app
In views.py you should return:
return render(request, template, context)
where
template = 'our-story.html'
and
context = { _your_context_dictionary }
also it is a good practice to use lower case names in the views:
def our_story

How to organize and access URLS and TEMPLATES in django

i have just begun learning Django, and i stick with the principle that the fastest and best way to learn is through practice. I'm on the process of building my first web app, and i would really appreciate your help on the following :
I'm working on getting the front end to show up. But i'm having a hard time understanding the way the URLS work.
I have the following directory :
/myApp
/myApp
/public
/templates
/account
login.html
base.html
settings.py
urls.py
...
/account
urls.py
views.py
I have this on my myApp(the main app) 'urls.py' file
urlpatterns = patterns('',
...
url(r'^$', include(account.urls), name='account'),
)
And inside my account urls.py file, i already have the ff :
...
urlpatterns = patterns(
'account.views',
url(r'^$', 'login_user', name='login'),
)
I already defined following on the account views.py file :
def login_user(request):
return render(request, 'account/login.html')
So i guess the request should render my login.html file.
But i get the error that,
NameError at /
name 'account' is not defined
Therefore, i've figured that there must be something wrong with my settings.py file, right?
So here it is if it serves some purpose (just the important stuffs) :
...
BASE_DIR = os.path.join(os.path.dirname(__file__), '.')
...
ROOT_URLCONF = 'myApp.urls'
WSGI_APPLICATION = 'myApp.wsgi.application'
TEMPLATE_DIRS = [
os.path.join(BASE_DIR,'templates'),
]
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = "/static/"
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'public'),
]
Right now, i really need to at least get the front end working. I hope the details i gave you gives you an idea of how i am organizing my file right now.
Additional note : I want to just create one template directory for the whole app. And as you can see on the structure, the template folder is inside the main app. How do i configure it inside the settings such that the apps use the main template folder?
You should put the account.urls in quotes. Also remove the $ sign from the regex:
url(r'^', include('account.urls')),
And in the account/urls.py file you should correct the base module name from oauth to the acount (your view is account.views.login_user, not the oauth.views.login_user):
urlpatterns = patterns('account.views',
....
)

I can't figure out how to refer to my template from a Django view

I am new to Django and cannot figure out what the path to my templates is supposed to look like. My project's directory tree looks like this:
blog
blog/blog
blog/blog/__init__.pyc
blog/blog/wsgi.pyc
blog/blog/urls.py
blog/blog/urls.pyc
blog/blog/wsgi.py
blog/blog/__init__.py
blog/blog/settings.py
blog/blog/settings.pyc
blog/home
blog/home/views.py
blog/home/templates
blog/home/templates/home
blog/home/templates/home/main.html
blog/home/__init__.pyc
blog/home/urls.py
blog/home/urls.pyc
blog/home/models.py
blog/home/tests.py
blog/home/__init__.py
blog/home/views.pyc
blog/manage.py
Here is my view (from blog/home/view.py):
from django.shortcuts import render_to_response
def home(request):
return render_to_response("home/main.html", {"name" : "maxwell"})
A redacted copy of my settings.py file can be found here: http://pastebin.com/UMTepK9j
And finally, here is the error I get when I browse to 127.0.0.1:8000:
TemplateDoesNotExist at /home/main.html
Can anyone tell me what the path ought to look like in my call to render_to_response?
The TEMPLATE_DIRS in your settings.py should point to the template folder.
Should be something like this:
TEMPLATE_DIRS = (
'../home/templates'
)
That should work.
Note: There you're using relative paths which is not recommended. You should have something like this in your settings.py:
import os
settings_dir = os.path.dirname(__file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))
...
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, '../home/templates/'),
)
Also, you're calling render_to_response without passing it a RequestContext. With this, your templates will lack of some variables important for your project like user. It's better to call it this way:
return render_to_response(
"home/main.html",
context_instance=RequestContext(
request,
{'name':'maxwell'}
)
)

"admin" Subdirectory Triggering (unintentially) Django Admin Pages

I am having trouble with some custom admin pages triggering the Django admin site instead of displaying my custom pages.
My urls.py follows:
urlpatterns = patterns('',
# ... trimmed ...
# Admin pages
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
# Lobby Visitor Log
url(r'^visitorLog', include('lobbyVisitorLog.urls')),
)
In my lobbyVisitorLog app I have the following directory structure, leading to "admin" pages
lobbyVisitorLog
- templates
- admin
And my lobbyVisitorLog/urls.py follows:
urlpatterns = patterns('visitorLog.views',
url(r'^/$', views.home, name='homeView'),
url(r'^/search', views.search, name='searchView'),
url(r'^/submit', views.submit, name='submitView'),
url(r'^/admin/$', views.adminView, name='adminView'),
url(r'^/admin/import/$', views.adminImportView, name='adminImportView'),
url(r'^/(?P<guest_type>\w+)$', views.logEntry, name='logEntryView'),
)
The views.py for the admin index page looks like this:
def adminView(request):
return render(request, 'admin/index.html', {}, context_instance=RequestContext(request))
When I go to "mysite/visitorLog/admin/" I get the Django admin site with the following message: “You don't have permission to edit anything.”
However, if I change my "admin" directory to "utils" (or whatever else, other then "admin") and update my views.py accordingly, everything appears as expected! This is okay, I can deal with my directory being called "utils" but it will annoy me... just enough.
What is happening that is causing the Django admin page to load instead of my custom pages?
By default django first checks each of the paths you have in TEMPLATE_DIRS for 'admin/index.html'. If it doesn't find it there, it starts searching in the templates directory for each app in the INSTALLED_APPS setting.
If 'django.contrib.admin' is listed first in INSTALLED_APPS, it will use the identically named 'admin/index.html' template from the django.contrib.admin app.
Moving 'django.contrib.admin' to the last position in INSTALLED_APPS should allow it to find 'admin/index.html' in your lobbyVisitorLog app first, but this will break the admin site by cause it to use 'admin/index.html' from your app, lobbyVisitorLog.
A good way to solve this is to always have a sub-directory named after your app within your app's templates directory. For example:
lobbyVisitorLog
- templates
- lobbyVisitorLog
- admin
- index.html
and then update your view's template path:
def adminView(request):
return render(request, 'lobbyVisitorLog/admin/index.html', {}, context_instance=RequestContext(request))
You can find more on how Django loads templates here

Categories