Selecting specific categories to display on the homepage and limiting the number of images shown

<<html>


<head>
    <meta charset='utf-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <title>Gallery</title>

    <!-- CSS only -->
    <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e2808d8d969196908392a2d7cd0ccd3">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">

    <style>
        .image-thumbnail {
            height: 200px;
            object-fit: cover;

        }

        .container {
            margin-left: 1;

        }

        .h1 {
            text-align: center;

        }

        .list-group-item a {
            text-decoration: none;
            color: black;
        }
    </style>
</head>

<h1 class="text-center m-5 p-3 mb-2 bg-light text-secondary">CHARLIES LIFE</h1>


<body class="m-5">
    <div class="container">


        <div class="row">
            <div class="col-md-3 mt-2">

                <div class="card">
                    <div class="card-header">
                        Categories
                    </div>
                    <ul class="list-group list-group-flush">
                        <li class="list-group-item">
                            <a href="{% url 'gallery' %}">All</a>
                        </li>
                        {% for category in categories %}
                        <li class="list-group-item">
                            <a href="{% url 'gallery' %}?category={{category.name}}">{{category.name}}
                            </a>
                        </li>
                        {% endfor %}
                        <a href="{% url 'add' %}" class="btn btn-dark btn-block m-1" type="button""> Add Photo</a>
                    </ul>
                </div>
            </div>


            <div class=" col-md-9">
                            <div class="row">

                                {% for photo in photos %}
                                <div class="col-md-4">
                                    <div class="card my-2">
                                        <img class="image-thumbnail" src="{{photo.image.url}}" class="card-img-cap"
                                            alt="...">
                                        <div class="card-body">
                                            <p class="card-text text-center">{{photo.category.name}}</p>
                                        </div>
                                        <div class="d-grid gap-2">
                                            <a href="{% url 'photo' photo.id %}" class="btn btn-dark m-1"
                                                type="button"> View</a>
                                        </div>
                                    </div>
                                </div>
                                {% empty %}
                                <h3> No Photos...</h3>
                                {% endfor %}


</body>

</html>

Hi, I'm currently building a photo blog to document the journey of my son as he grows. The code above represents the "home" page named gallery.

Currently, I have a loop that adds a "card" class to my gallery page each time I upload an image.

I would like to modify it so that it displays only one picture from each category I create, rather than showing every image uploaded to the site. I didn't anticipate the number of photos I would upload, and I don't want the home page to become overloaded with images.

If anyone has any suggestions on how I can achieve this or any other recommendations, I would greatly appreciate it.

Thank you in advance.

Answer №1

Allow me to clarify, it seems that you are aiming to categorize images into different sections such as 'young', 'teenager', and 'adult'. You want to display only the images corresponding to each section with the section name tagged in the image itself. To achieve this, you can use the following code:

{% for photo in photos %}
    {% if photo.tag == young %}
        <h1>Show young images here</h1>
    {% elif photo.tag == teenager %}
        <h1>Show teenage images here</h1>
    {% elif photo.tag == adult %}
        <h1>Show adult images here</h1>
    {% else %}
        <h1>No photo's yet</h1>
    {% endif %}
{% endfor %}

Alternatively, you can handle this logic in your views by serving different images to various templates. Here is an example of how to do that:

def teenageImages(request):
    teenageImage = Image.objects.raw("SELECT * FROM 
    photos WHERE tag = 'teenager' ")
    template = loader.get_template('teenager_photos.html')
    context = {
        'photos': teenageImage
    }
    return HttpResponse(template.render(context, request))

Another approach would be to utilize filters. More information on using filters can be found here.


For bootstrap cards, you can refer to this Github Repo showcasing bootstrap cards with images (no link provided): my webpage repo

Feel free to explore this repository for inspiration and ideas on implementing similar features.

Additionally, consider using Wagtail for a more streamlined approach. I have created a detailed Github repository related to Wagtail which can be accessed here.

Answer №2

If you're looking to enhance your home page functionality, consider implementing the RoutablePageMixin from Wagtail CMS (built on Django). For more information, check out the official documentation. This will allow your home page to accept categories as variables, similar to URLs like davidsonsblog.com/category/foobar

You can then create a queryset that is filtered by category and sorted randomly using the method .order_by('?'). If you only want to display one image, utilize the .first() method in the queryset.

The menu will automatically generate links based on these settings.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Capturing URL parameters and their values in Django urlpatterns

I am currently trying to extract the variable and value from a URL in urlpatterns in Django. Specifically, I want to input a URL in the browser address bar like: , in order to create a translator. I need to retrieve the variable and value in the function t ...

Utilizing Cache Features in the XDK Application Framework API

I recently posted a question in the Intel Developer Zone forum for XDK, but unfortunately have not received any answers yet. So I decided to seek help here: My project involves creating a simple HTML5-only periodical website that will be packaged as a sta ...

What is the best way to connect or link to a HTML table row <tr>

Is there a way to trigger an alert when the user double clicks on the whole row? ...

Angular Update Component on Input ChangeEnsuring that the component is automatically

<div class=" card-body"> <div class="row"> <div class=" font-icon-list col-lg-2 col-md-3 col-sm-4 col-xs-6 col-xs-6" routerLinkActive="active" *ngFor="let subject of subjects"> <div class=" fon ...

Creating a line with CSS is a straightforward process that involves using properties

I am trying to achieve a yellow line effect similar to the image shown using CSS. I have managed to create line holes so far, but I'm struggling with creating vertical straight lines. Here is an example of my current code: https://i.sstatic.net/MqRUE ...

Increasing the checkout date by one day: A step-by-step guide

I am looking to extend the checkout period by adding 1 more day, ensuring that the end date is always greater than the start date. Below are my custom codes for implementing the bootstrap datepicker: $(function() { $('#datetimepicker1').da ...

Encountering issue when deploying multiple HTML files using Parcel

Whenever I deploy a website with multiple entry points and many HTML files, and the host uses the build command: parcel build index.html aboutme.html, the deployed website gives me a 404 error. However, if I manually enter /aboutme.html or /index.html in t ...

Navigate to a different page using a sliding transition

Currently, I am working on a project using jquery-mobile where I need to redirect to another page after clicking on an image with a delay. This is what I have done so far: setTimeout(function(){ window.location.href = "myPage.html"; }, 1000); While t ...

Eliminating unnecessary gaps caused by CSS float properties

I need help figuring out how to eliminate the extra space above 'Smart Filter' in the div id='container_sidebar'. You can view an example of this issue on fiddle http://jsfiddle.net/XHPtc/ It seems that if I remove the float: right pro ...

Optimizing CSS across various pages and screens sizes with critical importance

My CSS file is in need of optimization for quicker loading times. It contains numerous components utilized across various pages (such as the start page, category pages, and detail pages) on different screen sizes (mobile, tablet, desktop). Manual optimizat ...

Clicking on a button within the parent element will enable you to remove the parent element multiple times with the use of VanillaJS

Within a ul element, each li contains multiple elements in the following structure: <ul> <li> <div> <p>some text </p> <button>delete</button> <div> </li> <li> ...

Utilize a standard PHP script for every subdirectory within a domain

I'm currently working on a website and the design I'm using involves changing the URL in the address bar using the history API when the page content changes through JavaScript and AJAX. However, I also want each "page" of the website to be access ...

Seeking help with a problem regarding the Tooltip Effect not displaying over a button

I'm currently struggling with implementing a tooltip for the "Back-end" button on my webpage. Despite my efforts, the tooltip effect fails to display over the button, and I'm at a loss as to why. Below is the code snippet I am working with: < ...

Adjusting the starting position of text within an HTML <li> element

I am currently in the process of designing a webpage layout with three columns. I have a specific vision for how I want the text to align within these columns but I am unsure of how to achieve it. Is there a way to make the text start at an exact position, ...

Always take the lead with the first image in navigation

Looking to add an image to your website navigation? Want the image to appear at the beginning of the navigation bar, before Link 1? Here's a bit of CSS code for you: <div class="topnav"> <img src="https://cdn.mos.cms.futurecdn ...

CSS navigation menu with drop-down functionality

I am currently learning CSS and HTML as I work on the CCTCcart project. When running the code in the bottom menu section, I noticed that when clicking on any menu item such as Products, the white background color merges with blue. I need to find a solution ...

What is the process for adjusting the switch size in react-bootstrap?

I've been struggling to increase the size of the react-bootstrap switch without success. I searched through the documentation and tried various methods, but couldn't find a solution other than directly modifying the bootstrap css. Here's the ...

Struggling to locate the CSS needed to perfectly center my Wordpress lightboxes

There seems to be an issue with the alignment of images on my website when opened in a lightbox (using the Lightbox Plus Colorbox plugin on Wordpress). Instead of appearing in the center, the images stubbornly show up at the top left-hand side of the scree ...

The functionality of the button seems to be malfunctioning specifically in the Mac Firefox

My button isn't working in Mac Firefox only. Below is the code: <button class="col btn-change"><a href="/p/88" style="color: white;">Landscape</a></button> However, the following two codes are functioning correctly. In the fi ...

The interaction between jQuery Masonry and Bootstrap results in unexpected grid behavior

I've spent hours trying to solve this problem, but I can't seem to get it to work as intended. I want the layout of my boxes to be like this using Bootstrap grids: However, after implementing the jQuery Masonry plugin (which I used to manage va ...