NBC
NBC

Reputation: 1698

Button not executing Django/Python script

In my home.html template:

  <button type="button" class="btn btn-dark"
          method = 'GET' action = '/action_name/' name="audio_record">Record Audio</button>

In my views.py:

def audio_functions(request):
    print('called function')

In my urls.py:

path('/action_name/', views.audio_functions, name='audio-record'),

what am i doing wrong?

Edit: I replaced the button with the suggested version below:

  <a href="{% url 'audio-record' %}" class="btn btn-dark"
    >Record Audio</a>

But I have a new problem. I actually don't want to redirect to by url/action_name. I just want to trigger the python script within the browser. How can I do this?

Upvotes: 0

Views: 49

Answers (2)

tim-mccurrach
tim-mccurrach

Reputation: 6825

In your urls.py you do not need the leading forward slash, as django adds this in automatically. Replace it with this and it should work:

path('action_name/', views.audio_functions, name='audio-record'),

Also the method and action attribues would normally go in the <form> tag, and not the button one. Also change type to submit on your button.

As @SALAHEDDINEELGHARBI says, you should really be using {% url 'audio-record' %} rather than hard-coding the url, however this is not the problem in this case (you shouldn't have a leading slash in urls as this would leave to a url with a double slash)

EDIT - In response to your edit: You can't trigger a python script within the browser. It's a common misconception. Django is a web framework built in python, yes. But anything that happens in the browse has to happen in javascript. If you want to use python, you'll need to make a call to some django endpoint, do the python, and the send it back.

Upvotes: 1

SALAH EDDINE ELGHARBI
SALAH EDDINE ELGHARBI

Reputation: 305

in html :

   <a href="{% url 'audio-record' %}" class="btn btn-dark"
        >Record Audio</a>

and urls.py

path('action_name', views.audio_functions, name='audio-record'),

Upvotes: 1

Related Questions