[英]I can't figure out how to refer to my template from a Django view
I am new to Django and cannot figure out what the path to my templates is supposed to look like. 我是Django新手,无法弄清楚模板的路径应该是什么样。 My project's directory tree looks like this:
我项目的目录树如下所示:
blog
blog/blog
blog/blog/__init__.pyc
blog/blog/wsgi.pyc
blog/blog/urls.py
blog/blog/urls.pyc
blog/blog/wsgi.py
blog/blog/__init__.py
blog/blog/settings.py
blog/blog/settings.pyc
blog/home
blog/home/views.py
blog/home/templates
blog/home/templates/home
blog/home/templates/home/main.html
blog/home/__init__.pyc
blog/home/urls.py
blog/home/urls.pyc
blog/home/models.py
blog/home/tests.py
blog/home/__init__.py
blog/home/views.pyc
blog/manage.py
Here is my view (from blog/home/view.py
): 这是我的观点(来自
blog/home/view.py
):
from django.shortcuts import render_to_response
def home(request):
return render_to_response("home/main.html", {"name" : "maxwell"})
A redacted copy of my settings.py file can be found here: http://pastebin.com/UMTepK9j 我的settings.py文件的编辑过的副本可以在这里找到: http : //pastebin.com/UMTepK9j
And finally, here is the error I get when I browse to 127.0.0.1:8000
: 最后,这是我浏览至
127.0.0.1:8000
时遇到的错误:
TemplateDoesNotExist at /home/main.html
Can anyone tell me what the path ought to look like in my call to render_to_response
? 谁能告诉我我对
render_to_response
应该是什么样的路径?
The TEMPLATE_DIRS
in your settings.py should point to the template
folder. 您settings.py中的
TEMPLATE_DIRS
应该指向template
文件夹。
Should be something like this: 应该是这样的:
TEMPLATE_DIRS = (
'../home/templates'
)
That should work. 那应该工作。
Note: There you're using relative paths which is not recommended. 注意:不建议使用相对路径。 You should have something like this in your
settings.py
: 您应该在
settings.py
:
import os
settings_dir = os.path.dirname(__file__)
PROJECT_ROOT = os.path.abspath(os.path.dirname(settings_dir))
...
TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, '../home/templates/'),
)
Also, you're calling render_to_response
without passing it a RequestContext
. 另外,您在调用
render_to_response
而不将其传递给RequestContext
。 With this, your templates will lack of some variables important for your project like user
. 这样,您的模板将缺少一些对您的项目重要的变量,例如
user
。 It's better to call it this way: 最好这样称呼它:
return render_to_response(
"home/main.html",
context_instance=RequestContext(
request,
{'name':'maxwell'}
)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.