0

Im working on a simple web app - that turns on a led on a arduino.

I'v got it working, but the problem is i dont want for the page to redirect/refresh after i press the button.

So once again; after i press the button i just want it to start a python script and dont redirect to a other page or refresh the current page.

Do i need to implement ajax or is there any other way? Tried some stuff with ajax but couldnt get it to work.

Code:

# urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('app.urls')),
    path(random_string(5), views.ledOn, name = "ledikaOn"),
    path(random_string(5), views.ledOff, name= "ledikaOff"),
    
    
# views.py

def ledOn(request):
    s.send(ledOnUkaz.encode('ascii'))  # to poslje kodo "ukaz ON" v server
    return render(request, 'home.html')
    
    
# home.html
    
<a class="btn btn-primary text-uppercase font-weight-bold bg-primary" href="{% url 'ledikaOn' %}" role="button">LED ON</a>
pampi
  • 7
  • 2

2 Answers2

0

Your href attribute is wrapped in double quotes: "" and '':

href="'{% url 'ledikaOn' %}'"

Change it to this:

href="{% url 'ledikaOn' %}"
SLDem
  • 2,065
  • 1
  • 8
  • 28
-1

Ok so i figured it out. If anybody has the same problem, here's the solution:

Add this to your .js file:

    $("#btnid").click(function(event) { // button event handler
      event.preventDefault(); // prevent page from redirecting
      $.ajax($(this).attr('href')).done(function(response) { // send call
         href="{% url 'ledikaOn' %}"  // url to your page/views
       });

modify your .html file: ** i had to add -> id="btnid" <-

<a class="btn btn-primary text-uppercase font-weight-bold bg-primary" id="btnid" href="{% url 'ledikaOn' %}" role="button">stissno</a>

i found the solution here: How to disable redirecting when clicking on a button?

Best regards, pampi

pampi
  • 7
  • 2