'Passing a context variable through a class in Django

how can i pass a context variable in a class. i know that i would use the render if i was showing my template from a function. and then i could just pass my context variable as part of the render. But how do i pass a context variable to html if i am using a class to show the template.

i have tried putting a function into my class but it has not worked.

views.py

class hithere(ListView):
model = Datadata
template_name = 'index.html'

def whatsup(request):
    context = {}
    context['my_string'] = "this is my sring"
    return render(request, context)

Index.html

<h1>  {{ my_string }} </h1>


Solution 1:[1]

You need to remove "show2" when you click the first button and change it so instead of using toggle, use add and remove.

let element2 = document.getElementById("button3");

element2.addEventListener("click", () => {
    let cat_result = document.getElementById("cat_result");
    
    cat_result.classList.remove("show2");
    cat_result.classList.add('show3');

    fetch('https://aws.random.cat/meow')
    .then(res => res.json())
    .then(data => {
        cat_result.innerHTML = `<img src="${data.file}"/>`
    })
    
});

let element1 = document.getElementById("button1");

element1.addEventListener("click", () => {
        let cat_result = document.getElementById("cat_result");
        cat_result.classList.add("show2");
        cat_result.classList.remove('show3');
        
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1