Django - Boolean Fields on Checkbox Input - django

I have a ModelForm which has Boolean Field and CheckboxInput. The Boolean field provides values of 0 (False) & 1 (True) by default in the MYSQL Database.
The form is a modal and uses JSON to output the content from the Model. When the JSON file adds the content it enters either True or False.
The issue occurs when I try to change and submit the value of the Checkbox Input from False to True. When I try this I get the following message:
Oddly this works the other way around and allows me to submit the change from True to False.
Below is the code (I have only included the field with the issue.) The field is First_Time_Booking
Model.
first_time_booking = models.BooleanField()
Form Widget.
'first_time_booking': CheckboxInput(attrs={'class': 'form-check-input', 'id': 'bfbw_edit_first_time_booking', 'name': 'bfbw_edit_first_time_booking'}),
View
def update_bfbw(request):
if request.method == 'POST':
bfbw_booking_id = request.POST.get('bfbw_booking_ref')
bfbw_data = BFBWBookings.objects.get(id=bfbw_booking_id)
bfbw_data.school_name = request.POST['school_name']
bfbw_data.first_time_booking = request.POST.get('first_time_booking', False)
bfbw_data.save()
else:
print(form.errors)
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
I have tried writing an If function to alter the value, this then works to change the value to On but not when switching it off. I then get the below message.
if request.POST['first_time_booking'] == 'on':
bfbw_data.first_time_booking = True
else:
bfbw_data.first_time_booking = False
Any help on this will be great. Thanks in advance for any replies.

The value is either "on" (if the user has checked the checkbox), or does not exists (if the user has not checked the checkbox).
You thus can work with:
bfbw_data.first_time_booking = request.POST.get('first_time_booking') == 'on'
Note: It is better to use a FormĀ [Django-doc]
than to perform manual validation and cleaning of the data. A Form will not
only simplify rendering a form in HTML, but it also makes it more convenient
to validate the input, and clean the data to a more convenient type.

Related

How to check which the fields of a form has been modified in Flask?

I am trying to figure out a way to check if there is any built-in Form method that would return true if the form has been modified in Flask/WTForms
I know that in Django Forms, we have that flexibility to use form.has_changed() method that would do exactly what i want.
I am trying to check if the form has been modified and if it is I would to do some database updates.
If anybody has any idea, please let me know about this or suggest the right approach to start with.
I haven't found any built-in Form methods, but here is my quirky approach how I check whether the form was changed, and which fields were changed.I'm just storing the whole original form in the additional StringField, and then comparing it with the new form. As I don't want this field to be displayed on the page, I'm changing it's style to style="visibility:hidden;display:none" in the html file
#app.route("/page", methods=["GET", "POST"])
def page(tag=None):
class Person(FlaskForm):
name = StringField("name")
age = StringField("name")
class Spouses(FlaskForm):
people = FieldList(FormField(JetsonForm), min_entries=0)
people_orig = StringField("people_orig")
submit = SubmitField("Submit")
data_from_database = get_data_from_database()
data = {'people': [], 'people_orig': json.dumps(data_from_database)}
for person_name in data_from_database:
data['people'].append(data_from_database[person_name])
form = Spouses(data=data)
people_orig_data = json.loads(form.people_orig.data)
people_new_data = dict()
for person in form.people:
people_new_data[person.name.data] = {
"name": persom.name.data,
"age": peron.age.data
}
if form.is_submitted():
for person_name in people_new_data:
for field in people_new_data[person_name]:
if people_new_data[person_name][field] != people_orig_data[cam][key]:
#update your database
return redirect(request.url)
return render_template("page.html",form=form)

Can you use a Django form multiple times in one view?

I have a Django view that uses one form multiple times. The form is just a boolean field form that is supposed to initialize to True but then the user can decide to uncheck the boxes or not.
The problem I'm having is that the all of the fields evaluate to True no matter what the user leaves checked. Is this a problem with using the same form multiple times, or did I mess something else up?
The form looks like this:
class DataTypeForm(forms.Form):
def __init__(self,*args,**kwargs):
section_label = kwargs.pop('section_label')
initial_value = kwargs.pop('initial_value')
super(DataTypeForm,self).__init__(*args,**kwargs)
self.fields['dataType'].label=mark_safe(section_label)
self.fields['dataType'].initial=initial_value
self.fields['dataType'].required=False
dataType = forms.BooleanField(required=False)
This is the view:
def Manual_Request(request):
form_dict = {}
arg_dict = {}
message = message = {'last_url':'Nominal_Request'}
if request.method == 'POST':
logger.info("Query submitted, beginning query results render for:")
form_NOM = DataTypeForm(request.POST or None,section_label="ENG_NOM",initial_value=True)
form_DDM = DataTypeForm(request.POST or None,section_label="SCI_DDM",initial_value=True)
form_RAW = DataTypeForm(request.POST or None,section_label="SCI_RAW",initial_value=False)
if form_NOM.is_valid():
NOM = form_NOM.cleaned_data['dataType']
arg_dict.update({'ENG_NOM':str(NOM)})
logger.info("ENG_NOM: {val}".format(val=NOM))
if form_DDM.is_valid():
DDM = form_DDM.cleaned_data['dataType']
arg_dict.update({'SCI_DDM':str(DDM)})
logger.info("SCI_DDM: {val}".format(val=DDM))
if form_RAW.is_valid():
RAW = form_RAW.cleaned_data['dataType']
arg_dict.update({'SCI_RAW':str(RAW)})
logger.info("SCI_RAW: {val}".format(val=RAW))
return Request_Results(request,args_dict)
else:
logger.info("Rendering query page")
form_NOM = DataTypeForm(section_label="ENG_NOM",initial_value=True)
form_DDM = DataTypeForm(section_label="SCI_DDM",initial_value=True)
form_RAW = DataTypeForm(section_label="SCI_RAW",initial_value=True)
form_dict.update({'form_NOM':...etc})
return render(request,'InterfaceApp/COMMRequest_Manual.html',form_dict)
Help much appreciated!
I haven't run your code, but my best guess is that yes, it's a problem with using the same form multiple times in the same view. The reason? All of your <input type="checkbox" name="..." ... /> tags will have the same name, 'dataType'. The user's browser knows nothing of your back-end, and will just send, for example, dataType=on&dataType=on as POST data for the three fields if two are checked and one is not.
Seeing the problem here? How is django supposed to know which of those dataType fields are for your NOM, DDM, or RAW forms? It can't know.
You should be able to solve this using form prefixes. In short, there's a kwarg that you can pass to a form's __init__() that will cause a prefix to be added to all of the form items in the rendered HTML. So, for example:
form_NOM = DataTypeForm(request.POST or None, section_label="ENG_NOM",
initial_value=True, prefix="NOM")
form_DDM = DataTypeForm(request.POST or None, section_label="SCI_DDM",
initial_value=True, prefix="DDM")
form_RAW = DataTypeForm(request.POST or None, section_label="SCI_RAW",
initial_value=False, prefix="RAW")
Hope that helps!
This is exactly what Django formsets are for. They allows you to create a set of the same type of form. It handles prefixes, and adds a management form so that Django doesn't get confused as to what data comes from what form.
https://docs.djangoproject.com/en/1.8/topics/forms/formsets/

submitting form results into db - django

i created a form to save a post into db for my blog project. I've designed index page. now i am tryin to create a form to create new posts. before that i was using ' manage.py shell'
here is my view :
def addpost(request):
form = addForm()
if request.method=="POST":
titleform = request.POST['title']
bodyform = request.POST['body']
checkform = request.POST['isdraft']
if form.is_valid():
n = Post(title = titleform, body = bodyform, isdraft=checkform)
n.save()
return HttpResponseRedirect('/admin/')
else:
pass
return render(request,'userside/add.html',{'form':form,})
my model.py:
class Post(models.Model):
title = models.CharField(max_length = 100)
body = models.TextField()
slug = AutoSlugField(populate_from='title',unique=True)
posted = models.DateField(auto_now_add=True)
isdraft = models.BooleanField()
def __unicode__(self):
return self.title
#permalink
def get_absolute_url(self):
return ('view_blog_post',None, {'postslug':self.slug})
class addForm(forms.Form):
title = forms.CharField(max_length=100)
body = forms.CharField(widget=forms.Textarea)
isdraft = forms.BooleanField()
if i submit form as 'isdraft' field is False(unchecked) ; it gives error like:
MultiValueDictKeyError at /admin/addpost/
"Key 'isdraft' not found in "
and if i submit the form as 'isdraft' field is True(checked) ; it gives nothing. just refreshing form. no adding data into db.
i am doing sth wrong..
thank you
edit : Dmitry Beransky's answer worked for checkbox error. but it still doesnt add any data into db. just refreshes the form.
The whole point of using a form is that it takes care of validation and cleaning, that is converting values to the proper data types. That's why you should be accessing form.cleaned_data rather than reques.POST, and you should be doing it inside the if form.is_valid() check.
Edit
I've just noticed that you're never passing request.POST to the form. So form.is_valid() will never be true.
Please go back and read the documentation about using a form in a view.
If a checkbox is not checked in your HTML form, it's name/value is not going to be included in the data that the browser sends to your server. Which meanst that the request.POST dictionary is not going to contain an entry for 'isdraft' which in turn will cause a key error when you try to read the isdraft value. A solution is to change the way you read the value from the posted data to:
checkform = request.POST.get('isdraft', False)
rather than throw an error if isdraft isn't found in the dictionary, this will set checkform to False (the default value in case of a missing key)
Maybe your form does not validate at all. Have you checked if your code even reaches those lines after the if form.is_valid() statement ? If they do, what you've done there is right and should create the db row for your new entry, though you could have used
Post.objects.create(....) , and that would have taken away the need for calling the method save().
Some points though:
instead of checking for request.POST , check for request.method == 'POST' , cause there might be a post which has an empty POST dict ( in case no arguments have been submitted ), in that case request.POST fails to provide the right check .
see the docs for more info : request.POST
instead of using request.POST['var_name'] , use request.POST.get('var_name', 'default_value') , cause doing this like request.POST['var_name'] might result in some exceptions ( in case for example the argument is not provided , like what happened for your checkform variable )
Try accessing those variables through form.cleaned_data
and finally , you don't need the else statement in the end , just use the indentation :)

Setting Initial in a ModelFormset

According to the documentation providing initial values for fields that are bound to a model is not possible.
In my model form though I have created an additional unbound field:
class DealCForm(ModelForm):
attach_deal_conversation = forms.BooleanField(required=False, initial=False)
Hence I would like to set this value if certain conditions are met.
View:
deal_formset = modelformset_factory(Deal, form=DealCForm, extra=0)
if (request.POST)
pass
else:
opendeal_formset = deal_formset(queryset=formset_query)
variables = RequestContext(request, {'opendeal_formset' : opendeal_formset)
return render_to_response('conversation.html', variables)
In the view, just before sending it to the template, I have set the value directly, however it doesn't work:
for dfm in deal_formset:
for odfm in opendeal_formset:
if dfm.pk == odfm.pk:
odfm.attach_deal_conversation = True;
But it doesn't work. ANy idea how to set the initial value for an unbound field?
Many Thanks
This runs for me:
for form in opendeal_formset:
form.fields['attach_deal_conversation'].initial=True

Best way to handle request variables in Django

I have a form 'in the wild' that takes many different variables - which may or may not be populated.
try:
app_version = request.REQUEST["appVersion"]
except:
app_version = ''
try:
app_name = request.REQUEST["appName"]
except:
app_name = ''
try:
app_code_name = request.REQUEST["appCodeName"]
except:
app_code_name = ''
Is there a tighter way to accomplish this?
app_version = request.REQUEST.get("appVersion", "")
get(key, default) is a method implemented on Python dicts. If the key exists in the dictionary, its value is returned; if the key does not exist, the specified default value is returned. In Django, request objects are dictionary-like objects, so get is also defined for them in the same manner.
If these variables are intended to populate a form, then you can safely pass the request.POST object directly into the form constructor.
if request.method == 'POST':
form = MyForm(request.POST)
The form will automatically pass the correct values to the correct form fields and use defaults for keys that don't exist and will still create blank fields for missing keys (see addendum).
If you are trying to process a form, it is still better to create a form object as above, and read out the values from that object.
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
# You may process these variables here
print form.appVersion
print form.appName
print form.appCodeName
Remember, validation code is best placed in the form class as well. That way, if form.is_valid() returns True, then you know you have a clean dataset to work with.
Note: Django docs recommend using request.POST or request.GET directly rather than the amalgamated variable request.REQUEST, as it is more explicit.
Addendum:
It is important to understand the difference between bound and unbound forms in this case. If you create an unbound form with form = MyForm(), then when the form is instantiated, it will fill in all fields with the initial property of each field (if it exists). For example, with this code:
from django import forms
class MyForm(forms.Form):
appVersion = forms.CharField(initial='1.0')
appName = forms.CharField()
appCodeName = forms.CharField()
the form will be initialized with appVersion having a value of '1.0'. However, if you bind a POST request to a form like this: form = MyForm(request.POST), then the initial properties are ignored. That means if the POST dict does not include an appVersion key, then that field will be left blank. As long as the field is not required, your form will still validate, and you can modify form.appVersion in the view after validation.
If you have many fields, a more compact version might be:
defaults = { 'field1' : 'val1', 'field2' : 'val2', ...}
defaults.update(request.POST)