eclipse cannot find my templates dir in django project - django

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.

Related

django, template_docs project, LIBREOFFICE path, Failed to initialize LibreOfficeKit

Hello everyone I am trying to make template_docs to work following the instructions:
Installation is as simple as pip install templated-docs. Don’t forget
to add templated_docs to your INSTALLED_APPS Django settings. Then
specify the path to LibreOffice’s program directory in your settings:
TEMPLATED_DOCS_LIBREOFFICE_PATH = '/usr/share/libreoffice/program'
However I am not sure about the path of libreoffice: TEMPLATED_DOCS_LIBREOFFICE_PATH = '/usr/share/libreoffice/program'
In my case the software is installed here: C:\Program Files (x86)\LibreOffice 5\program
Therefore I in my setting.py I added: TEMPLATED_DOCS_LIBREOFFICE_PATH = 'C://Program Files (x86)//LibreOffice 5/program' but it did not work.
The error that I get is pylokit.lokit.LoKitInitializeError: Failed to initialize LibreOfficeKit
Why two slashes instead of C:/Program Files (x86)/LibreOffice 5/program?
Anyway, according to https://github.com/xrmx/pylokit:
LibreOfficeKit currently works only on Linux systems.
pylokit is imported on line 22 in templated-docs/templated_docs/init.py.
from pylokit import Office

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

Eclipse + PyDev + Django 1.4 - Template directory problems

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

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

Django start new project error

i have installed Python2.6 and Django1.2.4 on windows vista. or so i think django is installed.
i test it this by opening the python shell(command prompt from the python folder) and typed this:
import django
django.get_version()
The result was "1.2.4". From this i understand that firstly python works, and second that django is installed, recognized.
Following the tutorial from Django website i get stuck on the first thing: creating a project
django-admin.py startproject mysite
I wrote this in a new command prompt window, chdir my way to a folder i decided it'll keep the project and then wrote the line above. The error is this:
E:\Workspace>django-admin.py startproject mysite
'django-admin.py' is not recognized as an internal or external command,
operable program or batch file.
I tried with writting "python" before the line but no.
I added in the Environmental Variables , in the PATH, the path to the bin folder of django and the path to python. I have the django-admin.py file in the Python/Scripts folder.
I understand this is a very easy , newbie mistake, but it currently blocks me. If somebody has gone through this error or knows about it, a bit of help would be much appreciated.
Thank you either way!
Did you to add the Python/Scripts to the PATH environmental variable? You'll need to restart any command prompts you have opened for the PATH change to take effect.
The same happened with me also and it is resolved by adding PATH environmental variable of Python\scripts.