0

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?

2 Answers 2

1

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'),
Sign up to request clarification or add additional context in comments.

Comments

1

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.

5 Comments

this doesn't seem to work (nothing prints in my terminal when i click the button)
@NBC I've update the answer. If it stil doesn't work can you add what is shown in the network section of your dev-tools, when you click on the button. What is the response code on the request? Does it send?
ok I got it to work, but I think I messed up originally. I actually don't want to redirect to by url/action_name. I just want to trigger the python script within the browser. Sorry and thanks so much for your help.
no worries, I've added an edit in reply to the edit you made in the question.
ah got it. Yeah I'm a rookie at this. Thanks for clarifying!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.