这是我的观点.py:
from django.http import HttpResponse
from django.template.loader import get_template
from django.template import Context
def login(request):
t = get_template('login.html')
return HttpResponse(t.render)
在我的 urls.py 中我放入
(r'^login/', include('project.views.login')),
显示我的模板
我在settings.py中设置了我的模板目录
但是我收到一个名为
的错误ImportError at /login/
No module named login
怎么了?
最佳答案
include
用于包含其他应用程序的其他 url 配置。如果您尝试为一个特定 View 添加 url 模式,则不应使用它。你应该有类似的东西
(r'^login/', 'project.views.login'),
另一个问题是您返回响应的位置。 render
是一种采用上下文的方法(请参阅 the docs )
def login(request):
t = get_template('login.html')
c = Context({})
return HttpResponse(t.render(c))
实际上,您通常不会加载模板、渲染它,然后返回响应。有两个快捷功能,render
和 render_to_response
,减少重复。
您在这里犯了一些基本错误。我建议您阅读 Django 教程(再次强调,如果您已经看过它们)。 Tutorial 3特别解释了所有这些内容。
关于Python 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8289672/