Passing multiple variables from dropdowns using Django/Python - python

I'm learning Python. I would like display results based on multiple selections from dropdowns.
The following code is working and returning the correct results for one dropdown. I would like to add at least one more dropdown, hopefully multiple, but I cannot fathom how to achieve passing and returning the multiple vars.
Form code
<p> <form method="get" action="">
<select name="search3">
{% for cs in course_list %}
<option value="{{ cs.pk }}">{{ cs.name }}</option>
{% endfor %}
</select>
<br/><br/>
<input type=submit value="Find staff" />
</form>
</p>
{% if employee_list %}
{{ employee_list.count }} employees matching : <strong>{{ course }}</strong><br/><br/>
{% for e in employee_list %}
{% if not e.img1 %}
<img src="/media/images/null_image.png" width="35" title="{{ e.first_name}} {{e.surname }}" />
{% else %}
<img src="{{ e.img1.url }}" width="50" title="{{ e.first_name}} {{e.surname }}"/>
{% endif %}
<a href="/employee/{{ e.slug }}" target="_blank" rel="popup" title="Details...<br />
{% for det in employee_list.employeedynamic_set.all %}
{{ det.mobile }}<br />
{{ det.depot }}<br />
{% endfor %}
"> {{ e.first_name }} {{ e.surname }}</a><br/>
{% endfor %}
{% else %}
<p>No employees matched: <strong>{{ course }}</strong></p>
{% endif %}
views.py code
# function to list training courses and participating uindividuals.
def CourseView(request):
course_list = Course.objects.all().order_by('name')
if 'search3' in request.GET:
search3 = request.GET['search3']
course = Course.objects.get(pk=search3)
else:
search3 = None
course = None
return render_to_response("course_display.html", {'course':course, 'course_list': course_list, 'search3': search3 })
Is it possible to add an another dropdown option or even multiple dropdown options and get a result. Or am I barking up the wrong tree?
UPDATE.
In order to pass one or multiple variables back to my function the code is simply the following
def CompetencyCheck(request):
course_list = Course.objects.all()
if 'search3' in request.GET:
search3 = request.GET.getlist('search3')
course = Course.objects.filter(id__in=search3).distinct().order_by()
.get() allows only the last variable passed from the web page form to be read/passed back to the function.
On the other hand, the .getlist() allows one or multiple vars to be passed back. Great, part the of problem solved.
The list of names being returned is unique in that if var1 returns 4 names and var2 returns 8 names and both vars have one common name, a list of 11 names in returned.
I want only the common names to both vars returned, i.e. 1 name to be returned.
Any ideas?

request.GET in your view is a dictionary; by doing request.GET['search3'] you're just accessing the value of its search3 key. So in your template, you should be able to just add other <select> elements with their own names and retrieve their submitted values from request.GET in the same way.

you can query using course_list only.
for example:
course_list = Course.objects.all().order_by('name')
var1 = request.POST.get('var1')
if var1:
course_list = course_list.objects.filter(some = var1)
var2 = request.POST.get('var2')
if var2:
course_list = course_list.objects.filter(some = var2)

Related

Jinja2 ValueError: too many values to unpack (expected 2)

{%for feed,tim in feeds,time %}
{% set nickname = feed.nick %}
{% set like = feed.like %}
{% set text = feed.text %}
{% set today = tim %}
{% set postid = feed.postid %}
{% set photo = feed.photo %}
{% set profile = feed.profile %}
{%for cmt in cmts %}
{% set nickname = cmt.nick %}
{% set cmt = cmt.cmt %}
{% set cmtid = cmt.cmtid %}
{% if cmtid == postid %}
<p class="description"><span>{{nickname}} </span> {{cmt}}</p>
{% endif %}
{% endfor %}
<div class="comment-wrapper">
<img src="../static/images/smile.PNG" class="icon" alt="">
<input type="text" class="comment-box" id='cmt' placeholder="Add a comment">
<button class="comment-btn" onclick=cmt_write()>post</button>
</div>
</div>
</div>
{% endfor %}
succeeded in executing the for statement with one list in jinja2,
but we have to use two lists.
i try 2 list ( feeds, time ) use in jinja2
how to jinja2 for loop from jinja2
Is there a way to use two lists in jinja2?
If you want to loop over a combined list of two lists (of same length) you have to apply zip function on them. E.g.:
def view_function():
feeds = [...]
time = [...]
feeds_and_time = zip(feeds, time)
# Looks like this: [('feed_1', 'time_1'), ('feed_2', 'time_2')]
Then pass this new feeds_and_time variable to the render function. And in the template, modify the loop:
{% for feed,tim in feeds_and_time %}

How i can to relate 2 Django ChoiceFileds

I have a problem in my project.
I want to create a ChoiceField to be categories and subcategories in the models.py file. Well, it must be that I chose the first choice and the second according to the index of the place. For example, like the car and its models, according to the brand I chose in the First ChoiceField, you look at its models in the second field. İn Django.
Welcome to Stack Overflow! I've done this in the past by creating my own widget, a CategorizedSelect, with some jQuery. Here's the code and comments:
my_app/widgets.py:
from django.forms.widgets import Select
class CategorizedSelect(Select):
"""
This widget takes a dict of categories and a list of dicts for items.
It renders two drop downs, the first of the categories, which then
populates the second drop down with the items in that category via
the jQuery chained library.
For example:
# Prepare data for chained dropdown field for security
intl_stocks = Security.objects.filter(
active=True,
security_type__short_name="INT",
).select_related(
'exchange',
).order_by(
'exchange__name', 'name',
)
exchanges, securities = OrderedDict(), []
for intl_stock in intl_stocks:
exchanges[intl_stock.exchange.id] = intl_stock.exchange.name
securities += [{'category_id': intl_stock.exchange.id, 'id': intl_stock.id, 'name': intl_stock.name}]
security = forms.ModelChoiceField(
queryset=Security.objects.none(),
widget=CategorizedSelect(
categories=exchanges,
items=securities,
),
)
"""
template_name = 'my_app/widgets/categorized_select.html'
def __init__(self, attrs=None, categories=None, items=None):
super().__init__(attrs)
self.categories = categories
self.items = items
def get_context(self, name, value, attrs):
context = super().get_context(name, value, attrs)
context['categories'] = self.categories
context['items'] = self.items
return context
my_app/templates/my_app/widgets/categorized_select.html:
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-chained/1.0.1/jquery.chained.min.js"></script>
<select id="chain_select_{{ widget.name }}_id" name="chain_select_{{ widget.name }}" class="form-control">
<option value="">--</option>
{% for id, name in categories.items %}
<option value="{{ id }}">{{ name }}</option>
{% endfor %}
</select>
<br>
<select name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>
<option value="">--</option>
{% for item in items %}
{% comment %}
If we match the initial value set by the Django form, we need to
set SELECTED on the child, and use jQuery to force the category parent
drop down to the proper selection as well. We will force item.id to a
string since the widget.value.0 is always a string.
{% endcomment %}
{% if item.id|stringformat:"i" == widget.value.0 %}
<option value="{{ item.id }}" class="{{ item.category_id }}" SELECTED>{{ item.name }}</option>
<script>
var selected_category_id = {{ item.category_id }}
</script>
{% else %}
<option value="{{ item.id }}" class="{{ item.category_id }}">{{ item.name }}</option>
{% endif %}
{% endfor %}
</select>
<script>
$("#{{ widget.attrs.id }}").chained("#chain_select_{{ widget.name }}_id");
if(selected_category_id) {
$('#chain_select_{{ widget.name }}_id').val(selected_category_id).change();
$('#chain_select_{{ widget.name }}_id').prop("disabled", true);
$('#{{ widget.attrs.id }}').prop("disabled", true);
}
</script>
The example in the comment shows how this was done for a set of international stocks; the international exchanges were the categories, with the stocks themselves being the items. Good luck!

Django query function not properly filtering for user type

I am trying to filter all my users and select ones that have user is teacher = True and user has instrument1 ='Cello'. My function is displaying all the users, instead of just the teachers, that have user has instrument1 ='Cello'. I am wondering how to properly write my function so that it selects teachers as well. If users are a teacher they are saved in my database with a user.teacher=1, else user.teacher=0.
views.py
def teacher_list(request):
data = User.objects.filter(Q(teacher=True) and Q(instrument1='Cello'))
form = TeacherProfileForm()
context = {'form' : form, 'data' : data}
return render(request, 'view/teacher_list.html', context)
HTML
<div class="desktop">
{% for user in data %}
<div class="row">
<div class="col-md-4 left">
<img src="{{ user.avatar.url }}" height="170" width="170">
</div>
<div class="col-md-4 mid">
<h3>{{ user.first_name|capfirst }} {{ user.last_name|capfirst }}</h3>
<h5>{% if user.instrument1 != "" %}{{ user.instrument1|capfirst }}{% endif %}{% if user.instrument2 != ""%}, {{ user.instrument2|capfirst }}{% endif %}{% if user.instrument3 != "" %}, {{user.instrument3|capfirst }}{% endif %}{% if user.instrument4 != "" %}, {{ user.instrument4|capfirst}}{% endif %}{% if user.instrument5 != "" %}, {{ user.instrument5|capfirst }}{% endif %}</h5>
<p>{{ user.bio|capfirst }}</p>
</div>
<div class="col-md-4 right">
<br />
<x-star-rating value="5" number="5" id="rating"></x-star-rating>
<br />
<br />
<br />
<a href="{% url 'view:profile' user.id %}">
<button type="submit" name="submit" class="btn blue_button">Book Lesson</button>
</a>
</div>
</div>
<hr />
{% endfor %}
In short: Do not use the and operator to combine Q objects. In Python, the and operator returns one of the operands, depending on the truthiness of the first one. Use &, or, like here, you can simplify the expression.
Background: Do not use and to combine two Q objects. and in Python is a function that inspects the truthiness of the first operand and if that one is False then returns the first one, otherwise it returns the second operand. Here this thus means that:
>>> Q(teacher=True) and Q(instrument1='Cello')
<Q: (AND: ('instrument1', 'Cello'))>
whereas:
>>> Q(teacher=True) & Q(instrument1='Cello')
<Q: (AND: ('teacher', True), ('instrument1', 'Cello'))>
Indeed, since Q objects with a condition have truthiness True, the Q(teacher=True) and Q(instrument1='Cello') expression will retrurn the second operand, so Q(instrument1='Cello')
In fact you do not need Q objects in the first place, you can query like:
def teacher_list(request):
data = User.objects.filter(teacher=True, instrument1='Cello')
form = TeacherProfileForm()
context = {'form' : form, 'data' : data}
return render(request, 'view/teacher_list.html', context)

Django store information in one variable

I have different pages with items, each item has its own price which I would like to store in one variable and then send all of them to paypal payment form.
So question is,how can I store information about price from different pages? I tried sessions but nothing happens.
<article>
{% block content %}
<span>Kuriame mieste registruojiesi</span>
<select>
{% for item in upsell.skaidyti_miestus %}
<option value="{{item}}">{{ item }}</option>
{% endfor %}
</select>
{%if upsell.prideti_upsell_nr_2_taip%}
<button onclick="location.href='/upselliai/{{upsell.upsell_nr2_taip.id}}/pirkti/'">Taip, pridÄ—ti prie bendros sumos</button>
<!-- {#prideti kaina paimti ID} -->
{% else %}
<button onclick="location.href='/pirkejas/apmoketi'">Taip, pridÄ—ti prie bendros sumos</button>
{%endif%}
{%if upsell.prideti_upsell_nr_2_ne%}
{{ upsell.atsisakymo_tekstas }}
<!-- {#prideti kaina paimti ID} -->
{% else %}
{{ upsell.atsisakymo_tekstas }}
{%endif%}
{% endblock %}
</article>
my views.py
def pirkti_upsell(request, upsellis_id = 1):
#if 'pirkejas_id' in request.session:
session_pirkejas_id = request.session['pirkejas_id']
Upsellis.objects.get(id = upsellis_id).sum(session_pirkejas_id)
return render_to_response("upsell_template.html", {"upsell": Upsellis.objects.get(id = upsellis_id)})

Get POST from multiple dropdowns in one form

Have a form where user can change name on attribut Name and change which attribut a is connected to (attribut b).
Template:
"<form id="form1" name="form1" method="post" action="/define_a/{{c.id}}/edit/">
{% csrf_token %}
{% endblock %}
{% block buttons %}
<p><input type="submit" value="Save" /> Cancel
</p>
{% endblock %}
{% block a_rows %}
{% for a, a_form in a_list %}
<tr><td><img class="icon" src="{{images_dir}}/delete-icon.png"
onclick="javascript: return confirmDelete_name('Are you sure?
This will delete the stuff and all associated information.
The removal happens immediately and cannot be undone.', '{{a.id}}', 'delete');" />
</td><td>{{a_form.name}}</td>
<td>
<select name= "test">
<option value = "Null">None</option>
<option value = "{{a_form.id}}" selected>{{a.b}}</option>
{% for a, a_form in a_list %}
<option value = "{{a_form.id}}">{{a.name}}</option>
{% endfor %}"
View:
Checking that it is a post and that it is valid.
post = [myForm(request.POST, instance = test) for a in a's];
for p in post :
if not new_t.b == p:
if p == 'None':
new_t.b = None;
else:
new_t.b = p;
But i can't get all the values from the dropdown in the post.
I get all a.name in but only one value from the dropdown, sometimes I don't get any value at all.
Any ideas?

Categories

Resources