I am trying to build a login page form. In my urls.py, I have linked the file to the built in Django login view, and pass in the path to a template directory. I have login folder inside the template and login.html file inside the login folder.
(r'^login/$', 'django.contrib.auth.views.login', {
'template_name': 'login/login.html'
}),
In settings.py, I have provided the directory where my templates are being stored.
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__),'templates'),
)
When I run the runserver command, it shows TemplateDoesNotExist
Exception Type: TemplateDoesNotExist
Exception Value:login/login.html
For anyone experiencing the same problem, it turns out the template path was incorrect. I needed to use '../templates', instead of 'templates' in settings.py:
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__),'../templates'),
)
A better approach would be define your Directories in another settings file, to alleviate the need to use ugly relative path in your template dirs:
// _paths.py
SETTINGS_DIR = os.path.dirname(__file__)
// _templates.py
from settings/_paths.py import SETTINGS_DIR
TEMPLATE_DIRS = (
os.path.join(SETTINGS_DIR, 'templates'),
)
Adjust accordingly based on your folder structure.
Related
I know this has been covered already but none of the solutions have worked for me. I have a basic django project and it cannot find the template. Here is my views.py:
now = datetime.datetime.now()
t = get_template('index.html')
html = t.render(Context({'current_date' : now}))
return HttpResponse(html)
This is my TEMPLATE_DIRS in settings.py. The project is specified under INSTALLED_APPS
TEMPLATE_DIRS = (
'/Users/Howie/Desktop/djangoCode/mysite/Movies/templates/',
)
This is the error I get every time
Template-loader postmortem:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
C:\Users\Howie\Desktop\djangoCode\mysite..\templates\index.html (File does not exist)
Using loader django.template.loaders.app_directories.Loader:
C:\Python34\lib\site-packages\django-1.7.2-py3.4.egg\django\contrib\admin\templates\index.html (File does not exist)
C:\Python34\lib\site-packages\django-1.7.2-py3.4.egg\django\contrib\auth\templates\index.html (File does not exist)
C:\Users\Howie\Desktop\djangoCode\mysite\Movies\templates\index.html (File does not exist)
I don't understand why django cant find my index.html file. I've tried moving the templates folder into the app itself(named Movies) and I've also had it in the root project folder but both of those didn't work. Any insight would be helpful.
The app_directories.Loader is enabled by default. This means you can put your template in an app/templates directory and be done. No need for TEMPLATE_DIRS and/or TEMPLATE_LOADERS settings. Just out of the box Django:
project/app/views.py
project/app/templates/index.html
get_template('index.html')
It's even better to use subdirectories:
project/app/views.py
project/app/templates/app/index.html
get_template('app/index.html')
This to avoid a index.html from some other app to take precedence.
If you really want a template folder in the root of your project, than don't use hardcoded paths. Do something like this:
from os.path import abspath, dirname, join, normpath
from sys import path
SITE_ROOT = dirname(dirname(abspath(__file__)))
TEMPLATE_DIRS = (
normpath(join(SITE_ROOT, 'templates')),
)
i had the same problem only to find out it was a typo error, i wrote templates
template/file_name/
as template which is not the Django way. The "s" is necessary
templates/file_name/
PyCharm does not find templates, nor template tags, nor static files in my Django project even though the project itself is set up right and working.
I am using Django 1.6.2 with this layout:
proj
.devtmp
manage.py
proj
settings.py
app1
templatetags
app2
templates
static
and with settings like these:
from os.path import join, dirname, pardir, abspath
PROJECT_ROOT = abspath(join(dirname(__file__), pardir))
DEV_TMP_DIR = join(PROJECT_ROOT, pardir, '.devtmp')
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
TEMPLATE_DIRS = (
join(PROJECT_ROOT, 'templates'),
)
MEDIA_ROOT = join(DEV_TMP_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = join(DEV_TMP_DIR, 'static')
STATIC_URL = '/static/'
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
'proj'
'proj.app1'
'app2'
)
Update:
In the IDE preferences, I have configured paths to the project root, settings.py and manage.py. In addition, I have configured the project interpreter (I am running the runserver from the IDE right now with no problems).
Please try this - it works for me for templates:
set templates directories in Python Template Languages -> Template directories
in Project Structure mark your apps as Source Folders
EDIT :
After a project structure reorganization I had problem with static files again. Setting destination of setting.py file in Django Support -> Settings resolved the issue.
Right click on the templates directory and "Mark Directory As" -> "Template Directory" and select template language as django
I've solved this problem by editing settings for Django framework, as seen on a picture.
You need to set up your project properly.
Django Set Up
In Preferences | Languages & Frameworks | Django, Enable Django Suport, set up the path to your project's root, settings.py file, and manage.py file:
Mark your templates directory as Templates
In File | Settings | Project Structure for Windows/Linux and
PyCharm | Preferences | Project Structure for macOS, choose the directory to be marked as a template root.
Click on Templates on Mark as.
Click on OK to apply the changes you made.
The issue for me was that I was doing this in my settings module:
INSTALLED_APPS = [*INSTALLED_APPS, "debug_toolbar"]
This caused PyCharm to think that debug_toolbar is the only app in the entire project. Declaring INSTALLED_APPS in the standard way solved it.
hi guys my django app urls are not working
below is my project urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('recipe.urls', namespace="recipe")),
)
and here is my app urls.py
from django.conf.urls import patterns,url
urlpatterns = patterns('recipe.views',
url(r'^$', 'index', name='index'),
url(r'^create/recipe/$', 'create_recipe', name='create_recipe'),
url(r'^create/ingredients/(?P<recipe_id>\d+)/$', 'create_ingredients',
name="create_ingredients"),
url(r'^create/steps/(?P<recipe_id>\d+)/$', 'create_steps',
name="create_steps"),
url(r'^view/recipe/(?P<recipe_id>\d+)/$', 'view_recipe',
name="view_recipe"),
)
I am not able to get the index page and other urls except admin is working fine. Please help me
From the question comments it seems you are having issues with Django's templates instead of urlconfig. Here is how Django templates work. In your settings.py you define a variable TEMPLATES_DIRS where you specify a tuple of all directories where Django will look for templates.
Assume you have the following TEMPLATES_DIRS:
TEMPLATES_DIRS = (
'/absolute/path/to/foo',
'/absolute/path/to/bar',
)
Then if you look for a template base.html, Django will look for it in the following places and will use the first location if it finds one:
/absolute/path/to/foo/base.html
/absolute/path/to/bar/base.html
In your case you mentioned that you store your templates in both the Django's project folder as well as the app's folder. In that case you have to make sure that both folders are defined in TEMPLATES_DIRS like:
TEMPLATES_DIRS = (
'/absolute/path/to/project/templates',
'/absolute/path/to/app/templates',
)
Then in your case Django will be able to find both base.html and index.html. Now to make things simpler you can define PROJECT_PATH in your settings.py which will store the absolute path to your project path so that you can easily move your project to a different location. I think this is where your issue might be. In Django >= 1.4, you have the following project structure:
/project <= this should be your PROJECT_PATH
/project <= instead of this
templates/
base.html
settings.py
/recipe
templates/
index.html
models.py
With that in mind try using something like this:
PROJECT_PATH = os.path.abspath(os.path.join(__file__, '..', '..'))
PROJECT_NAME = os.path.basename(PROJECT_PATH)
TEMPLATES_DIRS = (
os.path.join(PROJECT_PATH, PROJECT_NAME, 'templates'),
os.path.join(PROJECT_PATH, 'recipe', 'templates')
)
In the above PROJECT_PATH calculates the absolute path of the project. Imagine that your settings.py is located at /some/path/project/project/settings.py. Then you compute the project path as follows:
>>> # settings.py
>>> print __file__
/some/path/project/project/settings.py
>>> print os.path.join(__file__, '..', '..')
/some/path/project/project/settings.py/../../
>>> # now abspath normalizes the path two levels up
>>> print os.path.abspath(os.path.join(__file__, '..', '..'))
/some/path/project
>>> # now you figure out the project name so that you can get the project templates folder
>>> print os.path.basename(os.path.abspath(os.path.join(__file__, '..', '..')))
project
>>> print os.path.join(PROJECT_PATH, PROJECT_NAME, 'templates')
/some/path/project/project/templates
>>> print os.path.join(PROJECT_PATH, 'recipe', 'templates')
/some/path/project/recipe/templates
In your settings.py, find and edit this -
TEMPLATE_DIRS = (
os.path.join(os.path.abspath(os.path.dirname(__file__)), "templates"),
)
I am very new to Django and, following their tutorial, I am having issues loading a custom admin template (specifically, I am having trouble at the bottom of https://docs.djangoproject.com/en/1.4/intro/tutorial02/).
I have updated my TEMPLATE_DIRS so it looks like:
TEMPLATE_DIRS = (
'/Users/myname/Developer/Eclipse/templates/admin',
)
I have also tried:
TEMPLATE_DIRS = (
'/Users/myname/Developer/Eclipse/templates/admin/base_site.html',
)
The path for where I am storing this custom template follows:
"/Users/myname/Developer/Eclipse/templates/admin" (copy and pasted directly from file info)
Could someone point out what I am doing wrong? Thanks in advance.
Unless you don't use your admin folder for all the templates use this (I guess you want to overwrite admin templae. So admin folder should be subfolder of your template folder):
TEMPLATE_DIRS = (
'/Users/myname/Developer/Eclipse/templates/',
)
PROJECT_ROOT = os.path.dirname(__file__)
TEMPLATE_DIRS = (os.path.join(PROJECT_ROOT, "templates"),)
I'm receiving this error when trying to render a template in django:
TemplateDoesNotExist
...
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
Here's my settings.py entry:
SETTINGS_PATH = os.path.normpath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'template'),
)
Here's my view that's being called:
def handler(request):
if request.method == 'GET':
NewAppFormSet = modelformset_factory(Application)
return render_to_response(request, "template/apps.html",{"new_app_form":NewAppFormSet})
I've tried every possible combination of paths, adjusting permissions, etc. The real issue seems to be with the template loaders, as they are not recognizing any paths set forth in TEMPLATE_DIRS. I've hardcoded this string, to no avail. I've also ran python manage.py runserver with sudo / as root. I'm at a loss...
I had a very similar issue which was driving me crazy. It turns out that the book I was using to learn Python/Django did not say that I had to set the templates directory in settings.py expressly. Very confusing. I am using Python 2.6 & Django 1.3.1 - maybe a previous version automatically found the templates dir. Anyway, after pulling my hair out for a day and reading a lot on this site I figured out that I had to replace the default TEMPLATE_DIRS assignment with the following in settings.py:
# Find templates in the same folder as settings.py.
SETTINGS_PATH = os.path.realpath(os.path.dirname(__file__))
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(SETTINGS_PATH, 'templates'),
)
Thanks stackoverflow!!
I'm not sure what example I was following that specified the render_to_response shortcut method requires the views' request object to be passed, but it was as easy as changing it to:
return render_to_response("template/apps.html",{"new_app_form":NewAppFormSet()})
Try assigning SETTINGS_PATH with os.path.realpath:
SETTINGS_PATH = os.path.realpath(os.path.dirname(__file__))
By any chance, you might have copy-pasted the TEMPLATE_DIRS you have now, did you leave the default one lower down the SETTINGS file?
From my settings.py:
BASE_DIR = os.path.dirname(__file__)
def RELATIVE_PATH(*args):
return os.path.join(BASE_DIR, *args)
TEMPLATE_DIRS = (
RELATIVE_PATH('template'),
)
what if you try
return render_to_response(request, "apps.html",{"new_app_form":NewAppFormSet})
instead of
return render_to_response(request, "template/apps.html",{"new_app_form":NewAppFormSet})