I'm trying to create an AddtoCart and Checkout functionality using python flask and flask-sqlalchemy. I consider myself to be a beginner at web development in general. How do I take a product item and add it to a cart as a cart item using a button? I would also like to calculate the total price of cart items.
So far I created two models (ProductItem,CartItem). I created successfuly 2 ProductItems (dummy data) and was able to display them in a view using a for loop with jinja2 template. I've tried to create a function to select the product and add it to the cart but I couldn't figure out the way on how to make the add to cart button functionality work.
Thanks in advance!!
class ProductItem(db.Model):
__tablename__='products'
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(64),unique=True)
descr = db.Column(db.Text,unique=True,nullable=True)
price = db.Column(db.Float,nullable=False)
img = db.Column(db.String(64),unique=True)
cartitems = db.relationship('CartItem', backref='Product')
def __repr__(self):
return '<ProductName %r>' % self.name
class CartItem(db.Model):
__tablename__='cartitems'
id = db.Column(db.Integer,primary_key=True)
# adding the foreign key
product_id = db.Column(db.Integer, db.ForeignKey('products.id'))
#app.route('/')
def index():
products = Product.query.all()
return render_template('home.html',products=products)
def getproductitem():
itemid = product.id
productname = product.name
productname = CartItem(product_id=itemid)
db.session.add(product)
db.session.commit()
----------------html jinja----------
{% for product in products %}
<div class="product-item">
<h3>{{ product.name }}</h3>
<img src="static/img/products/{{ product.img }}" alt="" width="200px" height="200px">
<p> {{ product.price }}</p>
<button onclick="getproductitem()" type="button" class="btn btn-primary">Add to Cart</button>
</div>
{% endfor %}
Edit
Realised I didn't answer the question about the button. Seems like you're trying to call you python function from the html (unless you have a javascript function also in your front end template).
Your python lives on the server and your html/javascript will be on the client browser - you need to make them communicate by sending a HTTP request from your page to the server, you can't call functions directly.
Server:
#app.route('/cart/<int:product_id>', methods=['POST'])
def add_to_cart(product_id):
product = Product.query.filter(Product.id == product_id)
cart_item = CartItem(product=product)
db.session.add(cart_item)
db.session.commit()
return render_tempate('home.html', product=products)
add to your html:
<script>
function addToCart(productId) {
fetch('[your.local.host.]/cart/productId',
{method: 'POST'}
)
}
</script>
change the button:
<button onclick="addToCart({{product.id}})" type="button" class="btn btn-primary">Add to Cart</button>
Or something similar. Your page needs to talk to your server via HTTP requests.
Original answer about carts
It's probably not necessary to persist your cart in the database unless you really want your users to be able to access the same cart when logging in across devices, or you anticipate they will need to keep items there more long term.
Persisting will add unnecessary time to user requests (while you add/retrieve them) and that CartItem table will continue to get larger and larger and most rows will become redundant (unlikely people want to view their old shopping cart once they've bought products). One solution would be to also link the carts to a User table so you only have one cart per user (provided your users are logged in when shopping), or make sure you delete carts once they're bought or after a certain time period.
However, if you have no need to persist longer term, consider storing the product ids either in either
The flask session. Essentially a lightweight, in memory store on the server that is linked to a user and can be accessed during the request handling. See a tutorial on sessions here.
Inside a cookie. A cookie is stored in the browser (not the server) and usually signed with a key. This does not make them secure - it just means you can be sure no one has modified its content when you retrieve it on the server. See a tutorial here.
This article discusses a few drawbacks/merits to both approaches.
Related
I just integrated Razorpay payment gateway in my django project and i am new to this payment gateway. When i am initiate the payment razorpay show me an payment interface(like this image) and there is options for writing customer contact and email. Here is my question how can i get this two fields back contact and email when customer click on proceed button.
Here is my code:
Template file
<h2>Product Checkout</h2>
<p>Product: {{ order_obj.product.name }}</p>
<p>Total: {{ order_obj.total }}</p>
<form method="POST"> {% csrf_token %}
<script src="https://checkout.razorpay.com/v1/checkout.js"
data-key="rxxxxxxxxxxxxitP7h"
data-amount= {{order_amount }}
data-currency={{order_currency}}
data-buttontext="Pay with Razorpay"
data-name="xxxxxInc"
data-description="We are listning"
data-image="https://www.bihhs.in/wp-content/uploads/2020/05/jj-logo.png"
data-prefill.name="company name"
data-prefill.email="abc#gmail.com"
data-theme.color="#F37254">
</script><input type="hidden" custom="Hidden Element" name="hidden">
</form>
Views.py file
client = razorpay.Client(auth=("rzxxxxxxxaitP7h", "dZH5xxxxxxxxFmzG"))
payment = client.order.create(dict(amount=order_amount, currency=order_currency))
if payment:
order_obj.mark_paid()
print(payment)
print(payment.get('email'))
You should be getting an id in the response json. As per the docs here RazorpayAPI Reference and you can then get the details of the order with that ID. Then you create a payment with that order ID and then once payment is made you get a payment ID. There are methods to Fetch Payments with a Payment ID which should have these details if the user has filled it.
For your Reference, these are python method guidelines for Razorpay:
Payment Reference Python Razorpay
Here's a pseudo-code for the changes:
client = razorpay.Client(auth=("rzxxxxxxxaitP7h", "dZH5xxxxxxxxFmzG"))
order_details = client.order.create(dict(amount=order_amount, currency=order_currency))
if payment:
order_obj.mark_paid()
payment_id= client.order.payments(str(order_details["id"]))
payment_details= client.payment.fetch(str(payment_id)
payment_email= payment_details["email"]
print (payment)
print(payment_details)
I'm very new to Flask and WTForms, but what I'm trying to do is provide a user with a button to allow them to automatically upload a GIF to giphy.com. The GIF is located on the server and rendered to their browser on generate.html. Below the GIF is a Submit button that I intend to have launch an app.route to handle the upload process (opening a new tab which will eventually display a link to the uploaded GIF once that process completes).
Problem: The app.route for the uploading the gif (clicking the submit button) has no data assigned to its HiddenField (gifLocation), despite thinking I assigned it when generate.html was loaded.
My FileLocationForm code:
# class for sending along a file path used for uploading a gif
class FileLocationForm(FlaskForm):
gifLocation = HiddenField()
submit = SubmitField('Upload to GIPHY')
When the GIF is first created on my server, I run the following commands (in the app.route for generate.html):
form = FileLocationForm(gifLocation=gif_outfileloc)
return render_template('generate.html', gif_outfileloc=gif_outfileloc[7:], form=form)
At this point, gif_outfileloc exists and 100% points to a valid GIF on my server. The 2nd parameter's formating is because I'm displaying a file in the "static" folder, and need to remove "static" from the path, since it's added by flask in the img tag. That part is working.
The code for generate.html:
{% extends 'layout.html' %}
{% block content %}
<div class="gif_display center-align">
<img class="img-fluid mx-auto my-auto d-block" width="480" height="360" src={{ url_for("static", filename = gif_outfileloc) }}>
</div>
<form id='uploadGifForm' action = {{ url_for('generateGIFpage') }} target = '_blank' method='POST'>
{{ form.gifLocation() }}
{{ form.submit() }}
</form>
{% endblock content %}
We get a submit button on the page and clicking it does open a new tab, but it takes us home, instead of uploading to giphy.
Code for generateGIFpage:
#app.route("/upload", methods=["GET", "POST"])
def generateGIFpage():
if request.method == 'POST':
if request.form.get('gifLocation'):
gifLocation = request.form.get('gifLocation')
giphyobj = Giphy(API_KEY)
# response (below) is the URL for our giphy upload
response = giphyobj.upload([], gifLocation, username="QuoteGIFr")
return render_template('upload.html', response = response)
return redirect(url_for('homepage'))
Any help is appreciated. I've tried some variations on this, but I haven't had any success yet. If you need anymore code to make sense of what I'm doing, or need more information on what I'm trying to do, please ask.
I am new to nodejs/web app and I am trying to get data out from MongoDB.
My mongoDB has documents under collection "a"
{_id:("1"), "Question":"mcq1", "Answer":"one", "Keyword":"CLASS"}
{_id:("2"), "Question":"mcq2", "Answer":"two", "Keyword":"CLASS"}
{_id:("3"), "Question":"mcq3", "Answer":"three", "Keyword":"OVERLOAD"}
{_id:("4"), "Question":"mcq4", "Answer":"four", "Keyword":"OODP"}
I want to extract the data "Question": field_value, "Answer":field_value out using nodejs -> expressjs through a button and textbox form using the unique Keyword and to be displayed in a table form as below.
<tr>
<td><%= Question %></td>
<td><%= Answer %> </td>
</tr>
I have been able to get what i want with the monogodb shell using
db.a.find({"Keyword":CLASS},{"Question":1,"Answer":1,_id:0})
Currently the textbox and button onclick codes are below.
Input Keyword to search: <input type="text" id="searchBtn" value="">
<button type="button" onclick="alert(document.getElementById('searchBtn').value)">Click me!</button>
How can i extract the Question and Answer with the button click?
Using db.a.find({"Keyword":CLASS},{"Question":1,"Answer":1,_id:0})
i want to get a table in the form of
Question, Answer,Keyword
mcq1, one, CLASS
mcq2, two, CLASS
If you do a db query based on that keyword you'll get the occurrences in mongo, so you can do a form(GET/POST) field with the input and the button you already have. This will be caught in your express code as a route, there you can implement some basic code filling your search needs and the return value will be some simple data or an array if multiple match.
This is a basic search that one user (Miqe) once taught me here, first you need the query (or you can just put directly there) and later search in the mongo. But pay attention that your callback function will return the result, here is just a console.log() after returning the values you can assign them to a variable and pass through a template engine rendering in the html in the desire format.
query = {username: username, password: password}
dbo.collection('Users').find(query, function(err, user){
if(err) throw new Error(err);
if(!user)
console.log('Not found');
else
console.log('Found!');
})
Here is just the code to find in a collection named Users, you still need to join with the route and do a connection to the DB.
I'll leave a link who helped me a lot! The mongo documentation is still a good start too.
https://www.w3schools.com/nodejs/nodejs_mongodb_query.asp
i have an Angular Storefront app set up. I have a shopping cart functionality in place and a stripe "pay with card" button etc. pretty much looks like this:
<form action="/#/order" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="{{ stripeApiKey }}"
data-billingAddress=true
data-shippingAddres=true
data-amount="{{ amount }}"
data-name="StoreFront Name"
data-description="Custom-Made Jewellery"
data-image="../images/www/logo.png"
data-locale="auto">
</script>
</form>
Evrything up to this point is working fine. I submit the form and stripe returns the token but the form goes to the server following the route localhost/order (without the # symbol) instead of angular's localhost/#/order.
Why is stripe forcing this redirect? In other words why isn't angular capturing this return call?
Anyways. Then I create a route with Laravel to capture this and dump to inspect the returned data like so:
Route::post('/order', function($request){
dd($request);
});
Yep, data captured by stripe-generated form is returned except amount is missing... I mean everything including stripeToken, buyer's details such as: Name, Email, Billing and Shipping address are returned BUT detail regarding the amount is missing.
Is this normal or I'm I missing something?
Lastly currency is still showing the default: Where can I change currency from say USD to GBP?
Thanks in advance
1/ I don't think Checkout is forcing the redirect, but I don't know enough about Angular to explain what's going on, sorry.
2/ Yes, this is normal. The amount passed to Checkout in the data-amount configuration option is used for display purposes only. The actual amount that is charged is the one you pass in the amount parameter in the charge creation request in your server-side code.
If you need the amount to be user-specified (for instance, if you're taking donations), you'll need to add the amount to the form. Here is a simple JSFiddle to illustrate this case: https://jsfiddle.net/ywain/g2ufa8xr/
3/ You can use the data-currency parameter to change the currency displayed in the Checkout form. Just like data-amount, this is for display purposes only and the actual currency used for the charge is specified by the currency parameter in the charge creation.
This is what i managed to do.
I went with the custom form approach. I had a form template to capture both customer and card inputs in billing.template.html like so:
<form method="POST" id="payment-form">
<span class="payment-errors"></span>
<div>
<label>Name</label>
<input type="text" name="name" data-stripe="name">
</div>
<div>
<label>Email</label>
<input type="text" name="email" data-stripe="address_email">
</div>
<div>
<label>Address Line 1</label>
<input type="text" name="street" data-stripe="address_line1">
</div>
<div>
<label>Postcode</label>
<input type="text" name="postcode" data-stripe="address_zip">
</div>
<div>
<label for="country">Country</label>
<select ng-include="'../templates/_partials/_countrylist.html'"
id="countries" name="country" class="form-control"
name="country" ng-model="country" id="country" size="2"
data-stripe="address_country" required></select>
</div>
<div class="form-row">
<label>
<span>Card Number</span>
<input type="text" name="cardNumber" size="20" data-stripe="number"/>
</label>
</div>
<div class="form-row">
<label>
<span>CVC</span>
<input type="text" name="cvc" size="4" data-stripe="cvc"/>
</label>
</div>
<div class="form-row">
<label>
<span>Expiration (MM/YYYY)</span>
<input type="text" name="expMonth" size="2" data-stripe="exp-month"/>
</label>
<span> / </span>
<input type="text" name="expYear" size="4" data-stripe="exp-year"/>
</div>
<button id="customButton">Pay with Card</button>
</form>
I know we are not supposed to use name attribute in those form inputs but i left them so i could use angular validation, but i remove them using jquery before submitting to server.
Now i created a controller to handle the form: BillingController.js. In there i had an "on click" handler which kick started things by getting a hold of the form and doing some preparatory work: disabling button to prevent further clicks and removing those 'dreaded' name attributes, comme ca:
$('#customButton').on('click',function(event) {
var $form = $('#payment-form');
// Disable the submit button to prevent repeated clicks
$form.find('button').prop('disabled', true);
//NOW REMOVE THOSE NAME ATTRIBUTES
$form.find('input').removeAttr('name');
// call Stripe object and send form data to get back the token.
// NOTE first argument is $form
Stripe.card.createToken($form, stripeResponseHandler);
// Prevent the form from submitting with the default action
return false;
});
Now let me quote the documentation here as this is very important to understand: https://stripe.com/docs/tutorials/forms
The important code to notice is the call to Stripe.card.createToken.
The first argument is the form element containing credit card data
entered by the user. The relevant values are fetched from their
associated inputs using the data-stripe attribute specified in the
form.
Next we create stripeResponseHandler(). Remember it was the second argument in Stripe.card.createToken($form, stripeResponseHandler); above which gets called when Stripe returns the token.
function stripeResponseHandler(status, response) {
var $form = $('#payment-form');
if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
$form.find('button').prop('disabled', false);
} else {
// response contains id and card, which contains additional card details
var token = response.id;
// Insert the token into the form so it gets submitted to the server
$form.append($('<input type="hidden" name="stripeToken" />').val(token));
// and submit
$form.get(0).submit();
}
};
This is copy and paste stuff from stripe's own documentation: https://stripe.com/docs/tutorials/forms. Now, I want to say that, this is where a lot of us were tripping over the fact that form was performing a redirect etc. - notice final line $form.get(0).submit(); . Thats what caused the auto submit, redirecting to what ever action was on form, if u had any (in my case action attribute wasn't necessary as i was doing redirects in my controller).
So i decided to remove $form.get(0).submit() and implemented my own redirect after i was done sending data to the server.
NOTE: Stripe's response will have included data from the $form - try console.log(response); to have an idea of what's being posted back.
FINALLY:
We check if there were any errors returned and if so display them. Otherwise its all good, send data to the server.
The final code looks like:
function stripeResponseHandler(status, response) {
var $form = $('payment-form');
if (response.error) {
// Show the errors on the form
$form.find('.payment-errors').text(response.error.message);
} else {
// response contains id and card, which contains additional card details
var token = response.id;
// prepare data
var data = {
stripeToken: token,
fullName: response.card.name,
street: response.card.address_line1,
postcode: response.card.address_zip,
town: response.card.address_city,
country: response.card.address_country,
last4: response.card.last4
};
// send to server
$http.post('/checkout', data).then(function(result){
// here you can redirect yourself.
window.location.href = "/#/order-complete";
});
}
};
Angular really playing well with stripe here. Check out this link also: https://gist.github.com/boucher/1750368 - learn a lot from it.
I hope it helps someone today. Happy coding!
Stripe doesn't get involved with your form aside from preventing the default action on form submit event and stopping event propagation. Once the checkout process completes, it appends the relevant data to your form and then triggers a form submit event that is handled by HTML / Javascript natively.
I recommend using something like https://github.com/tobyn/angular-stripe-checkout to get your Stripe response handled correctly by Angular.
Otherwise you could add ng-submit="handleStripeCheckout($event)" to your form instead of action="/#/form". When Stripe's checkout process completes, your $scope.handleStripeCheckout method will be run and you can analyze the new form data inside that method.
Edit: Stripe checkout.js actually triggers form.submit(). That's a pretty bad bug on their part considering that almost no browsers handle that correctly. (Form submitted using submit() from a link cannot be caught by onsubmit handler)
I have the folowing gsp page:
<g:form controller="??" action="??">
<h1>Search</h1>
<g:submitButton name="search" value="Search"/>
<div id="resultsHere">
</div>
</g:form>
What i want to do is, everytime "Search is clicked", the database is searched for that record, lets imagine im looking for book titles. So everytime i write a title, the database finds the books and print every data related to the books. How can i do that=?
My idea is having something similar to this in the div:
<ul>
<g:each in="${bookList}">
<li>Name: ${it.name}, Locale: ${it.isbn}</li>
</g:each>
</ul>
So the point is, when the search button is clicked, the controller that handle that action should redirect the page to the same page, and pass the filtered list of books so it can be printed in the <g:each in="${bookList}"> tag.
I would like opinions about this being the best solution in this case. I could also render the results in the page directly, but i would like to do some css for the lookings so i think that wont be a good idea using render. Any help would be apreciated, and if possible, some lights with the code (specially the filtering part).
I would have one action in your controller, and render out the list.
ie: as pseudo-code (and not complete)
BookController {
def search = {SearchComamnd search ->
def books = []
if(search) {
books = Book.createCritera().list {
and {
title(search.title)
author(search.title)
}
}
}
render [ books:books ]
}
class SearchCommand {
def author
def title
}
}
and then when in your view
<g:form controller="??" action="??">
<h1>Search</h1>
<g:submitButton name="search" value="Search"/>
</g:form>
<g:each in="${books}">
<li class="book">Name: ${it.name}, Locale: ${it.isbn}</li>
</g:each>
you can now use css li.book to decorate the entry.