Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django render_to_字符串失败-无法捕获错误?_Python_Django - Fatal编程技术网

Python Django render_to_字符串失败-无法捕获错误?

Python Django render_to_字符串失败-无法捕获错误?,python,django,Python,Django,这是另一个困扰我的问题 下面的代码在完成长时间运行的任务后作为管理命令运行 logger.debug('preparing email') context = { 'project': task.project, 'site': Site.objects.get(pk=settings.SITE_ID), 'task': task, 'user': task.user, } subject = 'Custom Assay Design of #%s - "%s"

这是另一个困扰我的问题

下面的代码在完成长时间运行的任务后作为管理命令运行

logger.debug('preparing email')

context = {
    'project': task.project,
    'site': Site.objects.get(pk=settings.SITE_ID),
    'task': task,
    'user': task.user,
}
subject = 'Custom Assay Design of #%s - "%s" has completed' % (
    project.pk, project.title)

logger.debug('email step 1')
try:
    content_txt = render_to_string(
        'analysis/email/analysis_complete.txt', context)
except:
    logger.debug('failed to render string')
    import traceback
    traceback.print_exc()

logger.debug('email step 2')
它一开始没有那么多的调试或一般的try/except,但现在就是这样

如果我一步一步地检查shell中的所有内容,它就会完美地工作

如果我将content_txt和content_html更改为字符串变量,则一切正常

实际上,输出看起来像

2014-06-12 10:03:24,831 primer_search_finished line:28   DEBUG    Callback made to process PrimerTask 237
2014-06-12 10:03:24,831 primer_search_finished line:30   DEBUG    Trying to get task 237
2014-06-12 10:03:24,878 primer_search_finished line:54   DEBUG    Task 237 for project 280:Test -- 2014-04-11 marked complete
2014-06-12 10:03:24,889 primer_search_finished line:60   DEBUG    preparing email
2014-06-12 10:03:24,897 primer_search_finished line:71   DEBUG    email step 1
每次尝试运行render_to_字符串时它都会中断

python版本--2.7.5 django版本——1.5.6


有什么想法吗?

运行脚本时的基本目录与从命令行测试运行时的基本目录不一样

我的建议是:

...
try:
    content_txt = render_to_string(
        '%s/analysis/email/analysis_complete.txt' % PROJECT_ROOT, context)
....
这将确保您实际上在正确的位置查找文件


在此之前,您必须定义
PROJECT\u ROOT
。(您可以使用相对于此文件的位置查看
\uuuuu文件\uuuuuu

您是否尝试
将\u呈现到\u字符串('analysis/email/analysis\u complete.txt',context\u instance=context)
?triewd dictionary=context&context\u instance=context(尽管正在查找请求实例)--两个都不做--但是很乐意尝试任何新的想法如果你删除了try,你可能会添加代码提供的堆栈跟踪。无论有没有尝试,它的行为都是一样的(没有输出,只是无声退出),这让我放弃了一个不错的想法,但不幸的是,这是不可能的,我使用了存储在设置和连接路径中的项目目录——将绝对路径转储到带有记录器的控制台以验证——相同的行为。。。。