test by date in django and javascript - javascript

i need to do a test before setting values in my list so ,
i get a selected value from a combobox and i want to do a test between this value and a variable in my data base .
the source code is :
first in my template :
<select class="form-control" id="date_select" onchange="displayAll();">
<option value="">----- </option>
{% for v in v_date %}
<option id="" value="{{ v.date}}">{{ v.date}}</option>
{% endfor %}
</select>
so i am gonna do a test with the selected value from the combobox in javascript
this is the source code in js
var date_test = document.getElementById('date_select').value ;
var locations = [
{% for v in vs %}
{% if v.date == date_test %}
['okok',{{ v.latitude }},{{ v.longitude}}],
{% endif %}
{% endfor%}
]
the problem is that my source code doesn't work in IF conditions , i don't know if this line is correct {% if v.date == date_test %}
['okok',{{ v.latitude }},{{ v.longitude}}],
{% endif %}

You try to make a comparaison with = operator which does not exits.
You have to use == operator.
Example from Django documentation :
{% if somevar == "x" %}
This appears if variable somevar equals the string "x"
{% endif %}

This question is very similar to another that I've answered to.
Here is link to answer.

Related

Unable to pass variant data into shopify form

I am trying to pass through variant JSON data using a data-variants attribute into an add to cart form, but i cant refactor correctly to work with Shopifys more up to date form syntax.
<form id="add-to-cart-form" action="/cart/add" method="post" enctype="multipart/form-data" data-variants="{{variants_with_quantity_json | url_param_escape }}"></form>
{% form 'product', product, data-product-form: '', data-product-handle: product.handle, data-enable-history-state: 'true', id: "add-to-cart-form" %}
The data variant attribute simply outputs the object with no data in my version.
But i need the data to bee accessible on the form like so;
I am trying to add the invetory qty to the product.variants.json and need to figure out how {{ variants_with_quantity_json }} can be passed to the form.
{% assign variants_with_quantity_json = product.variants | json %}
{% unless variants_with_quantity_json contains 'inventory_quantity' %}
{% for variant in product.variants %}
{% assign replace_hook_variant_id = '"id":' | append:variant.id %}
{% assign replace_id_plus_inventory = replace_hook_variant_id | append: ',' | append: '"inventory_quantity":' |
append: variant.inventory_quantity %}
{% assign variants_with_quantity_json = variants_with_quantity_json | replace: replace_hook_variant_id,
replace_id_plus_inventory %}
{% endfor %}
{% endunless %}
okay, you need to assign the data to a variable before pass it to {% form %} in Shopify
So you {% form %} look like this:
{% assign data_variants = variants_with_quantity_json | url_param_escape %}
{% form 'product', product, data-product-form: '', data-product-handle: product.handle, data-enable-history-state: 'true', id: "add-to-cart-form" data-variants: data_variants %}
or you may use the {% capture %} liquid code tag to get and pass the value
{% capture 'data_variants' %}variants_with_quantity_json | url_param_escape{% endcapture %}
{% form 'product', product, data-variants: data_variants %}
...
{% endform %}

How to get only checked elements from a form in shopify?

{% assign type_bonbon = "Sans sucre, Gélifié, Guimauve, Nougat, Acide, Doux" | split : ", " %}
<section class="page__content">
{% capture contact_form %}
<div class="contact">
{% form 'contact', class: 'contact__form' %}
<div class="form__control">
<label class = "form__label" for="contact__type_bonbon">Types de bonbons</label>
{% for type in type_bonbon %}
<div class = "checkbox">
<input type="checkbox" id="contact__type_bonbon" name="contact[type]" value="{{type_bonbon}}">{{type}}
</div>
{% endfor %}
</div>
{% endform %}
</div>
{% endcapture %}
</section>
Hello everyone,
I am struggling to Get every checked values within the checkbox.
Right now im getting only the last checked value
I know in PHP we would use the GET method in order to put the "checked" method to every checkbox checked, however, idk how to proceed in shopify.
Thank you again
I just checked and I was able to replicate the behavior that you mentioned above about receiving only the last checked value. I don't know if it is a bug or feature from Shopify, but you can use a workaround like below where each field is named differently. Another minor thing is the ID attribute needs to be unique while it generates the same in your for loop above. Besides, that for value it should have been value="{{type}}" instead of value="{{type_bonbon}}" as type_bonbon is an array.
{% assign type_bonbon = "Sans sucre, Gélifié, Guimauve, Nougat, Acide, Doux" | split : ", " %}
<section class="page__content">
{% capture contact_form %}
<div class="contact">
{% form 'contact', class: 'contact__form' %}
<div class="form__control">
<label class = "form__label" for="contact__type_bonbon">Types de bonbons</label>
{% for type in type_bonbon %}
<div class = "checkbox">
<input type="checkbox" name="contact[type-{{type}}]" value="{{type}}">{{type}}
</div>
{% endfor %}
</div>
{% endform %}
</div>
{% endcapture %}
</section>
Only this line of code is changed, to generate new field name for each type.
<input type="checkbox" name="contact[type-{{type}}]" value="{{type}}">{{type}}

How to pass the data through button using unique id in a dynamic table

i am using django and bootstrap and i want to pass data through button and show that data on bootstrap popup modal and i want that in each loop x.bots data should be passed uniquely so that i can access bot title ,bot id
using another loop at x.bots
{% for x in data %}
<td>{% if x.bots %} Show Bots {{ x.bots|length }}{% endif %}</td>
{% endfor %}
this is my script file
<script>
$(document).on('show.bs.modal','#myModal', function (event) {
var button = $(event.relatedTarget);
var recipient = button.data(x.bots);
var modal = $(this);
modal.find('.modal-title').text('Bots Title');
modal.find('.modal-body input').val(i);
})
</script>
but i tried alot using different ways but not getting the data
You can try something like this. Use an onclick javascript function and pass the parameters you want to use.
{% for x in data %}
<td>{% if x.bots %}<a href="#myModal" class="btn btn-primary btn-sm" data-toggle="modal" data="x.bots"
onclick="myFunction({{ x.bots.title }} , {{ x.bots.id }})"> Show Bots
{{ x.bots|length }}{% endif %}</a></td>
{% endfor %}
Then in the script implement the javascript function.
function myFunction(title, id) {
//write code here
}
It seems to me that var modal = $(this); will not result in an element since I don't see this defined anywhere.
Maybe change it to var modal = $('#myModal'); or var modal = event.target?
Change this
{% for x in data %}
<td>{% if x.bots %} Show Bots {{ x.bots|length }}{% endif %}</td>{% endfor %}
To This,
{% for x in data %}
<td>{% if x.bots %} Show Bots {{ x.bots|length }}{% endif %}</td>{% endfor %}

Adding a filter to format Date values dynamically in Nunjucks

How might I check if a value is a date in Nunjuck so that I can dynamically add a date filter to control how it displays.
For example:
{% if field.value === Date %}
It's a date. Apply Filter!
<input type="text" value="{{field.value | dateFilter }}">
{% else %}
Not a date. Don't apply filter!
<input type="text" value="{{field.value}}">
{% endif %}
Thanks in advance for any pointers on this.
If field.value is Date then getTime and other Date-methods is exists.
{{ field.value | dateFilter if field.value.getTime else field.value}}
or like that
{% if field.value and field.value.getTime %}
// It's a date.
{% else %}
// Not a date.
{% endif %}

keep original selected values after form submit python/flask

I have a two pronged question and would appreciate any advice.
1) I have a flask template with multiple forms in it. Each form presents the user with a list that is dynamically generated from a mongodb query.
name_list = [p['name'] for p in posts.find({'type': "server"})]
name_list.insert(0, "select")
This is then refernced in my html template (thank you again to the person who helped me with this loop on a previous question)
<select name="option" id="myselect" onchange="this.form.submit()">
{% for x in server_list %}
<option value="{{ x }}"{% if loop.first %} SELECTED{% endif %}>{{ x }}</option>
{% endfor %}
This selection is then passed back to python to be used in another db query and then present the user with another dropdown select box. However when the first form is submitted, the new page render means that that value is now lost on the html and appears blank. Is there a way to keep this choice persistent so when after the new page render it still appears?
2) Secondly, I would like to keep a running list of what options the user makes, however given the if, elif structure I'm using that variable looses state and can no longer used. Eventually I would like to present the user with two sets of these dropdown menus to generate to final db querys that I can compare and return the difference, however I can only do this if I can keep state of these values generated in the loops.
Please see full code below:
python:
from flask import render_template
from flask import request
from flask import Response
from app import app
from pymongo import MongoClient
#app.route('/', methods=['POST','GET'])
#app.route('/index', methods=['POST','GET'])
def index():
user = {'name': 'Bob'}
client = MongoClient('mongodb://localhost:27017/')
db = client['test-database']
collection = db.test_collection
name_list = []
posts = db.posts
name_list = [p['name'] for p in posts.find({'type': "server"})]
name_list.insert(0, "select")
select_list = []
#if request.form['submit'] == 'myselect':
if request.method == 'POST' and request.form.get('option'):
choice = request.form.get('option')
select_list.append(choice)
sub_name_list = [q['type'] for q in posts.find({'name': choice})]
return render_template("index.html",
sub_server_list=sub_name_list)
elif request.method == 'POST' and request.form.get('option1'):
choice1 = request.form.get('option1')
select_list.append(choice1)
return render_template("index.html",
title='Database selector',
user='Person',
choiced=choice1,
total_choice=select_list)
return render_template("index.html",
title='Database selector',
user='Person',
server_list=name_list)
html/jinja:
<html>
<head>
<title>{{ title }} - Test</title>
</head>
<body>
<h1>Hi, {{ user }}!</h1>
<h2>Database selector</h2>
<h3><table><form action="" method="post">
<td>
<label>Select1 :</label>
<!--<select name="option" width="300px">-->
<select name="option" id="myselect" onchange="this.form.submit()">
{% for x in server_list %}
<option value="{{ x }}"{% if loop.first %} SELECTED{% endif %}>{{ x }}</option>
{% endfor %}
</select>
</td>
</form></table></h3>
<h3><table><form action="" method="post">
<td>
<label>Select2 :</label>
<select name="option1" id="sub_myselect" onchange="this.form.submit()">
{% for y in sub_server_list %}
<option value="{{ y }}"{% if loop.first %} SELECTED{% endif %}>{{ y }}</option>
{% endfor %}
</td>
</form></table></h3>
<h3>Here is the choice: {{ choiced }}</h3>
<h3>Here is the choice: {{ total_choice }}</h3>
</body>
</html>
Any pointers or ideas would be greatly appreciated.
Have a look at sessions. You might have to change the way you store your data though, maybe by finding an appropriate name for choiceand choice1 and storing them in a dict.

Categories