Django 获取搜索中上载的直接URL-Wagtail
作为搜索的一部分,我已经定制了我的搜索以查看图像和文档。这很好用。但是,我不知道如何获取返回的上传图像/文档的URL。理想情况下,浏览器中会有一个下载/显示的链接。我同意../media/images/xxx url。但我似乎不知道如何在模板中获取这些数据。思想?搜索是由这个函数执行的Django 获取搜索中上载的直接URL-Wagtail,django,wagtail,wagtail-search,Django,Wagtail,Wagtail Search,作为搜索的一部分,我已经定制了我的搜索以查看图像和文档。这很好用。但是,我不知道如何获取返回的上传图像/文档的URL。理想情况下,浏览器中会有一个下载/显示的链接。我同意../media/images/xxx url。但我似乎不知道如何在模板中获取这些数据。思想?搜索是由这个函数执行的 # Search if search_query: results = [] page_results = Page.objects.live().search(search_query)
# Search
if search_query:
results = []
page_results = Page.objects.live().search(search_query)
if page_results:
results.append({'page': page_results})
doc_results = Document.objects.all().search(search_query)
if doc_results:
results.append({'docs': doc_results})
img_results = Image.objects.all().search(search_query)
if img_results:
results.append({'image': img_results})
search_results = list(chain(page_results, doc_results, img_results))
query = Query.get(search_query)
# Record hit
query.add_hit()
else:
search_results = Page.objects.none()
html是这样的
{% if search_results %}
{{ count }} results found.
{% for result in search_results %}
{% for k, v in result.items %}
{% if k == 'page' %}
{% for item in v %}
<p>
<h4><a href="{% pageurl item %}">{{ item }}</a></h4>
Type: Article<br>
Author: {{ item.specific.owner.get_full_name }}<br>
Publish Date: {{ item.specific.last_published_at}}
</p>
{% endfor %}
{% elif k == 'docs' %}
{% for item in v %}
<p>
<h4><a href="">{{ item.title }}</a></h4>
Type: Document<br>
Publish Date: {{ item.created_at }}
</p>
{% endfor %}
{% elif k == 'image' %}
{% for item in v %}
<p>
<h4><a href="">{{ item.title }}</a></h4>
Type: Image<br>
Publish Date: {{ item.created_at }}
</p>
{% endfor %}
{% endif%}
{% endfor %}
{% endfor %}
{% if search_results.has_previous %}
<a href="{% url 'search' %}?query={{ search_query|urlencode }}&page={{ search_results.previous_page_number }}">Previous</a>
{% endif %}
{% if search_results.has_next %}
<a href="{% url 'search' %}?query={{ search_query|urlencode }}&page={{ search_results.next_page_number }}">Next</a>
{% endif %}
{% elif search_query %}
No results found
{% endif %}
{%if搜索结果%}
找到{count}}个结果。
{搜索结果中的结果百分比为%}
{result.items%中的k,v为%1}
{%如果k=='页'%}
{v%中项目的%s}
类型:文章
作者:{{item.specific.owner.get_full_name}}
发布日期:{{item.specific.last_published_at}
{%endfor%}
{%elif k=='docs%}
{v%中项目的%s}
类型:文件
发布日期:{{item.created_at}
{%endfor%}
{%elif k=='图像'%}
{v%中项目的%s}
类型:图像
发布日期:{{item.created_at}
{%endfor%}
{%endif%}
{%endfor%}
{%endfor%}
{%if search_results.has_previous%}
{%endif%}
{%if search_results.has_next%}
{%endif%}
{%elif search_query%}
未找到任何结果
{%endif%}
Django为文件和图像提供了url
属性,wagtail也提供了这个属性
{{ item.url }}
Wagtail非常支持重定向到文件,并集成了django sendfile
。这两种方法都允许图像下载
从wagtail.images.views.service导入ServeView
URL模式=[
]
**编辑如下
生成映像“as foo”以访问单个属性-访问底层映像实例
{% image item original as item_img %}
{{ item_img.url }}
谢谢很遗憾,item.url是无效的标记。因此,我怀疑我的视图没有找到图像或文档的URL。几周来我一直在追求的东西。我不喜欢wagtail。我更新了答案,包括使用wagtail标签访问图像。希望这有帮助。太好了。结果是这样的。我会注意到,我必须将{%load wagtailimages_tags%}添加到我的模板页面,以便标签能够被拾取。你知道文件是否有同等标准吗?我在文档中找不到。请尝试使用Document.objects.all().specific()?如果您使用的是自定义文档模型,而不是自定义模型,那么只需为PDF之类的东西提供普通的上传。本例中的Document.object来自wagtail.documents.models。但尝试任何形式的搜索摇尾和文档只会让人头疼。无论如何,您已经解决了此线程的问题。非常感谢。
{% image item original as item_img %}
{{ item_img.url }}