'Python/Django - Input radiobutton to autosend email
I'm trying to set up a radiobutton input that let's you choose to send email automatically. What I want is a form that gives input: manual or auto. This will the one row I have in the sqlite3 database for this:
class AutoSendMail(models.Model):
auto = models.BooleanField(default=False)
manual = models.BooleanField(default=True)
So eventually what I want is that in AutoSendMail the auto is set to true when the checkbox is checked + manual to true (or vise versa when the checkbox is unchecked, depending on the checkbox input)
I got as far as this:
mailindex.html
<div style="display:inline-block">
<label>Send email automatically </label>
<label class="switch">
<input type="checkbox" id="checkbox" value="{{auto_send.auto}}">
<span class="slider round"></span>
</label>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script> <!-- Import Jquery Here-->
<script type="text/javascript">
$(document).ready(function() {
$('#checkbox').change(function() {
$.post("mailbox/auto_send/", {
id: 1,
isauto: this.checked,
csrfmiddlewaretoken: '{{ csrf_token }}'
});
});
});
</script>
urls.py
path('mailbox/auto_send/', login_required(views.AutoSendView.as_view()), name='auto_send'),
models.py
class AutoSendMail(models.Model):
auto = models.BooleanField(default=False)
manual = models.BooleanField(default=True)
views.py
class AutoSendView(generic.TemplateView):
model = AutoSendMail.objects.all()[0]
extra_context = {"mailbox_page": "active"}
context_object_name = 'auto_send'
template_name = 'core/mailbox/mailindex.html'
def post(self, queryset=None, **kwargs):
w = AutoSendMail.objects.get(id=1)
logger.info(w.type)
if self.request.POST['isauto'] == 'true':
logger.info("check isauto True")
logger.info(w.type)
AutoSendMail.objects.filter(pk=1).update(auto=True)
AutoSendMail.objects.filter(pk=1).update(manual=False)
elif self.request.POST['isauto'] == 'false':
logger.info("check isauto False")
logger.info(w.type)
AutoSendMail.objects.filter(pk=1).update(auto=False)
AutoSendMail.objects.filter(pk=1).update(manual=True)
else:
logger.info("check not recognized")
EDIT:
When I tick the box I do get a signal from checkbox > urls.py > views.py. But in what I do it only gives vale true to views.py. How do I make the unchecked checkbox give false and the checked checkbox to true
Hope someone can help me :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
