Django: How can I call a view function from template?

后端 未结 6 1956
难免孤独
难免孤独 2020-11-27 12:27

I have a question on how to call a view function from a template HTML button? Like an onclick function? Here is the template:



        
6条回答
  •  天涯浪人
    2020-11-27 13:20

    Assuming that you want to get a value from the user input in html textbox whenever the user clicks 'Click' button, and then call a python function (mypythonfunction) that you wrote inside mypythoncode.py. Note that "btn" class is defined in a css file.

    inside templateHTML.html:

    inside view.py:

    import mypythoncode
    
    def request_page(request):
      if(request.GET.get('mybtn')):
        mypythoncode.mypythonfunction( int(request.GET.get('mytextbox')) )
    return render(request,'myApp/templateHTML.html')
    

提交回复
热议问题