BigCartel : {{ category.name }} and {{ artist.name }} display per item on Products page - bigcartel

I'm editing a theme in BigCartel and would like to display both the category name and the artists name underneath each product on the general 'Products' page of my BigCartel store. I have the 'Artists' category turned on (and have assigned each product to a category and an artist) and have used the code below but the names are still not displaying...They only show up when I search the store by category or Artist. (I've included 'Category' and 'X Artists Name' for styling only and will remove them once the issue is resolved.)
Any help on this would be greatly appreciated!
[Updated 03.12.2015]
The code below works fine when viewing a specified individual category or artist, however I would like to display both the Category and Artist name on the general Products page.
<span class="meta-category-name">Category {{ category.name }}</span>
{% if artists.active != blank %}
<span class="meta-artist-name">X Artists Name {{ artist.name }}</span>
{% endif %}

Ok I worked it out - For anyone elses benefit the code below solved it.
{% for category in product.categories %}
<span class="meta-category-name">{{ category.name }}</span>
{% endfor %}
{% for artist in product.artists %}
{% if artists.active != blank %}
<span class="meta-artist-name">{{ artist.name }}</span>
{% endif %}
{% endfor %}
</div>

Related

Is there a way in Django to load a template in 'sections' or 'chunks' a sort of pagination as you scoll?

The reason I ask is that I'm parsing a large List into a Django template, we're talking about 100,000 items being rendered into a table and it's painfully slow at loading!
or is there a method that I've completely missed?
I'm currently using {% regroup %} to group headers together.
with the Paginator class
from django.views.generic import ListView
from myapp.models import Contact
class ContactList(ListView):
paginate_by = 2
model = Contact
and the template
{% for contact in page_obj %}
{# Each "contact" is a Contact model object. #}
{{ contact.full_name|upper }}<br>
...
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if page_obj.has_previous %}
« first
previous
{% endif %}
<span class="current">
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
next
last »
{% endif %}
</span>
</div>
https://docs.djangoproject.com/en/3.1/topics/pagination/

How to keep 2 fields using django forms

As django forms run a loop for the fields
{% for field in form %}
<div class="col-6">
{{ field.errors }}
{{ field.label_tag }}
{{ field }}
</div>
{% endfor %}
I actually want to have 2 fields, I found a cheap solution to this issue using Bootstrap4
I created the columns by using col-6 by which I get 2 fields in the row. But what if I want to make custom designs of forms in django ?
Personally i am using this method to show only fields i want and then have if statements for the way each field should be shown
{% for i in form %}
{% if i.name in 'title,address,city' (Fields you want to show) %}
{% for error in i.errors %}
<div>
<li><strong>{{ error|escape }}</strong></li>
</div>
{% endfor %}
<p>
<label>{{i.label}}:</label>
<input type="text" name="{{i.name}}" required id="id_{{i.name}}">
</p>
{% endif %}
{% endfor %}
And like that you can style it however you want and show only those you want to show

Display column names in Django template without knowing them ahead of time

I have a queryset that I am passing to Django. There are 100 columns or so. I want to render this single object as either an HTML list without knowing all the column names. I know I could use ListView, but I want more control to do something like this within a TemplateView (with additional graphs):
<ul>
{% for obj in queryset %}
<li>
{% for column in obj %}
[Name of Column]: {{ column }}
{% endfor %}
</li>
{% endfor %}
</ul>
So I want to render both the name of the column and the column data without knowing the name of the column beforehand. Ordinary I'd do this:
<ul>
{% for obj in queryset %}
<li>
Column 1: {{ obj.column1 }}
Column 2: {{ obj.column2 }}
...
Column 100: {{ obj.column100 }}
</li>
{% endfor %}
</ul>
But this is a bit tedious and requires writing out each column name by hand.
Could you try this
<ul>
{% for obj in queryset %}
<li>
{% for column in obj.column_set.all %}
[Name of Column]: {{ column }}
{% endfor %}
</li>
{% endfor %}
</ul>

ListField is showing <ul> instead of <input> in edit/create post

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.

How do I make nested regroups in Django?

I've got the following situation in this system:
Each category of products has many subcategories, and each subcategory has many products under it.
I'm trying to make a product searh, which returns a list, and in my template, I show an overview of the results, like this:
Cellphones
Dumbphones (2 results)
Smartphones (3 results)
Monitors
CRT (1 result)
LCD (3 results)
I'm my template I have only the list of products. I've tryed many combinations of nested regroups, without success. Any ideas?
You can try something like this:
<div>
...
{% regroup results|dictsort:"subcategory.category" by subcategory.category as categories %}
<ul>
{% for category in categories %}
<li>{{ category.grouper }}
{% regroup category.list|dictsort:"subcategory" by subcategory as subcategories %}
<ul>
{% for subcategory in subcategories %}
<li>{{ subcategory.grouper }}
<!--The same way you can render a subcategory.list which is the prosucts list-->
</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
acceptance_report.company as companies %}
...
</div>