The code for my web app is currently on dropbox and I simply change the TEMLPLATE_DIRS variables in the settings.py module when working on my work and home computers.
I have run into an issue this evening when firing up the app. I am getting a TemplateDoesNotExist error, here are the details:
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
/Users/me/Dropbox/app/MyApp/Users/me/Dropbox/app/MyApp/templates/App/Page 1/pageone.html (File does not exist)
Basically the first /Users/me/Dropbox/app/MyApp shouldn't be there.
settings.py
TEMPLATE_DIRS = (
"Users/danielholmes/Dropbox/app/MyApp/templates/",
)
This is going to be something stupid i think - please let me know if more info is required.
Many thanks
Never hardocde the directory paths in settings files. Let Python generate the absolute path names for you. This makes your project portable across different environments. Below is a good approach to define paths (and may be solve your issue also):
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'),
)
Related
The template file is saved under the app directory, but it raises TemplateDoesNotExist exception while rendering:
Template-loader postmortem as following:
Django tried loading these templates, in this order:
Using loader django.template.loaders.app_directories.Loader:
...
$PROJECT/apps/myapp/templates/search.html (File does not exist)
...
I'm wondering why it looks for:
$PROJECT/apps/myapp/templates/search.html
rather than:
$PROJECT/apps/myapp/templates/myapp/search.html
The latter does exist indeed
$PROJECT/apps/myapp/templates/search.html. That is the path it'll look for as the doc says.
django.template.loaders.app_directories.Loader will look for a templates directory inside all the INSTALLED_APPS in order.
django.template.loaders.filesystem.load_template_source: This loader loads templates from the filesystem, according to TEMPLATE_DIRS. It is enabled by default.
django.template.loaders.app_directories.load_template_source: This loader loads templates from Django applications on the filesystem. For each application in INSTALLED_APPS, the loader looks for a templates subdirectory. If the directory exists, Django looks for templates there.
This means you can store templates with your individual applications, making it easy to distribute Django applications with default templates. For example, if INSTALLED_APPS contains ('myproject.polls', 'myproject.music'), then get_template('foo.html') will look for templates in this order:
/path/to/myproject/polls/templates/foo.html
/path/to/myproject/music/templates/foo.html
Note that the loader performs an optimization when it is first imported: it caches a list of which INSTALLED_APPS packages have a templates subdirectory.
This loader is enabled by default.
Supposing that you have a default project and app created as
django-admin startproject xyz
django-admin startapp abc
both xyz and abc folders are in main project folder
Following minimal changes are required to get first template working
in settings.py change INSTALLED_APPS to add 'abc' (I forgot and failed)
in urls.py add
add line: import abc.views
change urlpatterns to add path('abc/', abc.views.index)
in index definition of views.py use loader.get_template('abc/index.html')
Create abc/templates/abc/index.html file ( I wrote "template" and spent few hours before I realized that is was "templates")
make sure the templates and inside folders have execute permission and html file have read permission for "other" users. (I worked as root and failed due to this error)
Restart the project (runserver or apache2 service)
Summary of my mistakes
Edit INSTALLED_APPS
"templates" is default
setup x and r permissions
Ensure your app is added to settings.py INSTALLED_APPS
Ensure your config in apps.py has a name equal to the name of the application directory.
class DemoConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'app' # this must be the same as the parent directory
Add them to your settings file.
import os
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, "/apps/myapp/templates/"),
)
I have a Django 1.3 project with this options in settings.py
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATIC_ROOT = os.path.join(SITE_ROOT, 'static')
MEDIA_ROOT = os.path.join(SITE_ROOT, 'media')
TEMPLATE_DIRS = (
os.path.join(SITE_ROOT, 'templates'), )
But in Django 1.4 by default settings.py is moved in subdirectory with name that is equal to project name. Because of that static, media and templates directories now have to be moved in the same subdirectory?
Is this what I have to do, or just change STATIC_ROOT, MEDIA_ROOT and TEMPLATE_DIRS options?
I know that both variants are OK, but what is best practice for this in Django 1.4?
And also I know that every app can have it's own templates and static directories.
And is it better to put all other application directories inside the same subdirectory? This is not what is happening by default using manage.py startapp
OK the scheme that I follow is this:
myproject/requirements.txt - pip installable packages
myproject/deployment - Deployment stuff like server config files, fixtures(dummy data), etc.
myproject/docs - project's docs
myproject/tests - project's tests
myproject/myproject - project's operational code(and settings.py, urls.py)
Expanding myproject/myproject folder:
myproject/myproject/app1 - a regular app(encompassing its specific templates/static files)
myproject/myproject/app2 - another regular app(same as above)
myproject/myproject/website - semi special app, by convention.
This website app houses basically 4 things:
1) an empty models.py(so that django will consider it as a valid app)
2) a views.py with the entry point index view. Maybe some other views that don't fit in any other specific app.
3) a management dir with custom django commands which apply to the whole project.
4) a templates dir that has the 404.html and 505.html. Also it has a subdir called website that includes universal/base html templates that every other app extends, plus
the index.html.
5) a static dir with subsequent subdirs named css, js and media for global static files.
Nothing exotic I guess. I think that most people follow a similar pattern, but I would like to here any inefficiencies with this, if any.
EDIT:
with regards to settings for production VS development I use
the popular settings_local pattern, which you can read here and eventually will
lead you here, which describes a better pattern.
I was trying to use a template but it keeps giving me a TemplateDoesNotExist exception. If I look at the Template-loader postmortem, I notice that it is looking in the wrong directory. It is trying to find templates within my workspace folder and not within the location of the project (outside workspace)
If I try to specify a path in settings, it just appends that path to the workspace path.
I'm quite new to Eclipse and PyDev -- how can I sort this out?
I'm using Eclipse 3.7.2 with PyDev 2.4.0 and Django 1.4
This is what I do:
At the top of your file, write these two lines:
import os
ROOT_PATH = os.path.dirname(__file__)
Change TEMPLATE_DIRS to be:
os.path.join(ROOT_PATH, 'templates'),
The 'templates' part should be the relative path to your settings file. In my case it is just templates.
You can also use ROOT_PATH anywhere in the settings, along with join, and it's not a bad idea to use it, because this way your settings file is more universal then constrained to the same machine.
In your settings.py file, there should be a TEMPLATE_DIRS directive. This is a tuple which allows you to set up multiple directories (see below) or just a single entry, as is usually the case. Set that to the appropriate folder and it should work. If that doesn't check permissions on the folder to ensure the runtime user has access to it and the templates.
TEMPLATE_DIRS = (
'/var/www/templates/',
'/opt/myweb/django/templates',
)
recently I want to learn django in eclipse, but when I set up eclipse environment , I get a problem...
the django project cannot find my templates folder in eclipse
in the setting.py:
import os
TEMPLATE_DIRS = (
os.path.abspath('templates'),
)
the templates folder:
D:\django_workspace\eagle\eagle\templates
I run 'manage.py shell' and get:
>>> os.path.abspath('templates')
'D:\\django_workspace\\eagle\\eagle\\templates'
but, in eclipse I run the command:
>>>os.path.abspath('templates')
'D:\\Program Files\\eclipse\\templates'
it seems that the os root path is set to be 'D:\Program Files\eclipse' where I install the eclispe
how can I solve this problem ??
thx
The way you're doing it, you're getting the absolute path from a relative path based on the current working directory, so, your code will fail depending on your current directory...
Better would be actually calculating the path based on __file__ from your module (something as os.path.join(os.path.dirname(__file__), 'templates') -- not really sure where your settings file is relative to your templates, so, in your environment it may be a bit different.
It's not so easy questions as others.. at least. it's not a problem to setup simple one-dir-based static files location..
https://bitbucket.org/sirex/django-starter/src
There's such an interesting project in here.. this one is using distribute and buildout for making whole project and django with modules in one dir. you can migrate from dev to production mode easy and etc.. all you need is just to rename dir, and type "make" in it, and that's it =) there's manual in there...
Situation which works with python server, and don't work with apache mod_wsgi:
Default static files location is: "var/htdocs/static". This can be overridden with one static dir location for example apps/myapp/myapp/static/. This works with python webserver but doesn't work with wsgi/apache. wsgi can't see anything apart default directory.. example: http://localhost:8000/static/css/main.css works but with apache same url doesn't work. and this file lies in myproject/apps/myapp/myapp/static/css/main.css although default static dir is var/htdocs/static =)
As far as I understand this overriding made with StaticFiles application in settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BUILDOUT_DIR, 'var', 'htdocs', 'static')
STATICFILES_DIRS = (
os.path.join(BUILDOUT_DIR, 'project', 'static'), # <-- why "project" and not "apps" I don't know X_X
)
maybe this one is incorrect, I don't know but with py-server this works. apache vhost works with default location.. and setuped to "var/htdocs/static".
Maybe problem is in wsgi script?
#!/usr/local/bin/python2.6
import os,sys
sys.path[0:0] = [
'/usr/local/lib/python2.6/site-packages/',
'/www/webapp/visimes/eggs/PIL-1.1.7-py2.6-freebsd-8.1-RELEASE-amd64.egg',
'/www/webapp/visimes/eggs/South-0.7.3-py2.6.egg',
'/www/webapp/visimes/eggs/django_annoying-0.7.6-py2.6.egg',
'/www/webapp/visimes/eggs/coverage-3.4-py2.6-freebsd-8.1-RELEASE-amd64.egg',
'/www/webapp/visimes/eggs/django_debug_toolbar-0.8.4-py2.6.egg',
'/www/webapp/visimes/eggs/django_extensions-0.6-py2.6.egg',
'/www/webapp/visimes/eggs/django_test_utils-0.3-py2.6.egg',
'/www/webapp/visimes/eggs/ipdb-0.3-py2.6.egg',
'/www/webapp/visimes/eggs/ipython-0.10.1-py2.6.egg',
'/www/webapp/visimes/eggs/djangorecipe-0.21-py2.6.egg',
'/www/webapp/visimes/eggs/zc.recipe.egg-1.3.2-py2.6.egg',
'/www/webapp/visimes/eggs/zc.buildout-1.5.2-py2.6.egg',
'/www/webapp/visimes/eggs/BeautifulSoup-3.2.0-py2.6.egg',
'/www/webapp/visimes/eggs/setuptools-0.6c12dev_r88795-py2.6.egg',
'/www/webapp/visimes/parts/django',
'/www/webapp/visimes',
'/www/webapp/visimes/project', # <-- this one need for monitor.py which i put in there
'/www/webapp/visimes/apps/portal', # <-- startapp.sh script some how forgot to add this dir, it's my default app dir, which must be generated with startapp.sh and added in here..
]
import djangorecipe.wsgi
if __name__ == '__main__':
djangorecipe.manage.main('project.development')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.development'
import monitor
monitor.start(interval=1.0)
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I added last 4 lines by my self. because I couldn't start apache.. I'm guessing that djangorecipe.wsgi should handle everything else with staticFile override.. anyway please, check out that package, if you on linux or mac, and try it by your self. it must work
ps. (btw bin/django need to dublicated as bin/django.wsgi and etc/apache.conf is generated vhost for apache)
I'd really apriciated if somebody would try to launch this "Starter" manually with wsgi... then you'd understand everything.=)
Edit: Any information about how can WSGI understand where he needs to search static files apart default location from django settings, is REALLY appreciated =)
There is lots of documentation on the official mod_wsgi site for understanding how to use it. This includes how to set it up for serving static media files. See:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines
WSGI has nothing whatever to do with serving static files. This is all clearly covered in the Django deployment documentation - as Torsten suggests in the comments, you'll need to point Apache at your static files, probably via an alias.
I must say though that this project looks very dodgy. Manually adding a load of eggs to sys.path is not the right way to go about things - a much better way would be to use something like virtualenv, which manages all that for you.
No way of having some kind of overriding like django does with WSGI..
there's great command in django "manage.py collectstatic" which places all files from STATIC_DIR list (in settings.py) to main static directory.. in fact this commands just copy files from all those dir's and that's it =)
Would be great to know, how could I make this copying automatic when any file in that dir would be updated.. same thing like monitor.py for automatic wsgi reload when source is modified...