Django Bootstrap form field won't resize - django

I am using bootstrap to display my django forms but am having trouble getting them to resize. One of the forms is a text input area and I want it to span most of my panel width. I've tried multiple things but haven't been able to resize it/make the input area bigger.
The html:
<div class="container-fluid">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="text-center">
Raw sQL Query
</h4>
</div>
<form action="/InterfaceApp/table_search/" method="post" class="form">
{% csrf_token %}
<div class="panel-body text-center">
{% bootstrap_form rawsQL %}
</br>
<div class="container-fluid">
<button type="submit" class="btn btn-primary center-block" value="Submit" name="rawsQL">
{% bootstrap_icon "fire" %} Submit
</button>
</div>
</div>
</form>
</div>
The form:
class TextFieldForm(forms.Form):
def __init__(self,*args,**kwargs):
section_label = kwargs.pop('section_label')
initial_value = kwargs.pop('initial_value')
required_val = kwargs.pop('required')
super(TextFieldForm,self).__init__(*args,**kwargs)
self.fields['text'].label=mark_safe(section_label)
self.fields['text'].initial=initial_value
self.fields['text'].required=required_val
text = forms.CharField()
Right now it looks like this:
Can anyone help me with making the input area wider??

Have a look at grid system and form control sizing.

So it turns out that when you pass in forms to django templates like {% bootstrap_form formname %} you have to go into where you defined the form to edit how it looks. I fixed my resizing issue by doing:
Form:
class TextFieldForm(forms.Form):
def __init__(self,*args,**kwargs):
section_label = kwargs.pop('section_label')
initial_value = kwargs.pop('initial_value')
required_val = kwargs.pop('required')
super(TextFieldForm,self).__init__(*args,**kwargs)
self.fields['text'].label=mark_safe(section_label)
self.fields['text'].initial=initial_value
self.fields['text'].required=required_val
text = forms.CharField(widget=forms.TextInput(attrs={'class':"form-control text-center",'placeholder':".col-md-8"}))
html:
<div class="col-md-8 centering">
{% bootstrap_form rawsQL %}
</div>

Related

how to put my form in line orientation (horizontal). django forms

I want to put my form in horizontal.I tried to do this, but it got unformatted and disorganized
MY HTML:
<div class="tab-pane fade container-fluid p-2" id="profile" role="tabpanel"
aria-labelledby="profile-tab">
<h6 class="m-0 font-weight-bold text-primary">Horas Adicionais</h6>
<div class="row mt-4">
<div class="col">
{{ form_funcionarioadicional.management_form }}
{% for fa in form_funcionarioadicional %}
<div class="faform row">
<div class="col">
{{fa}}
</div>
</div>
{% endfor %}
</div>
</div>
</div>
currently it is like this, I wanted to leave it horizontal
How can I fix this in html, or in forms.py?
You can iterate form like this with col class of Bootstrap...
views.py
def Demoview(request):
form = DemoForm()
context = {'form':form}
return render(request,'index.html',context)
form.py
class DemoForm(forms.ModelForm):
class Meta:
model = DemoClass
fields = "__all__"
widgets = {
'field1':forms.TextInput(attrs={'class':'form-control','placeholder':'Field1'}),
'field2':forms.NumberInput(attrs={'class':'form-control','placeholder':'Field2'}),
'field3':forms.EmailInput(attrs={'class':'form-control','placeholder':'Field3'}),
'field4':forms.TextInput(attrs={'class':'form-control','placeholder':'Field4'}),
}
HTML Code
{% block body %}
<div class="container-fluid">
<div class="row">
{% for fm in form %}
<div class="col-6">
<label >{{fm.label}}</label>
{{fm}}
</div>
{% endfor %}
</div>
</div>
{% endblock body %}
Output

How to add spaces between lines and align elements in the same column in InlineCheckboxes of crispy-form (Django)?

I have a form field which is CheckboxSelectMultiple and I use InlineCheckboxes from crispy-forms to make all those check boxes inline. Since I don't have the image of it I will use code snippet as an example.
this is how I want the form looks like in html and I will use stars instead of boxes
*name1 *name2 *name3 *name4
*name5 *name6 *name7 *name8
*name9 *name10 *name11 *name13
What can I do to add more spaces between each line and make it aligns vertically like shown above?
here is my forms.py
class AttendanceForm(forms.ModelForm):
class Meta:
model = Attendance
fields = ['student',]
widgets = {'student': forms.CheckboxSelectMultiple()}
def __init__(self, class_pk, current_user, *args, **kwargs):
super(AttendanceForm, self).__init__(*args, **kwargs)
current_student = Class.objects.get(id=class_pk)
self.helper = FormHelper(self)
self.fields['student'].queryset = current_student.student.order_by('first_name')
self.helper.layout = Layout(
InlineCheckboxes('student')
)
my current html. (it's not aligned perfectly as shown above!)
<form method="POST" class="ml-auto mr-auto">
{% csrf_token %}
<div style="text-align: justify;">{% crispy form form.helper %}</div>
<button
type="submit"
class="btn btn-success btn-sm"
style="font-size: 15px"
>
Check
</button>
</form>
Thanks before hand!
Now all I want to do is to make all elements display inline and add spaces to each line if it has more than one line. So I don't need to use crispy form for that. I remove the all crispy form tags from template and form class then in template all i need to do is:
<form method="POST" class="ml-auto mr-auto">
{% csrf_token %}
<div class="row mt-2 ml-auto mr-auto">
{% for form in form.student %}
<div class="col-md-4">
<h6 class="ml-2 mt-2" id="info-text">
{{ form.tag }} {{ form.choice_label }}
</h6>
</div>
{% endfor %}
</div>
<button
type="submit"
class="btn btn-success btn-sm"
style="font-size: 15px"
>
Check
</button>
</form>

How to access object.pk in a function listview without having to iterate through the objects with a for loop?

I have a FBV using django-tables2 so the the template already call the objects but i need to add a button that triggers a modal so I need to call the pk of the objects inside the button
here's the views.py
def Patients_list(request):
patients = Patient.objects.filter(user=request.user)
table = PatientTable(patients)
RequestConfig(request).configure(table)
return render(request, 'patients/patients_list.html',{
'table' : table,
'patients':patients,
})
and the button is callable at the tables.py
class PatientTable(tables.Table):
FirstName = tables.Column(linkify=("patients:patient_detail", {"pk": tables.A("pk")}))
LastName = tables.Column(linkify=("patients:patient_detail", {"pk": tables.A("pk")}))
Telephone_no = tables.Column(linkify=("patients:patient_detail", {"pk": tables.A("pk")}))
delete = TemplateColumn('<button type ="button" class ="btn btn-danger" data-toggle="modal" data-target="#modalDelete" >Deleta</button>',extra_context={'patient': 'Patient'})
class Meta:
model = Patient
attrs = {'class': 'table table-striped table-hover'}
exclude = ("user", "Notes", "Adress")
template_name = 'django_tables2/bootstrap4.html'
and the template:
{% extends 'base.html' %}
{% load render_table from django_tables2 %}
{% block content %}
<div id="content">
{% if user.is_authenticated %}
<h1> Patients list: </h1>
<br>
Add Patient
<br>
<br>
{% render_table table %}
{% else %}
<h2>please login</h2>
{% endif %}
</div>
<div class="modal fade" id="modalDelete" tabindex="-1" role="dialog" aria-labelledby="modalDelete"
aria-hidden="true">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Delete patient!</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Are you sure you want to delete this patient?</p>
</div>
<div class="modal-footer">
<form method="POST" action="{% url 'patients:patient_delete' pk=patients.pk %}">
{% csrf_token %}
<input class="btn btn-danger" value="Yes" type="submit" >
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
so when i call the patient.pk in this page it always return an error :
Reverse for 'patient_delete' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['patients/delete/(?P<pk>[0-9]+)$']
but when i call the same url(with a different name if it's in another context) it worked and every FBV listview tutorial calls the objects in a different scenario using a for loop so how I can achieve that without using the for loop how can I imply that in my scenario here ?
I tried every other method of calling the pk even by
pk=patients.pk
pk=pk
I've been stuck on this for over 10 days now asking here and on forums so any help is really appreciated,I'm really desperate.Thanks.

Multiple formsets only saves the number of formsets in 'extra' attribute: Why?

Short version: Creating dynamically multiple formsets only saves as much formsets specified in extra attribute instead of as many as I've.
Long version: I'm doing my first project with Django and I've to create a CreateView with a Parent and a Child forms. I need to create as many nested Child forms the user wants, so I've created a small code in Javascript that adds/removes the child forms. This is working fine.
The problem is: I can only save as many forms as I've specified in the extra attribute. If I say '3', it creates 3 Child forms and I can save 3 of them, even if I create/remove Child forms. If I say '1', I can create 10, but only will save 1. Seems like I'm missing something in the template, but I don't know what. Here's the relevant code:
forms.py
class ParentForm(ModelForm):
class Meta:
model = Parent
exclude = ()
class ChildForm(ModelForm):
class Meta:
model = Child
fields = ....
ChildFormSet = inlineformset_factory(Parent, Child, form=ChildForm, can_delete=True, extra=1)
views.py
class ParentCreateView(LoginRequiredMixin, CreateView):
model = Entrada
fields = ...
def get_context_data(self, **kwargs):
data = super(ParentCreateView, self).get_context_data(**kwargs)
if self.request.POST:
data['child_form'] = ChildFormSet(self.request.POST)
data['materials'] = Material.objects.all()
else:
data['child_form'] = ChildFormSet()
data['materials'] = Material.objects.all()
return data
def form_valid(self, form):
context = self.get_context_data()
child_form = context['child_form']
with transaction.atomic():
self.object = form.save()
if child_form.is_valid():
child_form.instance = self.object
child_form.field1 = self.object.id
child_form.save()
return super(ParentCreateView, self).form_valid(form)
template.html
<form class="own-form" action="" method="post">
{% csrf_token %}
{% for hidden_field in form.hidden_fields %}
{{ hidden_field }}
{% endfor %}
<h2 class="text-center text-header"> New Entry</h2>
<div class="form-group">
{% for field in form.visible_fields %}
<div class="form-group row">
<div class="col-4 text-center">{{ field.label_tag }}</div>
<div class="col-8">{{ field }}</div>
{% if field.help_text %}
<small class="form-text text-muted">{{ field.help_text }}</small>
{% endif %}
</div>
{% endfor %}
</div>
<hr>
<div class="form-group form-material-box row form-0">
<div class="col-3 text-center">
<label>Total weight: </label>
<input type="number" id="kg_0">
</div>
<div class="col-3 text-center">
<label>Boxes to create: </label>
<input type="number" id="num_boxes_0">
</div>
<div class="col-3 text-center">
<label>Material: </label>
<br>
<select name="item_id" id="material_0">
{% for material in materials %}
<option value="{{ forloop.counter }}">{{ material }}</option>
{% endfor %}
</select>
</div>
<div class="col-3 text-center">
<button type="button" id="create_boxes_0" class="btn btn-danger">Create</button>
</div>
<!-- Nested forms with desired number of boxes -->
<div id="nested_forms_0">
<div class="row" id="box_0">
{% for bala in bala_form %}
<div class="col-3 text-center">
<h5>Bala #1: </h4>
</div>
<div class="col-2 text-center">
{{ bala.kg }}
</div>
<div class="col-2 text-center" >
{{ bala.material }}
</div>
<div class="col-2 text-center" >
{{ bala.box_price }}
</div>
<div class="col-3 text-center">
<button type="button" id='remove_box_0' class="btn btn-danger">Remove box</button>
</div>
{% endfor %}
</div>
</div>
</div>
<p>
{{ child_form.management_form }}
<input id="create" class="btn btn-secondary btn-lg btn-block btn-option btn-form" type="submit" value="Crear" />
</p>
Edit:
javascript relevant code
// Adds the difference between desired boxes and current boxes
function add_boxes(form_id, total, num){
for (let i = total; i != total+num; i++) {
var element = $('#nested_forms_' + form_id + "> :first-child ").clone().css('display', 'none');
element.appendTo('#nested_forms_' + form_id).show('250');
};
$('#id_child-TOTAL_FORMS').val(num + total);
};
// Removes every unneeded boxes
function remove_boxes(form_id, total){
$('#nested_forms_' + form_id).children().each(function(){
if ($(this).index() >= total) {
$(this).fadeTo(150, 0).slideUp(150, function(){
$(this).remove();
});
}
});
$('#id_child-TOTAL_FORMS').val(total);
}
What I'm missing?
Update
I've printed the information passed to Django from the template. Seems like its just getting the empty form without any value when I'm creating more than extra forms. Comparing the diffs when I'm creating 3 forms dynamically (left) and hardcoding 3 as the extra value (right) shows it (bales-materies-primeres is the name of the Child):
https://www.diffchecker.com/XskfB9y3
Your Javascript needs to modify the value of the form-TOTAL_FORMS field in the management form. See the formsets documentation.

ValueError when using ChoiceField

I want to create a Django form that allows users to select options from a dropdown menu, but I am getting a value error:
ValueError: need more than 1 value to unpack
Form:
class DropdownForm(forms.Form):
def __init__(self,*args,**kwargs):
choices = kwargs.pop('choices')
label = kwargs.pop('label')
super(DropdownForm,self).__init__(*args,**kwargs)
self.fields['selected'].label = mark_safe(label)
self.fields['selected'].choices = choices
selected = forms.ChoiceField(widget=forms.Select(attrs={'class':"form-control text-center"}))
View.py:
form_rate = DropdownForm(choices=[("HIGH","HIGH")],label="RATE",prefix="Rate")
form_pass_setup = DropdownForm(choices=[("AUTO","AUTO"),("MANUAL","MANUAL")],label="Pass Setup",prefix="pass_setup")
form_dict.update({'form_rate':form_rate,'form_pass_setup':form_pass_setup})
return render(request,'Nominal.html',form_dict)
Template:
<form action="/InterfaceApp/Nominal_Request/" method="post" class="form">
{% csrf_token %}
<div class="panel-body text-center">
<div class="row pad_forms">
<div class="col-xs-3">
{% bootstrap_form form_rate %}
</div>
<div class="col-xs-3">
{% bootstrap_form form_pass_setup %}
</div>
</div>
<br><br>
<button type="submit" class="btn btn-primary center-block" value="Submit" name="Single">
{% bootstrap_icon "fire" %} Generate a Single Requests
</button>
</div>
</form>
Can anyone tell me why I'm getting this value error?
The problem might occur cause you have not set the required argument choices for your ChoiceField.
Here is a link to the docs: https://docs.djangoproject.com/en/1.8/ref/forms/fields/#choicefield.