#MP123 - stripe flask
1 messages · Page 1 of 1 (latest)
How are you currently trying to load the css on the page? Can you show me the snippet of your HTML?
For the record, this is more of a pure python/flask question so this server typically won't be able to help with it (we are more for questions that directly involve stripe), but the server is slow and I have a flask setup so I am happy to help look in to this a bit
ok thank you
And this is from an html file that is in your templates folder, correct?
I don't have a templates folder, I have a static folder where the checkout.html is, should I have a templates folder?
This is how the file structure is
So I have my html in a templates folder and my js files in the static folder. Then when I include the js in the html files, I use flask's url_for function to get the path to the js file
<script src="{{ url_for('static', filename='checkout.js') }}" defer></script>
From what I can see on StackOverflow, the typical way to do css is to have the files in static/styles and then use url_for like this
https://stackoverflow.com/questions/22259847/application-not-picking-up-css-file-flask-python
Have you tried something like that?
I've seen something of that before, and gave it a shot for a day or two, no luck, but I will try it again 🙂
Yeah Idk ;-; url_for is just being weird for me
Can you show me your html with the url_for code? I am curious why it might be getting translated late that
idk what that static/css is tho, i had a css folder, and i made a static folder, and moved it into there
Can you try removing the leading slash in your filename= string? So filename='css/style.css' instead of filename='/css/style.css'
Sometimes that can throw it off
Wait, is that folder actually named "static\css" or do you have a "static" folder with a "css" folder in it?
That looks like VSCode where the folder actually has a slash in the name. I think you need it to actually be two seperate folders
static with a folder in it
It does that when there are no other files in it, i can put one to show that they are
Would it have something to do with this line in app.py?
Gotcha. And how are you serving this webpage? Are you doing return render_template('index.html')
Good question. Let me see what I had to do in my setup, it has been a bit
Does the page ever go through some kind of flask rendering? Or are you redirecting directly to the html file?
Should I try this?
Oh, that is a stripe hosted checkout page. You shouldn't need css for that. I thought this was about your own index.html page?
yeah, so my page that is not loading css is the page where you click the button to checkout
So you go to checkout, the user completes checkout, and when they are redirected back to your page there is no css?
So I run app.py, the page where the button to checkout should load, and it doesn't load the css to be able to click the button to go to checkout
Gotcha, how do you render that page with the button?
And this page is index.html right?
These are my total files
app.py loads, pricing.html(which gets button from css), and then should load stripes checkout
Also as a slight followup, my example app doesn't specify what the static url or folder is
app = Flask(__name__)
So it looks like my app just uses the defaults where they would just equal static
So how is pricing.html loaded? Like what is the python function for that route?
hmm idek, here ill send my app.py
I'm new to python
import stripe
app = Flask(__name__,static_url_path="",static_folder="pages")
stripe.api_key = 'sk_test_51KzqK9Hj2B2Quz911XrP11cB4Jb2ESrDCelSpRIZBqa18TWO9bGKlyuWsmiNeGYEHw4224xx5ghUWDaTQOukRjcf00rHXcZGYU'
YOUR_DOMAIN = "http://localhost:5000"
@app.route('/create-checkout-session', methods=['POST'])
def create_checkout_session():
try:
checkout_session = stripe.checkout.Session.create(
line_items = [
{
'price': 'price_1KzrAtHj2B2Quz91wMDanJjz',
'quantity':1
}
],
mode="payment",
success_url=YOUR_DOMAIN + "/success.html",
cancel_url=YOUR_DOMAIN + "/cancel.html"
)
except Exception as e:
return str(e)
return redirect(checkout_session.url,code=303)
if __name__== "__main__":
app.run(port=5000,debug=True)```
Can you try switching to
app = Flask(__name__)
And then add this route
def upe():
return render_template('pricing.html')```
And then navigate to localhost:5000/checkout ?
where would I add that route?
It sounds like somehow you are navigating to the exact html you wrote without flask getting a chance to modify the html to add its links in
To your app.py file
yeah but at the bottom? or in place of another route?
Above the main function. Like you can add it right under your existing create_checkout_session function
I think it just needs to be at the top level but let's keep the structure consistent
import stripe
app = Flask(__name__)
# ,static_url_path="",static_folder="pages"
stripe.api_key = 'sk_test_51KzqK9Hj2B2Quz911XrP11cB4Jb2ESrDCelSpRIZBqa18TWO9bGKlyuWsmiNeGYEHw4224xx5ghUWDaTQOukRjcf00rHXcZGYU'
YOUR_DOMAIN = "http://localhost:5000"
@app.route('/checkout')
def upe():
return render_template('pricing.html')
@app.route('/create-checkout-session', methods=['POST'])
def create_checkout_session():
try:
checkout_session = stripe.checkout.Session.create(
line_items = [
{
'price': 'price_1KzrAtHj2B2Quz91wMDanJjz',
'quantity':1
}
],
mode="payment",
success_url=YOUR_DOMAIN + "/success.html",
cancel_url=YOUR_DOMAIN + "/cancel.html"
)
except Exception as e:
return str(e)
return redirect(checkout_session.url,code=303)
if __name__== "__main__":
app.run(port=5000,debug=True)```
Like this?
Yup, that is what I was going for
Add it to your flask import
from flask import Flask, redirect, request, render_template
I tried it with pricing and pages/pricing, it doesn't seem to find the file if I do that
Download the full source code of application here:
#flask #python #stripe
Visit my Online Free Media Tool Website
Buy Premium Scripts and Apps Here:
this is the video I took reference of after trying the url_for method
and it works, but fails to read the css once it can find the original pricing.html file
Oh the render_template function assumes that your html file will be in the templates folder.
I'll change the name of the folder
I think part of this is what you are defining from that app = Flask line. I think the files are not where flask is expecting https://flask.palletsprojects.com/en/2.1.x/api/
Yeah so, I can change the name of the folder and then change the app = Flask like to fit that, and it will find the file.
like this
but the css inside the pricing.html is not found
So this is with the route that uses render_template?
no, but I can try it with that method
Yes, please try that. I think that flask needs to render the file for url_for to work
if you want I'm able to vc lol if its easier and you are bored
You might be able to do it with hard coded paths but I have honestly not played around enough to know how
Can't sadly. And actually have to log off soon anyways, sorry we haven't been able to make much progress
ok
This might be something to ask on StackOverflow or a more flask-centric forum
yeah I'll take a look, thank you so much for the help though
I thought I knew enough to help on this one but apparently not, my apologies
Of course, have a nice weekend and good luck figuring this one out!
It's alright, I paid 3 tutors to look and they couldn't find it either hahah