Eclipse + PyDev + Django 1.4 - Template directory problems - django

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

Related

django.template.loaders.app_directories.Loader fails to find the template file

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/"),
)

Django TEMPLATE_DIRS looking in the wrong directory?

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

How do I find my project name in a Django project?

I have a pycharm project and, presumably, a Django project. Perhaps they are one and the same, perhaps not - I'm unsure as to the distinction.
Anyway, in my settings.py file (which is in the root of project directory, which is what I presume is my pycharm project) I have:
ROOT_URLCONF = 'dumpstown.urls'
Does that mean dumpstown is my project name? Or my pycharm project name? What is the distinction? Because I also have a folder called dumpstownapp and this has all my models.py and view.py files. I would have thought that dumpstownapp was the Django project, but I really don't know!
So, to be concise:
In this folder setup
folderA
folderB
views.py
models.py
<other stuff>
templates folder
settings.py
<other stuff>
which is the "Django project name" ~ and by that I mean, if I have a UserProfile defined in my models.py (shown above) what would be the AUTH_PROFILE_MODULE entry I'd need for it? I'm getting several understandings from the django docs - I'd assume
dumpstownapp.models.UserProfile
But from the docs I'd get
dumpstownapp.UserProfile
Or maybe my app is called dumpstown? and then what do I get?
FolderA is the Django project folder, and folderB is the Django app folder.
I haven't used a UserProfile, but according to the docs ( https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users) it should contain a string containing the Django app name and the model name separated by a dot, so "dumpstownapp.UserProfile" should be correct. If you show the error someone can probably give you better help..
Django documentation used to say that the parent of the project folder (i.e. the parent of folderA) should be on the path, but I believe that has been changed to also include the project folder itself (i.e. folderA) -- since it makes sharing of Django apps much easier. PyCharm seems to assume that is the case, since pressing Alt+F7 to auto-add an import for a newly used module create an import statement that assumes folderA is on the import path (I'm a relative newcomer to PyCharm, and I'm using it on a project that started in the Django 0.96 era, so I might just have things set up wrong..) But folderA is both the Django and the PyCharm project (the .idea file is where PyCharm stores its project data).
In one of my django-app git-submodule projects I needed to find out the name of the Django project that django-app/library was used in. To that end, I tried to get the path of the file that was being executed, find my package in the path and take the folder above it. However, it turned out that on the production server the project was deployed in a folder with a different name (a standard name like www or something along those lines). So this way is not fully reliable.
So I ended up setting a PROJECT variable in the django settings file and using that instead.

eclipse cannot find my templates dir in django project

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.

Django StaticFiles APP and Wsgi

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...