how to use webpy's web.template.render() with a relative path when deployed on Apache

371 views
Skip to first unread message

吕仁坤

unread,
Oct 22, 2012, 1:45:44 AM10/22/12
to we...@googlegroups.com
The following code works using the development server but not when running on my Apache server.

render = web.template.render('templates/')
return render.index(self)

What I don't want to do is use an absolute path, because I'd like to be able to move my project files around without having to tinker with the code to keep it working.


Anand Chitipothu

unread,
Oct 22, 2012, 2:06:54 AM10/22/12
to we...@googlegroups.com
Try this:

import os.path

root = os.path.dirname(__file__)
render = web.template.render(os.path.join(root, "templates/"))

Anand

redfish

unread,
Oct 22, 2012, 4:15:36 AM10/22/12
to we...@googlegroups.com
but  the current python file  and 'templates' in different directory

在 2012年10月22日星期一UTC+8下午2时07分17秒,Anand写道:

Jason Macgowan

unread,
Oct 22, 2012, 8:02:23 PM10/22/12
to we...@googlegroups.com
What Anand posted will work. Your templates/ directory has to be in
the same root directory as the python file; otherwise the development
server would not have worked. If I'm mistaken, post your directory
tree for the project so we can see what's going on
> --
> You received this message because you are subscribed to the Google Groups
> "web.py" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/webpy/-/aOLVcNW4QzAJ.
>
> To post to this group, send email to we...@googlegroups.com.
> To unsubscribe from this group, send email to
> webpy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/webpy?hl=en.

redfish

unread,
Oct 22, 2012, 11:48:12 PM10/22/12
to we...@googlegroups.com
hi Jason Macgowan:
This is  my  directory tree

root
----config
----------__init__.py
----------setting.py(the code in this file)
----templates
----------index.html/view.html e.g.
----static 
----------css,image e.g.
----main.py (from conifg import setting)

在 2012年10月23日星期二UTC+8上午8时02分27秒,Jason Macgowan写道:

Jason Macgowan

unread,
Oct 23, 2012, 7:14:26 PM10/23/12
to we...@googlegroups.com
I wonder why that works with the dev server?

Anyway, just use this:

import os.path

root = os.path.dirname(__file__)
render = web.template.render(os.path.join(root, '..', 'templates/')) #
join is smart enough to know what you're trying to do
> https://groups.google.com/d/msg/webpy/-/9AGMVslCe4sJ.

吕仁坤

unread,
Oct 23, 2012, 9:50:06 PM10/23/12
to we...@googlegroups.com
thanks very much,the problem solved .
and i  print  the  "os.path.join(root, '..', 'templates/')"   it's 'E:\pydemo\simpleTodo\config\..\templates/',and not 
'E:\pydemo\simpleTodo\templates'   ,so magic join,but how does it let the 'wrong path' effect?

2012/10/24 Jason Macgowan <jasonm...@gmail.com>
Reply all
Reply to author
Forward
0 new messages