Django displaying selected items from multiple select in template - django

I have a multiple select in a ModelForm. When the page reloads with an error I want the currently selected items to be displayed.
view.html
error: {{ form.recipients.errors }}<br/>
currently selected: <br/>
{% for recipient in form.recipients %} <!--I want some type of loop through only the selected items in my recipients -->
{{ recipient }}<br/>
{% endfor %}
{{ form.recipients }}

You want to loop the value of the field, not the field itself:
{% for recipient in form.recipients.value %}
{{ recipient }}
{% endfor %}

Related

How to keep 2 fields using django forms

As django forms run a loop for the fields
{% for field in form %}
<div class="col-6">
{{ field.errors }}
{{ field.label_tag }}
{{ field }}
</div>
{% endfor %}
I actually want to have 2 fields, I found a cheap solution to this issue using Bootstrap4
I created the columns by using col-6 by which I get 2 fields in the row. But what if I want to make custom designs of forms in django ?
Personally i am using this method to show only fields i want and then have if statements for the way each field should be shown
{% for i in form %}
{% if i.name in 'title,address,city' (Fields you want to show) %}
{% for error in i.errors %}
<div>
<li><strong>{{ error|escape }}</strong></li>
</div>
{% endfor %}
<p>
<label>{{i.label}}:</label>
<input type="text" name="{{i.name}}" required id="id_{{i.name}}">
</p>
{% endif %}
{% endfor %}
And like that you can style it however you want and show only those you want to show

Django showing values of an annotated queryset without knowing the filed name

I´m building a dashboard where you can cross data from a sales database.
For example, sales by seller, products by client, etc. I let the user choose both option 1 and option 2.
I get the form back to the view and annotate the model with those options:
if filter_opts.is_valid():
option_1 = filter_opts.cleaned_data["option_1"]
option_2 = filter_opts.cleaned_data["option_2"]
results = ventas.values(option_2).annotate(Count(option_1, distinct=True))
The annotation works fine and if I just print the queryset in the template
{% for case in results %}
{{ case }}
{% endfor %}
I can see it like:
{'cliente': '502 EMSUR', 'prod_nombre__count': 9}
Then in the template I want to show only the values. But I can´t tell forehand which would be the value name to do something like this:
{% for case in results %}
{{ case.option_1 }}
{{ case.option_2 }}
{% endfor %}
If I iterate over the result I can see the field name:
{% for case in results %}
{% for item in case %}
{{ item }}
{% endfor %}
{% endfor %}
How can I show the values of that fields?
Thanks!
Since each case in results is a dictionary, you can use the property .items inside the template to iterate through its keys and values:
{% for case in results %}
{% for item, value in case.items %}
{{ item }} - {{ value }}
{% endfor %}
{% endfor %}

Django: how to pass value from a list template to a view, static url

In my template, I have a list of users on which the connected user can click to access the profile of a user on the list. I want to retrieve the user I clicked on in my view. However, I would like to keep a static URL (to avoid having user-related settings displayed in the URL). Do you have any idea how to do that?
Here is my template :
{% extends "base.html" %}
{% block content %}
{% if userConnected.adminRole == "SuperUser" %}
<h2>Members List</h2>
<h3>Members from OU : {{ userConnected.ou }} </h3>
{% for element in user_list %}
<p>{{ element.first_name }}</p>
{% endfor %}
{% endif %}
{% endblock %}
What should I add to my template and what should I write in my view "edit_other_profile" to get the right user ? Thanks a lot

Django accessing form fields dynamically in template

I am trying to mimic the django admin's fieldsets functionality, to group form fields in my own view.
So I have a form in my template, and I have passed the fieldset list in my context data.
{% for fieldset in fieldsets %}
<fieldset class="module aligned">
<h2>{{ fieldset.0 }}<h2>
{% for field in fieldset.1.fields %}
<div>
{{ form.field }}
</div>
{% endfor %}
</fieldset>
{% endfor %}
The problem is the {{ form.field }} is treating field as a string, and not as the value stored in the variable. Is there a way to access this in the template, like getattr().

django templates: How to know form field type and add any buttons based on field type

I am displaying my django forms dynamically with below code.
{% for field in form %}
{% if field.field.required %}
<span class="red">*</span>
{% endif %}
{{ field.label }}:
{{ field }}
{% endfor %}
Now I want to know the datatype of fields.if field type is Datetimeinput then I want give one button beside to it to get JavaScript calender.
i want do like as below but I am not able get it
{% for field in form %}
{% if field.field.required %}
<span class="red">*</span>
{% endif %}
{% if field.field_type == 'Datetimeinput' %}
{{ field.label }}:
{{ field }}
<label>From :</label><input type="text" name="from1" class="txtbox"><input type="button" value="Cal" onclick="displayCalendar(document.forms[0].from1,'yyyy-mm-dd',this)">
{% else %}
{{ field.label }}:
{{ field }}
{% endif %}
{% endfor %}
Help me out thanks in advance.
Make a template tag. Depending on Get type of Django form widget from within template. I used this solution once or twice.
from django import template
register = template.Library()
#register.filter('klass')
def klass(ob):
return ob.__class__.__name__
In template:
{{ field.field.widget|klass }}
Will return field class name to be used in your if statements.
You could write a custom template filter for that.
#register.filter
def fieldtype(obj):
return obj.__class__.__name__
Docs on custom filters: link
However, why not just render fields without looping over them?
{{ form.birthdate }}
Then you'll know for sure what is what.