I have customized my search to look at images and documents as part of that search. This is working fine. However I am not sure how to get the URL to the uploaded images/documents that are returned. Ideally there would be a link to download/display in browser. I am ok with the ../media/images/xxx url. But I can't seem to figure out how to get that data in the template. Thoughts? Search is performed by this.
# 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()
And the html looks like this
{% 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>{{ item }}</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>{{ item.title }}</h4>
Type: Document<br>
Publish Date: {{ item.created_at }}
</p>
{% endfor %}
{% elif k == 'image' %}
{% for item in v %}
<p>
<h4>{{ item.title }}</h4>
Type: Image<br>
Publish Date: {{ item.created_at }}
</p>
{% endfor %}
{% endif%}
{% endfor %}
{% endfor %}
{% if search_results.has_previous %}
Previous
{% endif %}
{% if search_results.has_next %}
Next
{% endif %}
{% elif search_query %}
No results found
{% endif %}
Django provides the url property to files and images, and wagtail has this also.
{{ item.url }}
Wagtail has a great support for the redirect to files and integrates django-sendfile as well. Both of these methods allow for image downloading.
https://docs.wagtail.io/en/stable/advanced_topics/images/image_serve_view.html
from wagtail.images.views.serve import ServeView
urlpatterns = [
...
re_path(r'^images/([^/]*)/(\d*)/([^/]*)/[^/]*$', ServeView.as_view(action='redirect'), name='wagtailimages_serve'),
]
** Edit below
Generating the image “as foo” to access individual properties - access the underlying image instance
{% image item original as item_img %}
{{ item_img.url }}
Related
I would like to implement custom pagination for my admin panel.
My url looks like the following:
http://localhost:8000/admin/items/?group_id=20
On this URL I do some work to filter the results using the parameter group_id (by overriding get_changelist method).
The page results are corrects, the problem is my pagination ending up like this http://localhost:8000/admin/items/?p=1 whereas I would like the URL to be http://localhost:8000/admin/items/?group_id=20&p=1 and keep the parameter.
Basically I want the same result as How to paginate Django with other get variables? but using Django admin.
How can I keep the parameter along with the pagination?
I've tried overriding pagination.html file but without any success.
Thank you.
Edit
I've tried overriding pagination.html but request.GET.items is still empty (even if my settings file is well configured)
{% load admin_list %}
{% load i18n %}
{% load content_extras %}
<p class="paginator">
{% if pagination_required %}
{% for i in page_range %}
{{ i }}
{% endfor %}
{% endif %}
{{ cl.result_count }} {% if cl.result_count == 1 %}{{ cl.opts.verbose_name }}{% else %}{{ cl.opts.verbose_name_plural }}{% endif %}
{% if show_all_url %}{% trans 'Show all' %}{% endif %}
{% if cl.formset and cl.result_count %}<input type="submit" name="_save" class="default" value="{% trans 'Save' %}">{% endif %}
</p>
FOUND A SOLUTION:
1/ Override changelist_view on admin.py and pass the extra data
def changelist_view(self, request, extra_context=""):
response = super(ItemAdmin, self).changelist_view(request, extra_context)
group_id = request.GET.get('group_id', None)
if group_id:
extra_context = {
'group_id': group_id,
}
response.context_data.update(extra_context)
return TemplateResponse(request, "admin/changelist.html", response.context_data)
2/ Create changelist.html file based on django admin template (copy/paste)
Add {% load content_extras %} at the top of the file (line 3)
Change line {% block pagination %}{% pagination cl %}{% endblock %} with {% block pagination %}{% custom_pagination cl %}{% endblock %}
3/ Create content_extras.py under templatetags folder and write custom_pagination function
from django import template
from django.contrib.admin.templatetags import admin_list
register = template.Library()
#register.inclusion_tag('admin/pagination.html', takes_context=True)
def custom_pagination(context, cl):
pagination = admin_list.pagination(cl)
if 'group_id' in context:
params = (('group_id', context['group_id']),)
pagination['params'] = params
return pagination
4/ Create pagination.html (same location as changelist.html)
{% load admin_list %}
{% load i18n %}
{% load content_extras %}
<p class="paginator">
{% if pagination_required %}
{% for i in page_range %}
{{ i }}
{% endfor %}
{% endif %}
{{ cl.result_count }} {% if cl.result_count == 1 %}{{ cl.opts.verbose_name }}{% else %}{{ cl.opts.verbose_name_plural }}{% endif %}
{% if show_all_url %}{% trans 'Show all' %}{% endif %}
{% if cl.formset and cl.result_count %}<input type="submit" name="_save" class="default" value="{% trans 'Save' %}">{% endif %}
</p>
If you activated django.template.context_processors.request in your settings, you can access parameters from your request directly from your template.
And then you can access the parameters in your templates directly. Something like:
href="?page={{ data.next_page_number }}{% for key, value in request.GET.items %}{% if key != 'page' %}&{{ key }}={{ value }}{% endif %}{% endfor %}"
Research (context_processors.request was moved from django.core to django.templates some time ago)
Template:
{% for code in group_codes %}
*_{{ code.build }}_*<br />
{% if test_info.test_type = 0 %}
{{ code.pre_testing_fail }}/{{ code.pre_testing_total }} failed pre-test<br />
{% else %}
{% for shelf in final_shelf_info %}
{{ shelf.build }} <br/>
{% if shelf.build = code.build %}
{{ mr_script_count_func }}/{{ code.script_total }}
<span>MR</span> failed during script<br />
{{gw_script_count_func}}/{{ code.script_total }}
<span>GW</span> failed during script<br />
{{ mr_post_count_func }}/{{ code.post_testing_total }}
MR failed during post-test<br/>
{{ gw_post_count_func }}/{{ code.post_testing_total }}
GW failed during post-test<br/>
{% endif %}
{% endfor %}
<br/>
<br/>
{% endif %}
{% endfor %}
View
def final_shelf_info(self):
shelves = self.bugs_stbs()
shelfList = list()
for shelf in shelves:
shelfList.append(shelf.results_stb_id)
final_info = ResultsStbs.objects.select_related(
'build',
'pre_testing_result',
'script_result',
'post_result',
).filter(
results_stb_id__in=shelfList,
tr_test_case_id=self.kwargs['trTestCaseID'],
).order_by(
'pair_no','shelf_no',
)
for info in final_info:
if info.stb_hw_info_ids:
info.stb_type = info.stb_hw_info_ids.stb_hw_info.stb_type
else:
info.stb_type = None
return final_info
I would like to get the first element in the for loop
{% for shelf in final_shelf_info %}
and compare with another data.
How can I get the first element in the first loop.
First element : Q004.01.55.01.55.19_9423
{{ shelf[0].build }} I tried like that, it did not work.
The output of the for loop:
1234.xx.xx.xx.xx.xx
Any helps would be appreciated.
{% for shelf in final_shelf_info %}
{% if forloop.first %}
Do something with {{ shelf }} since its the first item iterated
{% endif %}
{% endfor %}
More on the {% for %} template loop in the docs.
You could do something like this:
{% for t in things %}
{% if forloop.first %}
// do something
{% endif %}
// do stuff
{% if forloop.last or things.count == 1 %}
// do something
{% endif %}
{% endfor %}
More documentation is available at Django documentation
{% if final_shelf_info.0 == shelf %}
or
{% if final_shelf_info.first == shelf %}
I was adding pagination to my website tonight and I ran into issues where now I have broke my whole site.
I am using the standard Jekyll directory structure for the files. For my website, I want the index page to be about me, then you click the /blog page to view my blog. So I have index.md be by main page. Then I have a page blog.html be the blog page. I used to use {% for post in site.posts limit: 10 %} which worked and switched to {% for post in paginator.posts %} based on how the latest documentation recommended the loop and it broke. In my _config.yml file I have:
paginate: 10
paginate_path: "blog/page:num"
permalink: /blog/:year/:month/:day/:title.html
I want the paging to be /blog/page2.html. I am unsure what I have configured wrong. Here is my entire blog.html page:
---
title: Blog
layout: default
permalink: /blog/index.html
---
{% for post in paginator.posts %}
<div class="row">
<div class="col-lg-12">
{% if post.layout contains "link" %}
<h4><i class="icon-link"></i> {{post.title}}</h4>
{% else %}
<h4>{{post.title}}</h4>
{% endif %}
<small>{{post.date | date: "%m/%d/%Y"}}</small>
<div class="post-content-truncate">
{% if post.content contains "<!-- more -->" %}
{{ post.content | split:"<!-- more -->" | first % }}
Read full article...
{% else %}
{{ post.content }}
{% endif %}
</div>
</div>
</div>
{% endfor %}
{% if paginator.total_pages > 1 %}
<div class="row">
<div class="col-lg-12">
<ul class="pagination">
{% if paginator.previous_page %}
<li>« Prev</li>
{% else %}
<span>« Prev</span>
{% endif %}
{% for page in (1..paginator.total_pages) %}
{% if page == paginator.page %}
<li><em>{{ page }}</em></li>
{% elsif page == 1 %}
<li>{{ page }}</li>
{% else %}
<li>{{ page }}</li>
{% endif %}
{% endfor %}
{% if paginator.next_page %}
<li>Next »</li>
{% else %}
<li><span>Next »</span></li>
{% endif %}
</ul>
</div>
</div>
{% endif %}
Any ideas of where I am going wrong?
The problem was actually two problems:
I was using an older version of Jekyll. Once I upgraded to the latest it was almost working.
I had to put my /blog.html file into /blog/index.html
Once I did those two steps it worked.
I am using Flask, mongoengine for a project and I am trying to get basic stuff working from http://docs.mongodb.org/manual/tutorial/write-a-tumblelog-application-with-flask-mongoengine/
After implementing everything from above link I added a new field for "tags" in Post and when I try to create a post, my tags doesn't show a input box.
Any help is appreciated.
My code and screenshot below
class Post(db.DynamicDocument):
created_at = db.DateTimeField(default=datetime.datetime.now, required=True)
title = db.StringField(max_length=255, required=True)
slug = db.StringField(max_length=255, required=True)
comments = db.ListField(db.EmbeddedDocumentField('Comment'))
tags = db.ListField(db.StringField(max_length=30)) # New field I added
template form
{% macro render(form) -%}
<fieldset>
{% for field in form %}
{% if field.type in ['CSRFTokenField', 'HiddenField'] %}
{{ field() }}
{% else %}
<div class="clearfix {% if field.errors %}error{% endif %}">
{{ field.label }}
<div class="input">
{% if field.name == "body" %}
{{ field(rows=10, cols=40) }}
{% else %}
{{ field() }}
{% endif %}
{% if field.errors or field.help_text %}
<span class="help-inline">
{% if field.errors %}
{{ field.errors|join(' ') }}
{% else %}
{{ field.help_text }}
{% endif %}
</span>
{% endif %}
</div>
</div>
{% endif %}
{% endfor %}
</fieldset>
{% endmacro %}
rendering form code
{% extends "admin/base.html" %}
{% import "_forms.html" as forms %}
{% block content %}
<h2>
{% if create %}
Add new Post
{% else %}
Edit Post
{% endif %}
</h2>
<form action="?{{ request.query_string }}" method="post">
{{ forms.render(form) }}
<div class="actions">
<input type="submit" class="btn primary" value="save">
Cancel
</div>
</form>
{% endblock %}
From what I can gather, your problem is you're telling WTF to render the tags field, but WTForms doesn't know how to handle that information.
From looking at the Flask-MongoEngine documentation, it seems the ListField is just a FieldList as WTForms refers to it.
Currently you're not actually defining the form independently in WTForms, you're just using the magic included in Flask-MongoEngine, so my first attempt would be to add some more logic to your macro, add a {% elif field.type == 'ListField' %} and try and discover what's contained in there to iterate through to produce your form. From having a quick look at the source-code, something like the following might work.
{% elif field.type == 'ListField %}
{# render_the_group_label #}
{% for subfield in field.entries %}
{% if subfield.type == 'StringField' %}
{# render_the_subfield #}
{% endif %}
{% endfor %}
...
That code will need to be worked on, but hopefully it'll point you in the right direction. Otherwise, I'd actually define the form seperately in WTForms to give you a bit more control on the code-side. Luckily they provide a csv tag example which should help you if you need to go that route. I wrote a guide that takes a different route using #property decorators to achieve a similar effect, which again, might at least point you towards the finish line.
I am a Django newbie and am unable to achieve something trivial. Please help me with this.
I am setting a variable pgurl in my views.py
Am able to access the variable {{pgurl}} in my with_tag.html template. This template includes a pagination.html template into itself
In pagination.html I am unable to use the variable {{pgurl}} and nothing is printed
How can I get this variable passed into the included template?
views.py
def with_tag(request, tag, template_name='main/with_tag.html', current_page=1, pgurl=''):
if request.method == 'GET':
query_tag = Tag.objects.get(name=tag)
primes = TaggedItem.objects.get_by_model(Prime, query_tag)
primes = primes.order_by('-date')
request.page = current_page
tcm_pp = TCM_ITEMS_PER_PAGE
pgurl = request.path
else:
return HttpResponseRedirect(request.path)
return direct_to_template(request, template_name, { 'primes' : primes, 'prime_total' : Prime.objects.count(), 'now': datetime.now(), 'page' : current_page, 'tcm_pp' : tcm_pp, 'tag' : tag, 'pgurl' : pgurl })
with_tag.html
{% extends "base.html" %}
{% load comments %}
{% load pagination_tags %}
...
{% include "pagination.html" %}
{% paginate %}
pagination.html
{% if is_paginated %}
{% load i18n %}
<div class="pagination">
{% if page_obj.has_previous %}
‹‹ {% trans "previous" %}
{% else %}
<span class="disabled prev">‹‹ {% trans "previous" %}</span>
{% endif %}
{% for page in pages %}
{% if page %}
{% ifequal page page_obj.number %}
<span class="current page">{{ page }}</span>
{% else %}
{{ page }}
{% endifequal %}
{% else %}
...
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
{% trans "next" %} ››
{% else %}
<span class="disabled next">{% trans "next" %} ››</span>
{% endif %}
</div>
{% endif %}
It will be helpful if you post the output of the rendered page. The context should get passed, might be your template tags instead. Try to do assert and check to see if the variables were passed correctly.