admin管理员组文章数量:1328003
I have a work_experience model which contains "is_working" field which is true when a user is still working at a pany. On front end I'm using a toggle switch and I want to change the boolean field value "is_working" on click. What should be the logic to use toggle in django?
Toggle switch
HTML
<div style="display:inline-block">
<label>Currently working here?</label>
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</div>
Model
class Work_Experience(models.Model):
job_title = models.CharField(max_length=100, null=True, blank=True)
pany = models.CharField(max_length=100, null=True, blank=True)
description = models.CharField(max_length=300, null=True, blank=True)
exp_start_date = models.DateField(null=True, blank=True)
exp_end_date = models.DateField(null=True, blank=True)
is_working = models.BooleanField(default=False)
I have a work_experience model which contains "is_working" field which is true when a user is still working at a pany. On front end I'm using a toggle switch and I want to change the boolean field value "is_working" on click. What should be the logic to use toggle in django?
Toggle switch
HTML
<div style="display:inline-block">
<label>Currently working here?</label>
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</div>
Model
class Work_Experience(models.Model):
job_title = models.CharField(max_length=100, null=True, blank=True)
pany = models.CharField(max_length=100, null=True, blank=True)
description = models.CharField(max_length=300, null=True, blank=True)
exp_start_date = models.DateField(null=True, blank=True)
exp_end_date = models.DateField(null=True, blank=True)
is_working = models.BooleanField(default=False)
Share
Improve this question
asked Apr 14, 2019 at 1:31
Ahsaan-566Ahsaan-566
6033 gold badges6 silver badges22 bronze badges
2 Answers
Reset to default 4It is a bad idea to give null=True
parameter for CharField
.
If you want to change the boolean field value is_working
on click, you need to use Jquery.
I created an app named toggle
, so you need to replace it with your app's name.
here is the full code
urls.py::
from django.urls import path
from toggle.views import home, toggle
urlpatterns = [
path('', home),
path('toggle/', toggle),
]
views.py:
from django.shortcuts import render
def home(request):
w, created = Work_Experience.objects.get_or_create(id=1)
return render(request,'home.html', {'workexperiance': w})
from django.http import HttpResponse
from toggle.models import Work_Experience
def toggle(request):
w = Work_Experience.objects.get(id=request.POST['id'])
w.is_working = request.POST['isworking'] == 'true'
w.save()
return HttpResponse('success')
home.html:
<div style="display:inline-block">
<label>Currently working here?</label>
<label class="switch">
<input type="checkbox" id="checkbox" value="{{workexperiance.is_working}}">
<span class="slider round"></span>
</label>
</div>
<script src="https://code.jquery./jquery-3.3.1.js"></script> <!-- Import Jquery Here-->
<script type="text/javascript">
$(document).ready(function() {
$('#checkbox').change(function() {
$.post("/toggle/", {
id: '{{workexperiance.id}}',
isworking: this.checked,
csrfmiddlewaretoken: '{{ csrf_token }}'
});
});
});
</script>
run: ./manage.py runserver
and visit: http://localhost:8000
when you click the Currently working here? check box, will instantly change the boolean field value "is_working".
If you don't mind the page being refreshed, here is how I will do it:
views.py:
from django.urls import reverse_lazy
from .models import Work_Experience
from django.shortcuts import redirect
def toggle_status(request,pk):
status = Work_Experience.objects.get(id=pk)
status.is_working^=True
status.save()
return redirect(reverse_lazy('HTML.html'))
urls.py
from django.urls import path
from .views import toggle_status
urlpatterns = [
...
...
...
path('toggle-status/<int:pk>/', toggle_status, name='toggle-status'),
]
HTML.html
<div style="display:inline-block">
<label>Currently working here?</label>
<a href="{% url 'toggle-status' task.id %}">
<label class="switch">
<input type="checkbox">
<span class="slider round"></span>
</label>
</a>
</div>
本文标签: javascriptHow to use toggle switch with django boolean fieldStack Overflow
版权声明:本文标题:javascript - How to use toggle switch with django boolean field? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742243190a2438962.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论