0

我正在使用 Apache+wsgi_mod 开发一个 django 应用程序。我定义了一个简单的查看器返回用户的图像。但如果用户没有任何图片,我想渲染一个静态标准图像(取决于性别),存储在我的静态文件夹中。我知道我可以使用 static.serve() 但 django 文档阻止了这一点。如何从查看器提供静态文件?

更新:

viewer 是在views.py 中定义的一种方法(例如下面的img)

我可能想将 HttpResponseRedirect() 返回到我的静态内容。但比我需要绝对 URL。

我需要这个,因为我有类似的东西:

def img(request, usr_id):
    usr_image = get_usr_image(usr_id)
    if usr_image == None:
        return respond_with_standard_image()
    else:
        response = HttpResponse(mimetype='image/jpg')
        response.write(usr_image)
        return response

并希望使用标准用户图像进行响应。

更新2:

我可以做这样的事情:

return HttpResponseRedirect('http://' + request.get_host() + settings.STATIC_URL + 'img/125px-Silver_-_male.png')

但我不满意。

4

1 回答 1

0

Django should'nt be involved into rendering images or any other static file. You should put your users images in a custom directory, and serving it directly with your web server.

Rendering a different image if the original file does not exists can easily be achieved with a rewrite rule.

于 2011-07-28T12:26:21.263 回答