ive a problem. in my template i want to click on parts of ingredients to select them by changing their id and when I'm clicking on a button I want the page to show all reciepes where the ingredients are in. Everything works for me. I can select ingredients and send the ajax request to my views.py and select all the reciepes with those ingredients and here comes my problem.
I am not able to reload the page without lopsing my context data from the ajax post request I hope i could discribe my problem
here ist the part of my views.py:
def what_to_cook(request):
recipes = Recipe.objects.all()
ingredients=Ingredients.objects.order_by().values('ingredient_name').distinct()
context = {'recipes':recipes, 'ingredients':ingredients}
if request.is_ajax():
tasks = request.POST.getlist('marked_ingredients[]')
selected = Ingredients.objects.filter(ingredient_name__in=tasks)
print(selected)
context = {'recipes':recipes, 'ingredients':ingredients,'tasks':tasks, 'selected':selected}
return render(request, "/cooking/what_to_cook.html", context)
else:
return render(request, "cooking/what_to_cook.html", context)
And here is my template:
{% extends 'main/base.html' %}
{% load main_tags %}
{% block head %}
{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'cooking.css' %}">
{% endblock %}
{% block title %}Kochen?!{% endblock %}
{% block subtitle %}Was koche ich heute?{% endblock %}
{% block content %}
<input type="text" id="myInput" onkeyup="search()" placeholder="Suche Zutaten ..." title="Type in a name">
<ol id="Zutaten">
{% for ingredient in ingredients|dictsort:"ingredient_name" %}
<li class="element" id="not_marked" onclick="done(this)">{{ingredient.ingredient_name}}</li>
{% endfor %}
</ol>
<button onclick="build_array()">Gespannt auf den Vorschlag?</button>
{% for part in selected %}
{{part}}
{% endfor %}
<p>{{selected}}</p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script>
function done(value) {
console.log(value.id);
if (value.id == "is_marked") {
value.setAttribute("id", "not_marked");
value.style.color="black";
} else {
value.setAttribute("id", "is_marked");
value.style.color="green";
}
}
function build_array() {
var $marked_ingredients = document.querySelectorAll("[id='is_marked']");
var i;
marked_ingredients = []
for (i = 0; i < $marked_ingredients.length; i++) {
marked_ingredients.push($marked_ingredients[i].innerText);
}
console.log(marked_ingredients)
var csrftoken = $("[name=csrfmiddlewaretoken]").val();
$.ajax({
type:'POST',
url:'/cooking/what_to_cook',
data: {'csrfmiddlewaretoken': '{{csrf_token}}',
'marked_ingredients[]':marked_ingredients}
});
}
function search() {
var input, filter, ul, li, a, i, txtValue;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
ul = document.getElementById("Zutaten");
li = ul.getElementsByClassName("element")
for (i = 0; i < li.length; i++) {
txtValue = li[i].textContent || li[i].innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
</script>
{% endblock %}
UPDATE I got it to work with this kinde of code
TableItem +='<tr>' +
'<td><a href="/cooking/'+value.recipe_id+'">'+value.recipe_id+'</a></td>'+
'<td>'+value.recipe_id+'</td>'+
'<td>'+value.recipe_id+'</td>'+
'</tr>'
$('#myTable').html(TableItem);
But is there a possibility to include this into a string to display it in the template above
{% for recipe in recipes %}
{% if recipe.id == value.recipe_id %}
{{recipe.title}}
{% endif %}
{% endfor %}