django table form ad a 3rd column for errors - django

I am trying to get my possible error messages in a 3rd column behind the input fields. but except for customizing my whole html in my template I have no idea how to do this. An with my eyes on the widgets and ModelForm class I guess there should be something for it.
My form:
class ProvinceForm(forms.ModelForm):
"""Form to create or edit Provinces."""
name = forms.CharField()
choice_set = Country.objects.all()
country= forms.ModelChoiceField(queryset=choice_set, empty_label="Choose its country")
flavor = forms.CharField(
widget=forms.Textarea(attrs={'width': 300, 'height': 200}))
class Meta:
model = Province
And then my template:
<form action="{% url 'create_province' %}" method="post">{% csrf_token %}
<table>
{{ province_form.as_table }}
<tr>
<td></td>
<td style="float: right;"><input type="submit" value="add province" /></td>
</tr>
</table>
</form>
What would be the logical way to add that 3rd column instead of stripping the whole {{ province_form.as_table }} apart

You need to render each field separately as:
<table>
{% for field in province_form %}
<tr>
<td>{{field.label}}</td> <td> {{field}} </td> <td> {{field.errors}} </td>
</tr>
{%endfor%}
<tr>
<td></td>
<td style="float: right;"><input type="submit" value="add province" /></td>
</tr>
</table>
Note: you may want to change rendering of errors appropriately by looping over {{field.errors}}.

Related

Delete multiple rows in django

I am trying to delete severals rows at the same time in django.
How can I simply change the state of the booleanfield 'to_delete', just by clicking on the checkbox?
I am using datatables. The way how I am doing it is to create a boolean to_delete in my model, when the checkbox is selected, I am calling the function delete_multiple_company in my view. However, this doesn`t work. Any idea, what I am doing wrong please. Many Thanks,
I`ve created my view:
views.py
def delete_multiple_company(request, company_id):
company = get_object_or_404(Company, pk=company_id)
company = Company.objects.get(pk=company_id)
company.objects.filter(to_delete=True).delete()
return HttpResponseRedirect(reverse("company:index_company"))
urls.py
url(r'^(?P<company_id>[0-9]+)/delete_multiple_company/$', views.delete_multiple_company, name='delete_multiple_company'),
models.py
class Company(models.Model):
to_delete = models.BooleanField(default=False)
index.html
<span class="fa fa-plus"></span>Delete Companies
<table id="dtBasicExample" class="table table-striped table-hover">
<thead>
<tr>
<th>Select</th>
<th>#</th>
<th>Checked ?</th>
</tr>
</thead>
<tbody>
{% for company in companys.all %}
<tr>
<td id="{{ company.id }}"><input type="checkbox" class="companyCheckbox" name="checkedbox" id="{{ company.id }}" value="{{ company.id }}"></td>
<td>{{ company.id }}</td>
<td>{{ company.to_delete }}</td>
</tr>
{% endfor %}
</tbody>
</table>
I experienced a similar issue as you, what I did was put in a form.
index.py
<form method="POST" class="post-form">{% csrf_token %}
<button type="submit" class="save btn btn-default">Delete</button>
<span class="fa fa-plus"></span>Delete Companies</a>
<table id="dtBasicExample" class="table table-striped table-hover">
<thead>
<tr>
<th>Select</th>
<th>#</th>
<th>Checked ?</th>
</tr>
</thead>
<tbody>
{% for company in companys.all %}
<tr>
<td id="{{ company.id }}"><input type="checkbox" class="companyCheckbox" name="checkedbox" id="{{ company.id }}" value="{{ company.id }}"></td>
<td>{{ company.id }}</td>
<td>{{ company.to_delete }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="submit" class="save btn btn-default">Delete</button>
</form>
Clicking on the button then triggered the POST method in my view.
views.py
def index(request):
if request.method == 'POST':
print('I made it here')
# Put your code here, note you will return a dict, so some trial and error should be expected

how to pass the selected check box id of a input tag to views in django and bulk update the selected rows in table

** 1.how to pass the selected check box id of a input tag(which is in for loop, so the name and value of input tag are dynamic ) to views in django and bulk update the selected rows of table in django**
<div class="jumbotron">
<form method="post" action="">
{% csrf_token %}
{{form|crispy}}
<input class = "btn btn-primary" type="submit" value="Search">
</form>
<h3 > Model values </h3>
<ul>
<table class="table" id="tab1">
<thead>
{% if teams %}
<tr>
<th>Select</th>
<th>#</th>
<th><b>Logo</b> </th>
<th><b> Team </b></th>
</tr>
</thead>
<tbody>
{% for value in models %}
<tr>
<td><input name="lol" type = "checkbox" value = "{{value.id}}"/> </td>
<td> {{forloop.counter}}</td>
<td> <img class="w3-display-topmiddle w3-container" src="{{ value.logUri.url }}" alt="alt txt" height="910" width="910"></td>
<td> {{ value.name }} </td>
</tr>
{% endfor %}
</tbody>
</table>
**realized that since i am not keeping input tag in form tag the input tag's name values is not captured by request.POST(now changes are dome in html as below ),later in django views i removed the unnecessary keysvalues using dict.pop() and created a list containing on primary keys
in django orm (model.filter(id__in= lst_of_id_captured).update(field='somevalue') **
<form method="post" action="">
{% csrf_token %}
{{form|crispy}}
<h3 > Model values </h3>
<ul>
<table class="table" id="tab1">
<thead>
{% if teams %}
<tr>
<th>Select</th>
<th>#</th>
<th><b>Logo</b> </th>
<th><b> Team </b></th>
</tr>
</thead>
<tbody>
{% for value in models %}
<tr>
<td><input name="lol" type = "checkbox" value = "{{value.id}}"/> </td>
<td> {{forloop.counter}}</td>
<td> <img class="w3-display-topmiddle w3-container" src="{{ value.logUri.url }}" alt="alt txt" height="910" width="910"></td>
<td> {{ value.name }} </td>
</tr>
{% endfor %}
</tbody>
</table>
<input class = "btn btn-primary" type="submit" value="Search">
</form>

validate display none in django

template is
<button type="submit" id="add" class="button_style" onclick="addreporter();" value="Add New Authorised Reporter">Add New Authorised Reporter</button>
<div id="authorisedreporter" style="display:none">
<form method="post" action="{% url incident.views.about_me %}">{% csrf_token %}
<table width="100%">
<tr>
<td style="width:100px;">First name:</td>
<td>{{registerform.first_name}}{{registerform.first_name.errors}}</td>
</tr>
<tr>
<td>Last name:</td>
<td>{{registerform.last_name}}{{registerform.last_name.errors}}</td>
</tr>
<tr>
<td>Daytime phone:</td>
<td>{{createprofile.phone_daytime}}{{createprofile.phone_daytime.errors}}</td>
</tr>
<tr>
<td>Mobile phone:</td>
<td>{{createprofile.phone_mobile}}{{createprofile.phone_mobile.errors}}</td>
</tr>
<tr>
<td>Email:</td>
<td>{{registerform.email}}{{registerform.email.errors}}</td>
</tr>
<tr>
<td>User name</td>
<td>{{registerform.username}}{{registerform.username.errors}}</td>
</tr>
<tr>
<td>Password</td>
<td>{{registerform.password}}{{registerform.password.errors}}</td>
</tr>
<tr>
<td colspan=2 "">
<input type="checkbox" name="is_qualified_firstaiders" style="margin: 0;vertical-align:middle" />Qualified First Aiders</td>
</tr>
</table>
</form>
</div>
js:
function addreporter(){
$("#authorisedreporter").toggle();
if ($("#authorisedreporter").is(":visible")) {
$("#authorisedreporter").show();
$("#add").hide();
}
}
Using the jquery toggle,the gets open on click of this <button>Add new Authorized Reporter</button>,every thing is fine except that if their is any error in form,the form is going to be in hide mode,if i want to see the errors again i need to click the <button>Add new Authorized Reporter</button>,its a small logic to set if errors are in form,the form should not hide.What i thought is if i validate the display:none in <div> will work but no idea how to validate it in template.
Thanks
You can do:
<div id="authorisedreporter" {% if not registerform.errors %}style="display:none"{% endif %}>
Also, you might have to modify addreporter() accordingly.

Django model filter not pulling in object

I'm using Django 1.4 with Python 2.7 on Ubuntu 12.04.
I have a template that is supposed to show a product and a list of product features for each product and for some reason the features don't show in the template.
Here is the view:
#login_required
def view_products(request):
"""
.. function:: view_products()
View the Products
:param request: Django Request object
"""
data = { 'user' : request.user }
if (request.user.is_authenticated() and request.user.is_superuser):
products = Products.objects.all()
add_feature_form = rsb.forms.AddProductFeatureForm();
data.update({ 'form' : add_feature_form })
data.update({ 'products' : products })
data.update(csrf(request))
return render_to_response("view_products.html", data)
return render_to_response("index.html", data)
Here is the portion of the template working with the product features:
<table>
{% for product in products %}
<tr>
<td align="right">Product Name:</td><td>{{ product.name }}</td>
</tr>
<tr>
<td align="right">Price:<br /></td><td>${{ product.price }}</td>
</tr>
<tr>
<ul>
{% for productfeature in product.productfeature_set.all %}
<form action="/removeProductFeature/" method="post">{% csrf_token %}
<li>
{{ productfeature.feature }}
<input type="hidden" name="feature" value={{ productfeature.feature }}>
<input type="hidden" name="product_id" value={{ product.id }}>
<label class="formlabel"> </label><input type="submit" value="Remove ►">
</tr>
</form>
{% endfor %}
</ul>
</tr>
<tr>
<form action="/addProductFeature/" method="post">{% csrf_token %}
<table>
<tr>
<td align="right"><label class="formlabel">Add Feature:<br /></label></td><td>{{ form.feature }}</td>
</tr>
<input type="hidden" name="product_id" value={{ product.id }}>
<tr>
<td align="right"><label class="formlabel"> </label></td><td><input type="submit" value="Add ►"></td>
</tr>
</form>
</table>
</tr>
{% endfor %}
</table>
Basically this template should show you a product. Each feature will be listed below it with the option to "remove" that feature. Then, at the bottom, a field that allows you to add additional features.
The existing features do not show up at all. Any suggestions on what I might be doing wrong?
UPDATE 1:
I missed an s in the template. product.productfeatures_set.all not product.productfeature_set.all. I'm good to go. Thanks all!
Please don't do this:
product_features = []
for product in products:
features = ProductFeatures.objects.filter(product = product)
product_features.append(features)
product.features = product_features
Instead, just pass your products variable to the template context.
And in the template do:
{% for product in products %}
Product id: {{ product.pk }}
{% for productfeature in product.productfeature_set.all %}
{{ productfeature.feature }}
{% endfor %}
{% endfor %}
What is productfeature_set you would ask (or, I hope you would ask :D), and that's a very good question. Don't panic, it's all documented.
Now, this is going to cause subqueries to spawn. The solution is to use prefetch_related. But you don't have to worry about that for the moment I think :)
Yes, you are trying to implement something that is already build into Django!
You should use the build in reverse relations of django.
# give an Product instance product
# this should work
product.productfeature_set.all()
Which is accessible from the template a product.productfeature_set.all
and can be itterated over.

How to display option-names instead of option-values?

I poked around with get_FOO_display to no avail, here's what I've got (I trimmed some stuff to make it easier to read, like a custom def save:
class Profile(models.Model):
LEVEL_CHOICES = (
(1, 'Primary'),
(2, 'Secondary'),
(3, 'Tertiary'),
)
level = models.IntegerField( verbose_name = 'Level', blank=True, null=True, choices=LEVEL_CHOICES)
piplevel = models.ForeignKey(AccessLevel, related_name='profiles')
vanity = models.ForeignKey(AccessLevel, related_name='vanity')
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
# HTML CODE from Template below
<tbody>
{% for field in user_form %}
<tr>
<th>{{ field.label_tag }}</th>
<td class="updatable">
<div class="field">
{{ field }}
</div>
{% if field.errors %}{{ field.errors }}{% endif %}
</td>
</tr>
{% endfor %}
{% for field in profile_form %}
<tr>
<th>{{ field.label_tag }}</th>
<td class="updatable">
<div class="field">
{{ field }}
</div>
{% if field.errors %}{{ field.errors }}{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
The output I get:
Level 1
Piplevel 15
Vanity 20
The output I want:
Level Primary
Piplevel Diamond
Vanity Ambassador
I tried replacing the {{ field }} and for loop with {{field.get.level.display}} and {{ field.get_piplevel_display }} but it they just returned blanks. When these display, they're a drop-down menu and DO have all the named-options available - that is, when I click on the "15" I do get an option list i.e.:
Piplevel 15 click (current selected is 15)(option value="1">Regular(/option>(option value="2">Platinum(/option>etc. (not html code)
Am I missing some easy function in Django for this?
Edit: Here's the HTML output I GET currently.
<tr class="even">
<th>
<label for="id_piplevel">Piplevel</label>
</th>
<td class="updatable" title="Click to modify">
<div class="field" style="display: none;">
<select id="id_piplevel" name="piplevel">
<option value="">---------</option>
<option value="1">Regular</option>
<option value="2">Platinum</option>
...
<option selected="selected" value="7">Emerald</option>
</select>
</div>
<div>7</div>
</td>
</tr>
Here's the HTML output I WANT, ideally:
<tr class="even">
<th>
<label for="id_piplevel">Piplevel</label>
</th>
<td class="updatable" title="Click to modify">
<div class="field" style="display: none;">
<select id="id_piplevel" name="piplevel">
<option value="">---------</option>
<option value="1">Regular</option>
<option value="2">Platinum</option>
...
<option selected="selected" value="7">Emerald</option>
</select>
</div>
<div>Emerald</div>
</td>
</tr>
And, if I replace {{ field }} with {{field.get_piplevel_display}} or {{field.choice.get_piplevel_display}}, I get this as output:
<tr class="even">
<th>
<label for="id_piplevel">Piplevel</label>
</th>
<td class="updatable">
<div class="field"> </div>
</td>
</tr>
It seems like this is something everyone would want, and so I expected there to be some argument in Django that just... fixes this, but I can't find it. Sorry for making a simple question seem so complicated - this is supposed to be a simple question, isn't it?
For every field that has choices, Django automatically adds a get_FOO_display() method on the model (docs). All this method does is take the already stored value for the field, looks it up in the provided choices for the field and returns the "display" text for that value.
This method is not available on the field or the form. You must reference an existing object or the instance of the form (e.g. form.instance).
Again, I'm not sure what you're trying to accomplish, because the <select>s are already populated such that they display the text and not the underlying value. Even if you're editing an existing object, the <select> will still show the display text for the currently set value.
I don't really see what you're trying to do, but:
{{field.get.level.display}}
should be something like
{{field.choices.get_level_display}}
# or
{{field.get_level_display}}