Select One Column Using Peewee - python

I have selected one column by using the peewee, and then send it to the template. But nothing return.
I have one table named Entry, with column tag_name.
#app.route('/archive')
def tag():
query_tag = (Entry.select(Entry.tag_name)).distinct())
return object_list('t.html', query_tag, check_bounds=False)
The corresponding template:
{%block content %}
{% for tag in object_list %}
<p>{{ tag }}</p>
{% endfor %}
{% endblock %}
And finally it display "None"
result_photo
But if I change to below code, it can work,:
#app.route('/archive')
def tag():
query_tag = (Entry.select().distinct())
return object_list('t.html', query_tag, check_bounds=False)
And the template:
{%block content %}
{% for tag in object_list %}
<p>{{ tag.tag_name }}</p>
{% endfor %}
{% endblock %}

You can combine your two examples and the following should work:
query_tag = Entry.select(Entry.tag_name).distinct()
And the template:
{% for entry in object_list %}
<p>{{ entry.tag_name }}</p>
{% endfor %}
Because, even though you've only selected one column, Peewee will still be returning Entry objects. The Entry objects will only have the "tag_name" field populated, though.

Related

Django: Nested Loop In Template

category = ['cat1','cat2','cat3']
inventory = CurrentInventory.objects.all()
for cats in categories
inventorybycat = inventory.filter(category =cats)
setofinventories.append(inventorybycat)
dictofstuff = [zip(setofinventories,categories)]
context = {
'setofinventories':setofinventories
'category':category
'dictofstuff':dictofstuff
}
In views.py above this loop creates a list of objects per each category.
In the template below this loop prints a filtered object list per every item in the category list.
{% for inventory in setofinventories%}
{% for item in inventory %}
{{ item.category }}
{{ item.productName }}
{% endfor %}
{% endfor %}
The only thing I am missing is I do not now how to reference the category in the template. I pass the whole list in context, but {{category{{forloop.counter}}}} is not a valid statement.
I would either like to use zip(category,setofinventories) to pass these two items together,
or create a category model, filter by that model and then I can reference that model by item?
If I zip these items dictofstuff = [zip(setofinventories,categories)]
How do I reference the category in the template?
{% for inventory,categories in dictofstuff %}
{% for inventory in setofinventories%}
{% for item in inventory %}
{{ item.quantity }}
{{ item.productName }}
{% endfor %}
{% endfor %}
{% endfor %}
Returns Error: "Need 2 values to unpack in for loop: got 1."
If i right understand you, you want to print category only one time.
In your case:
{% for inventory in setofinventories%}
{% ifchanged inventory.category %}
{{ inventory.category }}
{% endifchanged %}
{% for item in inventory %}
{{ item.quantity }}
{{ item.productName }}
{% endfor %}
{% endfor %}
i use ifchanged, more here:
https://docs.djangoproject.com/en/4.1/ref/templates/builtins/#ifchanged
in views i zipped this to a *listofstuff. Forgive the nomenclature.
dictofstuff = [zip(categories,setofinventories)]
in template. Again, forgive bad nomenclature. I should go back and rename these but it works.
{% for category in dictofstuff %}
{% for item in category %}
{{item.0}}
{% for stuff in item %}
{% for thing in stuff %}
{{thing.productId}}{{thing.productName}}
{% endfor %}
{% endfor %}
{% endfor %}
{% endfor %}

Django, variable {{ }} inside {% %}

I'm using Django-instagram for simply display Instagram content on my webpage:
{% load instagram_client %}
{% instagram_user_recent_media intel %}
This example works perfect, but the problem is Instagram name changes depending on selected page. I pass it via context:
def about(request, foo_id):
article = get_object_or_404(Bar, id=foo_id)
return render_to_response('about.html', {'foo' : article})
It works fine if I'm using it without template:
<p>{{ foo.instagram }}</p> --> returns valid name
How can I pass my "foo.instagram" like this:
{% load instagram_client %}
{% instagram_user_recent_media {{ foo.instagram }} %}
You can use set:
{% set new_var = foo.instagram -%}
{% load instagram_client %}
{% instagram_user_recent_media new_var %}

Django assignment_tag conditional

I'm trying to show partials based on a simple condition. My condition is whether an assignment_tag is True or False.
Templatetag:
from django import template
register = template.Library()
#register.assignment_tag
def partner():
return False
Template:
{% load partner_check %}
{% if partner %}
{% block header %}
{% include 'includes/partner_header.djhtml' %}
{% endblock header %}
{% block footer %}
{% include 'includes/partner_footer.djhtml' %}
{% endblock footer %}
{% endif %}
No matter what I set partner to, the blocks still appear. What am I missing?
Firstly, that's not how assignment tags work. You have never actually called the tag; if partner refers to a (non-existent) template variable named "partner". You call an assignment tag by using it on its own along with a variable to assign it to:
{% partner as partner_value %}
{% if partner_value %}...{% endif %}
Secondly, that's not how blocks work either. You can't dynamically define blocks; they are part of the basic structure of a template, not something that is assigned during evaluation.
I accomplished this by using a context_processor (https://docs.djangoproject.com/en/1.7/ref/settings/#std:setting-TEMPLATE_CONTEXT_PROCESSORS)
Context Processor:
def partners(context):
return {
'partner': False
}
Template:
{% block header %}
{% if partner %}
{% include 'includes/partner_header.djhtml' %}
{% else %}
{{ block.super }}
{% endif %}
{% endblock header %}
{% block footer %}
{% if partner %}
{% include 'includes/partner_footer.djhtml' %}
{% else %}
{{ block.super }}
{% endif %}
{% endblock footer %}

Insert separator only if didn’t insert one before

I have a template variable product_list, which is a QuerySet of Product objects; Product objects, in turn, have a one-to-many field of Track objects (reverse mapped from Track, of course), which is possible to be empty. I want to create a list of Tracks, grouped by Products like this:
{% for product in product_list %}
{% if this is not the first product with tracks %}
<li class="separator"></li>
{% endif %}
{% for track in product.tracks %}
<li>{{track}}</li>
{% endfor %}
{% endfor %}
The question is, what should I write of if this is not the first product with tracks? I tried ifchanged product but it inserts a separator even on the first iteration (as it changes from "" to "someproduct"). forloop.counter is also not usable here, as it is possible that the first two products won’t have tracks.
One workaround could be to change product_list to track_list like this:
track_list = Track.objects.order_by('product__name')
so I can indeed use ifchanged. It is doable in my case, but I’m still interested in a solution for my first method.
You should compose the condition. It looks simple, perhaps is not this that your are asking for.
{% for product in product_list %}
{% if not forloop.first and product.tracks %}
<li class="separator"></li>
{% endif %}
{% for track in product.tracks %}
<li>{{track}}</li>
{% endfor %}
{% endfor %}
If this is not a solution for you, I suggest to you to cook data on view and send ready to be parse to template, more easy.
{% for product in product_list %}
{% if product.should_have_separator %}
<li class="separator"></li>
{% endif %}
{% for track in product.tracks %}
<li>{{track}}</li>
{% endfor %}
{% endfor %}
In your view append should_have_separator field dynamically to products should have it:
product_list = Product.objects.....
is_the_first_product = True
for product in product_list:
is_the_first_product_with_tracks = ( is_the_first_product
and bool( product.tracks ) )
product.should_have_separator = not is_the_first_product_with_tracks
is_the_first_product = False

'for' loop through form fields and excluding one of the fields with 'if'

The problem I'm struggling with is as follows:
I have:
{% for field in form %}
{{ field }}
{% end for %}
What I want is to put an 'if' statement to exclude a field which .label or whatever is provided. Like:
{% for field in form%}
{% if field == title %}
{% else %}
{{ field }}
{% endif %}
{% endfor %}
Is it possible? I have to many fields to write them one by one and only one or two to exclude.
Thank you for any tips.
BR,
Czlowiekwidmo.
Yes, this should be possible:
{% for field in form %}
{% ifnotequal field.label title %}
{{ field }}
{% endifnotequal %}
{% endfor %}
Django's template tags offer ifequal and ifnotequal variants, and you can test the field.label against either a context variable, or a string.
You might be a lot happier creating a subclass of the form, excluding the offending field.
See http://docs.djangoproject.com/en/dev/topics/forms/modelforms/#form-inheritance
class SmallerForm( MyForm ):
class Meta(MyForm.Meta):
exclude = [ title ]
{% for field in form %}
{% if field.name != 'field_name' %}
{{ field }}
{% endif %}
{% endfor %}

Categories