Here I iterate data from my database in mongodb i just want that when my add to cart button is clicked it gets stored into another database named cart and gets displayed on another page
{% for data in product_data %}
<div class="shop-item" >
<span class="shop-item-title" id="title-item">{{ data.title }}</span>
<input type="image" class="shop-item-image" id="image-item" src={{ data["img_file"] }} onclick="takethatpage();">
<div class="shop-item-details">
<span class="shop-item-price" id="price-item">{{ data["price"]}}</span>
<button class="btn btn-primary shop-item-button" type="button">ADD TO CART</button>
</div>
</div>
{% endfor %}
I will not spoon feed but let me tell you the approach for it.
Firstly you would need an API implemented at the server end to accept products to be added to the cart for the respective user.
Secondly, for your template there needs to be an AJAX request fired each time the person click on the add to cart button.
Related
I have a flask html page that retrieve applicants information from the database sqlalchemy, I want to click on a name of applicant from the first page and another page open with specific information for this applicant.
I sent a query for the first page and it works like I want, but I struggled in the second page, I did not know how to send that one record
this is the code of the first page, applicant-report page is the second page
{% for report in reports %}
<div class="rec_box">
<a class="text" href="/applicant-report">
{% for a in applicant %}
{% if a.phone == report.applicant_phone %}
{{a.name}}
{% endif %}
{% endfor %}
</a>
<p class="rec_small">O: {{(report.op*100)}}%</p>
<p class="rec_small">C: {{report.co*100}}%</p>
<p class="rec_small">E: {{report.ex*100}}%</p>
<p class="rec_small">A: {{report.ag*100}}%</p>
<p class="rec_small">N: {{report.ne*100}}%</p>
</div>
{% endfor %}
Pass parameters between pages is usually using the URL, since this method is showed to the end user, I suggest not include any passwords or something like that
In the link you will redirect the user ends in something like
<a class="text" href="/applicant-report?phone={{a.phone}}">
Where the a.phone is the value of your variable. To read this url in the other page, you use something like
applicant_phone = request.args.get('phone')
applicant = session.query(Applicant).filter_by(phone=applicant_phone).first()
If you want to add another parameter to the url you concat using & between them, like
<a class="text" href="/applicant-report?phone={{a.phone}}&{{a.contact}}">
And so on.
Hope you find this usefull
I have a problem with trying to get a response from my HTML page using Django (admin).
I have a pretty simple div = contenteditable and need to pass data from this div back after the submit button was clicked.
Everything, including choosing selection and opening the intermediate page works fine. But when I tapped submit button, the condition if "apply" in request.POST failed to work.
Please, tell me, what I'm doing wrong?
This is my Django admin:
class QuestionAdmin(AnnotatesDisplayAdminMixin, admin.ModelAdmin):
def matched_skills(self, question):
return ', '.join(s.name for s in question.skills.all())
def update_skills(self, request, queryset):
if 'apply' in request.POST:
print("something")
skills = []
for question in queryset:
skills.append(self.matched_skills(question))
return render(request,
'admin/order_intermediate.html',
context={'skills': skills})
update_skills.short_description = "Update skills"
This is my order_intermediate.html page:
{% extends "admin/base_site.html" %}
{% block content %}
<form method="post">
{% csrf_token %}
<h1>Adjust skills. </h1>
{% for skill in skills %}
<div>
<div id="title" style="margin-left: 5px" contenteditable="true" > {{ skill }} </div>
</div>
{% endfor %}
<input type="hidden" name="action" value="update_status" />
<input type="submit" name="apply" value="Update skills"/>
</form>
{% endblock %}
Actually, request.POST is an HttpRequest object. For getting available keys in the body of the request, you need to use "request.POST.keys()" method. So, you can simply change your condition to:
if 'apply' in request.POST.keys():
print("something")
In my knowledge, you can not send div content with form submit. However you can use input tag with array in name attribute for this. This will send an array as post variable when submit
First, send skills as a enumerate object from your views
return render(request, 'admin/order_intermediate.html', context={'skills': enumerate(skills)})
Then edit your html to this (Note: if you have css in title id, change it to title class)
{% for i,skill in skills %}
<div>
<input class="title" name="skill[{{ i }}]" value="{{ skill }}" style="margin-left: 5px">
</div>
{% endfor %}
and handle array with any action you want to perform in update_skills()
for skill in request.POST.getlist('skill[]'):
# your code
I am trying to have a button in HTML that removes a row in the database when I click it. This is for a flask app.
here is the HTML:
<div class="container-fluid text-center" id="products">
{% for product in productList %}
<div class='userProduct'>
{{ product.title|truncate(30) }}
<h4 class="currentPrice">${{ product.currentPrice }}</h4>
<h5 class="budget">Budget: {{ product.userBudget }}</h5>
<form action="{{ url_for('delete', id=product.id) }}">
<button class="btn btn-sm btn-primary" type="submit">Remove from Wishlist</button>
</form>
</div>
{% endfor %}
</div>
And here is the route in the python file
#app.route('/delete/<id>')
#login_required
def delete(id):
remove_product = Product.query.filter_by(id=int(id)).first()
db.session.delete(remove_product)
db.session.commit()
return redirect(url_for('dashboard'))
Is there anything wrong I am doing with url_for? I want to pass the id from the button to the python file so I can determine which entry to delete from the database.
You're probably passing ID as an integer and your route is expecting a string. Use this instead:
#app.route('/delete/<int:id>')
I just encountered the same error,
In my case it was caused by some values being None in the database.
So maybe check whether product.id has missing values.
To handle this, I needed an extra line, which would look something like this for you:
#app.route('/delete/<id>') # current line
#app.route('/delete', defaults={'id': None}) # added line which handles None
In delete(id) you then have to handle the case where id is None:
if id is None:
... # what should happen if id is None
I need some help conditionally rendering data in a modal pop up window on my site.
What I want to do:
When the user clicks on the "make reservation" button, I want to display this in the modal window
<h3 style="margin-top:20px;">Choose dates</h3>
<div style="margin-top:20px;" class="pick-dates-div">
<form method="GET" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="form-btn save btn btn-default">Make A Reservation</button>
</form>
<button style="margin-top: 25px;" class="btn-primary btn-block btn-lg" data-toggle="modal"
data-target="#inquiryModal">More
Questions ?</button>
</div>
Then the user can pick the dates from the date picker and press the "make a reservation" button ( which is a GET request ), the page refreshes and I want to display only this in the same modal window :
<h1>Start Date: {{ date_start }}</h1>
<h1>End Date: {{ date_end }}</h1>
<h1>Price Per Day: ${{ price_per_day }}$</h1>
<h1>Total: ${{ total_price }}$</h1>
<form method="POST" class="post-form">{% csrf_token %}
{{ form.as_p }}
<button href="www.facebook.com" type="submit" class="form-btn save btn btn-default">Confirm Reservation</button>
</form>
After that the user submits the form ( POST request ) and I want to display a text :
<h3> Thank you for your reservation </3>
What would be the ideal way to achieve this ?
Thank you stack
The ideal way to achieve this is by using JavaScript.
One of the many methods this could be achieved is by rendering all the three views inside separate containers in a modal and then hiding the next two using javascript.
You can use element.style.display = 'none' to hide and
element.style.display = 'block' to show the content inside the element container.
Once the user clicks on "make reservation" button, hide/show the required containers to achieve the desired result.
Do not forget to secure your website by using proper validation on the server end. Hope that helps!
How we can generate fields dynamically in flask with wtf forms without using java script DOM to add fields like here in this link:
http://formvalidation.io/examples/adding-dynamic-field/
for example adding a field or when we delete the field to delete a set on database like in the images down here.
Form with one row:
Form with one row
Form with multiple rows:
Form with multiple rows
{{ form.hidden_tag() }}
{{ form.Book }}
{{ form.ISBN }}
{{ form.Price }}
<button type="button" class="btn btn-default addButton">
<i class="fa fa-plus"></i>
</button>
<button type="submit" class="btn btn-default">Submit</button>
And my python class form
class BookForm(FlaskForm):
isbn = StringField("ISBN",[validators.DataRequired("Please enter your ISBN number.")])
title = StringField("Titile",[validators.DataRequired("Please enter your Book Title.")])
price = FloatField("Price")
I saw some people proposing some class_ thing but i didn't understand that.