Home > Back-end >  Django - button redirecting to {% url 'index' %}
Django - button redirecting to {% url 'index' %}

Time:08-22

I can't find any solution on any article so I'm asking here. I'd like to make button which is gonna redirect user to specific url. I have already did it this way:

<button onclick="location.href='create_recipe/'" type="button" >Create new Recipe</button>

but instead of passing whole link I'd like to use {% url 'some_view' %} but I do not have an idea how I should do that. Is it even possible to do that ?

It has to be <button>,

edit:

something like:

<button type="button" ><a href="{% url 'index' %}">Create new Recipe</a></button>

also does not work

CodePudding user response:

You can do this by adding this to the button:

onclick="goToSomeView()"

And then this to one of your JS files, as stated below by Sunderam Dubey:


function goToSomeView(){
    document.location.href = "{% url 'some_view' %}"
}

CodePudding user response:

index is your function which exist views.py file.

from django.urls import path
from . import views

path('',views.index, name='index'), 

HTML:

<a   href="{% url 'index' %}">Create new Recipe</a>
  • Related