Django在render_to_response时查看ValueError

发布于 2024-11-29 01:37:43 字数 2451 浏览 0 评论 0原文

我正在为即将到来的项目学习一些 Django 教程,但我无法正确加载模板。调试模式返回一个“ValueError”,表示它“需要超过1个值才能解压”。我正在运行 Django 的捆绑服务器。知道有什么问题吗?任何帮助表示赞赏。

这是跟踪:

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  111. response = callback(request, *callback_args, **callback_kwargs)

File "/Users/tinwaijosephlee/Sites/djcode/dev2/../dev2/views.py" in hours_ahead
  26. return render_to_response('plus.html', {'offset': offset, 'dt': dt})

File "/Library/Python/2.7/site-packages/django/shortcuts/__init__.py" in render_to_response
  20. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

File "/Library/Python/2.7/site-packages/django/template/loader.py" in render_to_string
  181. t = get_template(template_name)

File "/Library/Python/2.7/site-packages/django/template/loader.py" in get_template
  157. template, origin = find_template(template_name)

File "/Library/Python/2.7/site-packages/django/template/loader.py" in find_template
  128. loader = find_template_loader(loader_name)

File "/Library/Python/2.7/site-packages/django/template/loader.py" in find_template_loader
  93. module, attr = loader.rsplit('.', 1)

这是我的视图代码:

from django.shortcuts import render_to_response
from django.http import HttpResponse

import datetime
import sys
import os

def current_datetime(request):
    now = datetime.datetime.now()
    return render_to_response('current_datetime.html', {'current_date': now})

def hours_ahead(request, offset):
    offset = int(offset)
    dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
    return render_to_response('plus.html', {'offset': offset, 'dt': dt})

这是我的 urls.py

from django.conf.urls.defaults import *
from dev2.views import *

urlpatterns = patterns('',
    (r'^time/$', current_datetime),
    (r'^time/plus/(\d{1,2})/$', hours_ahead),
)

这是 settings.py 中的模板加载器配置

TEMPLATE_LOADERS = (
    '/Users/some_user_name/Sites/djcode/dev2/template',
)

这是模板 html 文件

<html><head></head><body>It is now {{ current_date }}.</body></html>

I am going through some tutorials of learning Django for an upcoming project, and I could not load templates correctly. The debug mode returns a "ValueError" saying that it "need more than 1 value to unpack". I am running Django's bundled server. Any idea what's the problem? Any help is appreciated.

Here's the trace:

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  111. response = callback(request, *callback_args, **callback_kwargs)

File "/Users/tinwaijosephlee/Sites/djcode/dev2/../dev2/views.py" in hours_ahead
  26. return render_to_response('plus.html', {'offset': offset, 'dt': dt})

File "/Library/Python/2.7/site-packages/django/shortcuts/__init__.py" in render_to_response
  20. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

File "/Library/Python/2.7/site-packages/django/template/loader.py" in render_to_string
  181. t = get_template(template_name)

File "/Library/Python/2.7/site-packages/django/template/loader.py" in get_template
  157. template, origin = find_template(template_name)

File "/Library/Python/2.7/site-packages/django/template/loader.py" in find_template
  128. loader = find_template_loader(loader_name)

File "/Library/Python/2.7/site-packages/django/template/loader.py" in find_template_loader
  93. module, attr = loader.rsplit('.', 1)

Here's my view code:

from django.shortcuts import render_to_response
from django.http import HttpResponse

import datetime
import sys
import os

def current_datetime(request):
    now = datetime.datetime.now()
    return render_to_response('current_datetime.html', {'current_date': now})

def hours_ahead(request, offset):
    offset = int(offset)
    dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
    return render_to_response('plus.html', {'offset': offset, 'dt': dt})

Here's my urls.py

from django.conf.urls.defaults import *
from dev2.views import *

urlpatterns = patterns('',
    (r'^time/

Here's the template loader config in settings.py

TEMPLATE_LOADERS = (
    '/Users/some_user_name/Sites/djcode/dev2/template',
)

And here's the template html file

<html><head></head><body>It is now {{ current_date }}.</body></html>
, current_datetime), (r'^time/plus/(\d{1,2})/

Here's the template loader config in settings.py


And here's the template html file


, hours_ahead),
)

Here's the template loader config in settings.py

And here's the template html file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自演自醉 2024-12-06 01:37:43

您在 settings.py 中设置了错误的内容。 TEMPLATE_LOADERS 用于查找和加载模板的 Python 代码。您希望将目录放入 TEMPLATE_DIRS 中。

You've set the wrong thing in settings.py. TEMPLATE_LOADERS is for Python code that finds and loads templates. You want to put your directory into TEMPLATE_DIRS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文