17π
One option is, you can wrap the submit
button with a form
Something like this:
<form action="{% url path.to.request_page %}" method="POST">
<input id="submit" type="button" value="Click" />
</form>
(remove the onclick
and method
)
If you want to load a specific part of the page, without page reload β you can do
<input id="submit" type="button" value="Click" data_url/>
and on a submit
listener
$(function(){
$('form').on('submit', function(e){
e.preventDefault();
$.ajax({
url: $(this).attr('action'),
method: $(this).attr('method'),
success: function(data){ $('#target').html(data) }
});
});
});
114π
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:
<form action="#" method="get">
<input type="text" value="8" name="mytextbox" size="1"/>
<input type="submit" class="btn" value="Click" name="mybtn">
</form>
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')
- [Django]-Django UniqueConstraint
- [Django]-LEFT JOIN Django ORM
- [Django]-Distributed task queues (Ex. Celery) vs crontab scripts
12π
How about this:
<a class="btn btn-primary" href="{% url 'url-name'%}">Button-Text</a>
The class is including bootstrap styles for primary button.
- [Django]-Using Cython with Django. Does it make sense?
- [Django]-Optimal architecture for multitenant application on django
- [Django]-Why there are two process when i run python manage.py runserver
5π
you can put the input inside a form like this:-
<script>
$(document).ready(function(){
$(document).on('click','#send', function(){
$('#hid').val(data)
document.forms["myForm"].submit();
})
})
</script>
<form id="myForm" action="/request_page url/" method="post">
<input type="hidden" id="hid" name="hid"/>
</form>
<div id="send">Send Data</div>
- [Django]-Problems with contenttypes when loading a fixture in Django
- [Django]-Django admin, hide a model
- [Django]-Parsing a Datetime String into a Django DateTimeField
5π
For deleting all data:
HTML FILE
class="btn btn-primary" href="{% url 'delete_product'%}">Delete
Put the above code in an anchor tag. (the a tag!)
url.py
path('delete_product', views.delete_product, name='delete_product')]
views.py
def delete_product(request):
if request.method == "GET":
dest = Racket.objects.all()
dest.delete()
return render(request, "admin_page.html")
- [Django]-Error loading MySQLdb Module 'Did you install mysqlclient or MySQL-python?'
- [Django]-Unsupported operand type(s) for *: 'float' and 'Decimal'
- [Django]-How to check whether the user is anonymous or not in Django?
4π
For example, a logout button can be written like this:
<button class="btn btn-primary" onclick="location.href={% url 'logout'%}">Logout</button>
Where logout endpoint:
#urls.py:
url(r'^logout/$', auth_views.logout, {'next_page': '/'}, name='logout'),
- [Django]-Django 1.5 custom User model error. "Manager isn't available; User has been swapped"
- [Django]-How to add readonly inline on django admin
- [Django]-DRY way to add created/modified by and time