Django build_absolute_uri抛出NoReverseMatch错误

时间:2013-08-07 00:29:34

标签: python django django-templates django-template-filters

尝试了之前发布的众多相关解决方案,但效果不佳。我显然错过了一些东西。我正在使用Django 1.4。我有一个生成随机数量的jpeg文件的进程。我正在尝试创建视图/模板/模板标签的组合,这将让我动态显示图像。不涉及任何模型。使用了来自Lois Di Qual

的模板标签过滤器的想法

settings.py文件安装了正确的TEMPLATE_CONTEXT_PROCESSORS。提前谢谢。

这就是我所拥有的:

模板:

{% load mytemplatetags %}
{% for fn in jpg_names %}
    {{ fn|absolute_img_uri:request }}  
{% endfor %}

模板标签

from django import template
from django.template.loader import render_to_string
from django.core.urlresolvers import reverse

register = template.Library()

'''
Usage: {{ fn|absolute_media_url:request }}
'''
@register.filter
def absolute_img_uri(fn, request):
    return request.build_absolute_uri(reverse("returnImage", kwargs={'fn':fn}))

查看:

def returnImage(request,**kwargs):
path = "/opt/local/var/media/facstool/"
fn = kwargs['fn']
img = open(str(path)+fn,'rb').read()
response = HttpResponse(img,mimetype='image/jpeg')
return response

的url:

import ri.tools.views as rv
urlpatterns = patterns('ri.tools.views',
url(r'^$', 'index', name="index"),
url(r'defineParameters','defineParameters',name='defineParameters'),
url(r'downloadcsv','downloadcsv',name='downloadcsv'),
url(r'downloadpdf','downloadpdf',name='downloadpdf'),
url(r'downloadZipFile','downloadZipFile',name='downloadZipFile'),
url(r'returnImage/(?P<fn>.*)',rv.returnImage,name='returnImage'),
)

* - 修改 - *

回溯:

NoReverseMatch at /tools/defineParameters
Reverse for 'returnImage' with arguments '()' and keyword arguments '{'fn': u'Lep-_Clone12_Median__ss_293F_Median__ss_293F_H8_graph.jpg'}' not found.
Request Method: POST
Request URL:    http://localhost:8000/tools/defineParameters
Django Version: 1.4
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'returnImage' with arguments '()' and keyword arguments '{'fn': u'Lep-_Clone12_Median__ss_293F_Median__ss_293F_H8_graph.jpg'}' not found.
Exception Location:  /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 396
Python Executable:  /opt/local/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
Python Version: 2.7.2

1 个答案:

答案 0 :(得分:1)

好吧,我没有找到解决方案,为什么它不起作用,但我确实找到了一个有效的解决方案。希望这可以帮助其他人,或者它可以为某些人提供原始方法失败的原因。

我做了以下更改:

模板:

{% for fn in jpg_names %}
  <img src="/tools/returnImage/{{ fn }}"> 
{% endfor %}

URLS:

urlpatterns = patterns('ri.tools.views',
url(r'^$', 'index', name="index"),
url(r'defineParameters','defineParameters',name='defineParameters'),
url(r'downloadcsv','downloadcsv',name='downloadcsv'),
url(r'downloadpdf','downloadpdf',name='downloadpdf'),
url(r'downloadZipFile','downloadZipFile',name='downloadZipFile'),
url(r'returnImage/.*','returnImage',name='returnImage'),)

查看:

def returnImage(request):
   path = "/opt/local/var/media/facstool/"
   _, fn = os.path.split(request.META['PATH_INFO'])
   f = open(str(path)+fn,'rb')
   img = f.read()
   f.close()
   os.remove(str(path)+fn)
   response = HttpResponse(img,mimetype='image/jpeg')
   return response

基本上,这会将文件名添加到img标记中,然后在视图中删除文件名,在服务器上打开它,将其读入变量,删除原始文件并返回图像。这是我想要的行为,我只是不明白为什么OP不起作用。