简体   繁体   English

我不知道如何从Django视图中引用我的模板

[英]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.

相关问题 无法找出我的 jinja2/django 模板上的错误 - Can't figure out the error on my jinja2/django template 我无法在 django 中使用渲染查看我的模板 - I can't view my template with render in django 我不知道为什么我的makemigrations无法在Django中工作,它显示以下错误 - I can't figure out why my makemigrations isn't working in django,its showing following error 我不知道如何阻止它被打印 - I can't figure out how to stop this from being printed Django:无法找出Django中的编码 - Django : Can't figure out encoding in Django 我无法弄清楚Django qeryset用于通过条件从字段中检索值 - I can't figure out the django qeryset for retriving a value from a field by a condition 为什么我的Django视图无法从Django模板中创建的标签接收数据? - Why can't my Django view receive data from a created tag in my Django template? 我在 Django 中有一个带有 ManyToManyField 的表。 无法弄清楚如何更新表条目 - I have a table in Django with ManyToManyField. Can't figure out how to update a table entry 我的代码中不断出现错误,无法弄清楚 - I keep getting an error in my code and can't figure out 我正在使用 python 3.9,但我不知道如何在我的 Windows 10 中安装 pyaudio - I am using python 3.9 and I can't figure out how to install pyaudio in my Windows 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM