Issues with feeding data into database when using for loop - django

In my template I have used for loop for some fields
<div class="register_div">
<p>Title:</p>
<p>{{ form.title }}</p>
</div>
<div class="register_div">
<p>Upload Images :</p>
<p>{{ form.image }}</p>
</div>
{% for item in product %}
<div class="register_div">
<p>{{ item.Name }} <input type="text" name="custom[{{item.id}}]"/></p>
</div>
{% endfor %}
<div class="register_div">
<p>Price:</p>
<p>{{ form.price }}</p>
</div>
As code shows there is one field which using for loops if that field has three records then in the form it shows three text boxes so that user can feed data to all three fields, but my table in database have only one col for his id and one for his vaue.
so how i can feed data to my database so that if user feels all three text boxes then it can be easily stored in to my database.
If it is possible by three duplicate entry for title , image and price then its ok,But it should have take three different item name and there corresponding value.
e.g.
if there are three item names comes out from for loop then what user see is.
title
image
item 1 ---> its vaue
item 2 ---> its vaue
item 3 ---> its vaue
price
now it should we store in database like
id title image item_name item_value price
1 asd a.jpg item1 value 1 1111
2 asd a.jpg item2 value 2 555
3 asd a.jpg item3 value 3 789
or there is any another efficient way to do this please let me know

The "efficient way" to do this is to make sure your models match this data, and then use Model FormSets.
If, for some arcane reason, you don't want to define models to match your data, you can use normal formsets instead.
If you aren't familiar with Django forms, I highly recommend you try to understand those first. This answer by yours truly has a fairly comprehensive walkthrough on django forms.

Related

Javascript returns the same id for every iterated element in html

I have website which i build using Django where every visitor can leave their message or press a like button on any comment. I would like to show how many of a comment has been liked by the user.
As each comment is unique, I think to access the comment_id attribute which I wrote in the html part. I then use a simple js function to retrieve the id. The problem is, although html is showing unique id for each comment, Javascript still return the same id, that is the id of the first comment that is written on the page. Can someone tell me where is my mistake in this case?
Thank you in advance for your help.
my code in js:
function likeButton(){ let el = document.getElementById("chat-icon-thumbsup") alert(el.attributes["comment_id"].value) };
my html code, which is looping the datatable records "comment" from a datatable in Django framework:
{% for comment_ in comments %} <strong> {{ comment_.name }} - {{ comment_.date_added }} <button><i comment_id="{{ comment_.comment_id }}" id="chat-icon-thumbsup" class="fa fa-fw fa-thumbs-up" title="like this comment" onclick="likeButton()"></i></button> {{ comment_.nr_of_likes }} <br/> </strong> {{ comment_.body }} <br/> {% endfor %}
image 1: here you see when I inspect the DOM elements, it retrieves different "comment_id"
enter image description here
image 2 and image 3: every button pressed in the comment line alerting the same id, which is 16
enter image description here
I tried to google any possible same problem, but no solutions found
In HTML an ID should be unique on every page. JS will therefore only return the first field it finds with that ID. What you should be using is a class-name that defines your field, like this for example:
<i id="comment_{{ comment_.comment_id }}" class="fa fa-fw fa-thumbs-up chat-icon-thumbsup" ....>
Then you can use document.getElementsByClassName("chat-icon-thumbsup"), there is a reason why it's called getElementById as singular and not getElementsById

Django template: not showing product if the product has already been shown

Really sorry about the disturbing title, my case is a bit tricky to explain (and i'm not that good in english). I'll try to do my best.
I have a django app where users can save paired food products - the first is the "product_to_substitute" and the second one is the "substitute_product". But the thing is that the "product_to_substitute" can actually have multiples "substitute_product". Here is what my "favorites" DB table can looklike (numbers are products IDs) :
product_to_substitute_id
substitute_product_id
3
5
3
50
3
45
58
124
12
98
So what I would like to do on my favorites html page is to show one section with the "product_to_substitute" and a second section that shows all the "substitute_product" that are paired with the "product_to_substitute".
Here is a little bit of my code:
{% for product in favorites %}
{% if product.product_to_substitute %}
### Block where I want to show only once the product to substitute ###
<section style="background-image: url('{{ product.product_to_substitute.image_url }}')">
...
</section>
### Block where I want to show all the substitutes products that are paired with the product to substitute ###
<section>
<img class="img-fluid" src="{{ product.substitute_product.image_url }}" alt="..." />
</section>
{% endif %}
{% endfor %}
By looking at the example table, my code right now would show 5 paired blocks of "product_to_substitute" and "substitute_product", which is not I want. I would like to have 3 paired blocks. One for the product_to_substitute_id "3" and two for the product_to_substitute_id "58" and "12".
I hope that makes sense...
Thanks!

Filtering in Django html template

I'm passing two models into the html template
listings - has all the properties of the listed items (like on an auction site)
bids - has a listing field as a foreign key to link to listings (and the user who made a bit, plus the amount)
I'm looping through the listings items to display all their attributes of the individual listings on cards (that works nicely)
I want to display the bid that belongs to the individual listing if there is any (only the highest bid is passed on for each listing in the 'bid' queryset)
I suspect the problem is with the syntax here, but can't find a solution.
<p>{{bids.filter(listingID=listing.listingID)}}</p>
The error message:
Could not parse the remainder: '(listingID=listing.listingID)' from 'bids.filter(listingID=listing.listingID)'
This is my HTML template
{% extends "auctions/layout.html" %}
{% block body %}
<h2>Active Listings</h2>
<div></div>
{%for listing in listings%}
<div class="listingItem">
<div class="listingLeft">
</div>
<div class="listingRight">
<a href={{listing.id}}><h4>{{listing.listingName}}</h4></a>
<p>{{listing.listingDesc}}</p>
<p>Current bid: ${{listing.listingFirstBid}}</p>
<p>{{bids.filter(listingID=listing.listingID)}}</p>
<p class="created">Created: {{listing.listingCreated}}</p>
</div>
</div>
{%endfor%}
{% endblock %}

How to get the information of a specific object that is retrived from a django model using for loop

I have a boostrap slider which is populating using for loop and django model objects now I want that if I click on any of the slider object then it show me the information related to that specific object which I clicked the Information must be retrived from the django model.
Thanks In Advance please help me to get out of this situation ..
Here is my code which is populating the slider. I want when I clicked on any object in the loop it should only show me the information related to that object or How can I access and display the name and description of individual product diplayed using for loop in django
{% for product in products %}
<div class="services-block-two">
<div class="inner-box">
<div class="image">
<img src="/{{ product.prd_logo }}" alt="" />
</div>
<div class="lower-content">
<h3>{{ product.prd_name }}</h3>
<div class="text">{{ product.prd_des }}</div>
Read More <span class="arrow flaticon-next"></span>
</div>
</div>
</div>
{% endfor %}
here is a image of product i want to display the name,logo and description of the product on different web page when user would click on that product

django template fetch ID or Name for selected dropdown value

I am building a new order form and there are few dropdowns on my HTML page. I would like to build a number based on the dropdown selection. Below is the select tag code which displays the values from the database.
<div class="col-md-4 mb-5">
<select class="selectpicker show-tick" data-width="fit" id="test" data-header="Choose Cost Type Description" title="Choose Cost Type Description">
{% for row in odrrows %}
<option name="costtypedesc" value = "{{ row.odr_cost}}">{{ row.odr_cost_desc}}</option>
{% endfor %}
<select>
</div>
Is there a way fetch row.odr_cost value to same page instead of row.odr_cost_desc that too without submit? I know we can fetch the row.odr_cost_desc value using javascript but not sure about row.odr_cost.
Here is the sample table1
My dropdown displays drinks, liquids and XYZ. When users selects drinks, I would like to display number 52 in the same webpage(maybe with p tag) instead of drinks.