I have a Django application where I want to have a setting: if the email is sent automatically or manually. What I have works, but not in the style that I want it.
At the moment I have 2 buttons where you can click them and the setting is set to what you want. But what I would want is radio buttons, that are also already checked or unchecked, depending on the setting.
What I have now is:
model.py
class AutoSendMail(models.Model):
auto = models.BooleanField(default=False)
manual = models.BooleanField(default=True)
send_type = (
('manual', 'MANUAL'),
('auto', 'AUTO')
)
type = models.CharField(max_length=6, choices=send_type, default="manual")
forms.py
CHOICES = [('manual', 'MANUAL'),
('auto', 'AUTO')]
class SendMailSetting(ModelForm):
class Meta:
model = AutoSendMail
fields = ['auto', 'manual', 'type']
widgets = {
"manual": forms.RadioSelect(attrs={'class': 'form-control'}),
"auto": forms.RadioSelect(attrs={'class': 'form-control'}),
'type': forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect)
}
views.py
class AutoSendView(generic.TemplateView):
template_name = 'core/mailbox/autoSendMail.html'
context_object_name = 'autosend'
extra_context = {"mailbox_page": "active"}
form_class = SendMailSetting
def post(self, request, *args, **kwargs):
if request.POST.get('manual'):
logger.info("Set to: manual email send")
AutoSendMail.objects.filter(pk=1).update(auto=True,
manual=False,
type="manual")
elif request.POST.get('auto'):
logger.info("Set to auto email send")
AutoSendMail.objects.filter(pk=1).update(auto=True,
manual=False,
type="auto")
return HttpResponseRedirect(self.request.path_info)
autoSendMail.html
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<div >
<h3 >Update Mail Settings</h3>
<div >
<div >
<div >
<label >Send E-mail</label>
<button type="radio" name="manual" value="manual" >Manual</button>
<button type="radio" name="auto" value="auto" >Automated</button>
</div>
</div>
</div>
</div>
</form>
Currently it looks like this:
And I would like it to look more like this:
At the moment I'm only using a POST request, and I guess, to inform the user I also need to use a GET request, but I can't get it to work. Also I'm now not using the form, I tried to use it in different ways but I can't get the result I want..
Can someone help me?
CodePudding user response:
You had created a modelForm but you are not using it here. Anyway for basic method you can try below method
<input type="radio" name="update_type" value="manual">Manual</input>
<input type="radio" name="update_type" value="auto">Automated</input>
<button type="submit" >Submit</button>
views.py
def post(self, request, *args, **kwargs):
update_type = request.POST.get('update_type'):
if update_type == 'manual':
"update db with manual to true"
else:
"update the db with auto to true"