Display a list of shop categories on Big Cartel shop landing - templates

I'm using the Luna theme of Big Cartel.
My shop sits on /products and clicking on a shop category links to /category/tees or /category/accessories etc.
The /products page currently shows a list of all products in the store. I want to change that to show a list of categories
Is there a way to check if I'm on a category page or on the shop landing page?
I also tried the following but it doesn't seem to work
{% if page.url == '/products' %}
// Show categories grid
{% else %}
// Show category products
{% endif %}
Any help would be appeciated

Use the page.full_url variable with contains for best results:
{% if page.full_url contains '/category' %}
// Show category products
{% else %}
// Show categories grid
{% endif %}

Related

Django - for each field value

I have a question.
In my view, I return a dataset which includes:
Category
Product
Price
I want to say something like the below, where I show UI elements related to a category as a whole.
is it possible?
{% for item.category in products %}
Not directly. You would do
{% for item in products %}
...
{{ item.category}}
Or if item.category is itself an iterable you can nest loops. If it's the set of related objects you need a .all (c.f. python obj.foo_set.all() )
{% for item in products %}
...
{% for category in item.category.all %}
{% for item in products %}
{{item.Category}}
{{item.Product}}
{{item.Price}}
{% endfor %}

How to display {% django block %} dynamically with for loop?

Hello Awesome People!
I'm building a dashboard for my website, in the left section, I have a menu containing multiple items : Home, Messenger, Job Offers ....
When creating an account, Users are able to choose their items, re-order or remove.
I have Menu & Item models,
class Menu(models.Model):
items = models.ManyToManyField('Item',blank=True)
class Item(models.Model):
name = models.CharField(max_length=100)
unique_key = models.CharField(max_length=100,unique=True)
url = ......
For the menu of the user, in my base_template I display the items of the current users with for loop. With a CSS class I want to highlight the current view, the current item.
When the user visits his messenger, I want to highlight that item messenger with a CSS class.
<ul>
{% for item in user.menu.items.all %}
<li class='{% block item.unique_key %}{% endblock %}'>
{{item.name}}
</li>
{% endfor %}
</ul>
Now, when being in messenger.html for example, knowing that I have a unique_key for messenger called item_messenger, I do
{% block 'item_messenger' %}active{% endfor%}
It's not working, the item Messenger or whatever I choose doesn't become highlighted, I wonder why?
Is there another way to achieve that?
Any hint will be helpful, thanks in advance!
You are trying to have the block tag interpret a variable as it's name. That doesn't work.
You will need to pass along the active item's key in the template context and then in your base_template you can just do a simple if statement to add the active class to the correct entry.
<ul>
{% for item in user.menu.items.all %}
<li class='{% if item.unique_key == active_key %}active{% endif %}'>
{{item.name}}
</li>
{% endfor %}
</ul>

Wagtail / Django inbuilt main menu

Started a project working with Wagtail CMS and what I understood of their principal of pages is that it is hierarchical leading me to the conclusion that pages are either parent/child related or siblings.
So how I have done it is have my main page landing and all other pages would be children of this page.
Here is my structure
Home page
/ | \
News About us Events
/ | \ / | \
n_item n_item n_item e_item e_item e_item
So they are 6 types of pages
Home page * (unique)
News * (unique)
n_item (News Item)
About Us * (unique)
Events * (unique)
e_item (Event Item)
I then ticked the "Show in menus:" in the promote tab for the starred pages (*) in the above list.
The n_item and e_item will allow the editor (non technical) person to add as many of these pages as they wanted as they will be listed as part of the contents in the "News" and "Events" pages respectively.
Ideally the editor should not create siblings to Home Page,News, About us and events. Is it also possible to limit wagtails functionality of where editors can add pages if they are not super users?
But there doesn't seem to be a clear simple way (maybe a tag) to generate my menu. This is what I have found in my research.
page url and slug url which i cant get to seem to work in every template that is not being referenced directly by that pages template. It would also mean its not that dynamic
django-simple-menu doesn't seem dynamic to me as i have to manual link pages in code. Plus if this is the way wagtail intended to do it, it seems like a waste of maintaining page hierarchy internally.
List item seems to have been deprecated but even if it wasn't it seems cumbersome for the same reasons as 1 and 2
I wanted to have automatically generated menu too but only first level with home. I might did something naive but it works. I am using Wagtail 2.5.1 and Django 2.2.2.
My Wagtail page structure look like this:
Home page
/ \
Blog About
/ | \
n_item n_item n_item
I am using bootstrap4 so this is my nav in base.html template:
{% with root_p=request.site.root_page %}
{# this is static item for home #}
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link {% if '/' == request.path %} active {% endif %}" href="{{root_p.url}}">{{root_p.slug}}</a>
</li>
{# Here I am looping home's childrens #}
{% for nav in root_p.get_children.live %}
{% if nav.show_in_menus %}
<li class="nav-item">
<a class="nav-link {% if nav.slug in request.path %} active {% endif %}" href="{{nav.url}}">{{nav.slug}}</a>
</li>
{% endif %}
{% endfor %}
</ul>
{% endwith %}
This is result:
Now I am able to change slug, tick/untick "show in menus" and my bootstrap menu reflect these changes.
EDIT:
My last answer does not solve custom order of menu items. We can order items by standard fields in Page model "first_published_at" or alphabetically by "title" name. But I needed make it custom. So I added custom integer field in each my page model. Then wagtail users can change order of menu items using "promote tab".
My current page structure look like this:
Home
/ | \
Case studies Services Contact
My page model: (example of services only)
class ServicesPage(Page):
template = "home/services.html"
max_count = 1
subpage_types = []
menu_order = models.IntegerField(default = 0, help_text = "Setup custom menu order")
promote_panels = Page.promote_panels + [
FieldPanel('menu_order'),
]
class Meta:
verbose_name = "Services"
verbose_name_plural = "Services"
This is how it looks in promote tab of each page model
Last step is edit navbar loop in base template:
{% with root_p=request.site.root_page %}
{% for nav in root_p.get_children.specific.live|dictsort:"menu_order" %}
{% if nav.show_in_menus %}
<li class="nav-item {% if nav.slug in request.path %} active {% endif %}">
<a class="nav-link" href="{{nav.url}}">{{nav.title}}</a>
</li>
{% endif %}
{% endfor %}
{% endwith %}
For ordering I am using default django template filter dictsort.
I guess it is not best way how to do that, but it works.
Have you seen https://github.com/rkhleics/wagtailmenus? It's designed to "manage and render multi-level navigation and simple flat menus in a consistent, flexible way".

django how to display a tag only if the value coming from the database is not null?

The logic for showing a tag
{% if profile.facebook is not None %}<i class="facebook icon"></i>{% endif %}
i want to show the facebook icon only when the user has provided the link. if not i dont want to display the icon.
You can define your conditional as well:
{% if profile.facebook %}
<i class="facebook icon"></i>
{% endif %}
if your code not work, you can share more info, like your views.py etc.

How to remove a page link from Navigation menu big cartel

I'm using the Luna Theme on Big Cartel. I've added a static 'Thank you' page for signing up to the newsletter. However the link appears in the footer navigation menu. How do I remove the link from the menu?
To remove a specific page from the footer navigation in the Luna theme, you can head to Customize Design > Advanced > Layout in the admin, and click the "Custom" button to enable code editing.
Scroll down almost all the way to the bottom, and you'll see this block of code:
{% for page in pages.all %}
<li>{{ page | link_to }}</li>
{% endfor %}
To exclude a specific page, you can modify that block - if your page is titled "Thank You", this code will only display every other page:
{% for page in pages.all %}
{% if page.name != 'Thank You' %}<li>{{ page | link_to }}</li>{% endif %}
{% endfor %}