I want to include mangaid variable in "{% url 'manga' mangaid %}". Neither ${mangaid} nor mangaid works. How can I do this?
let input = document.querySelector("#search")
input.addEventListener("keyup", () => {
document.querySelector("#results").innerHTML = "";
let value = (input.value)
fetch(`searchresponse/${value}`).then((response) => response.json()).then((results) => {
results.forEach(function (result) {
let mangaid = (result["id"])
document.querySelector("#results").insertAdjacentHTML('beforeend', `<a class="result" id="result${mangaid}">${result["title"]}</a>`);
document.getElementById("result"+mangaid).addEventListener("click", (event) => {
event.target.href = "{% url 'manga' mangaid %}"
})
})
})
})
You can do this by using get params f.e. http://localhost:8000/manga/?a=1.
In the view you can access get params via request.GET.get('a'), in this case the code would return 1 from a=1 in the url.
In JS with djangos url templatetag you can do fetch("{% url 'manga' %}/mangaid=" mangaid)....
Django will replace {% url 'manga' %} with the url named manga.
mangaid needs to be a js variable.
Your view head should look similiar to this:
def mangaView(request):
mangaid = request.Get.get('mangaid')
print(mangaid)
EDIT:
Url should have trailing / to prevent redirects (301), this reduces server load (important in prod) http://localhost:8000/abc/?a=1.