#MP123 - stripe flask

1 messages · Page 1 of 1 (latest)

crude urchin
#

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

crude urchin
#

And this is from an html file that is in your templates folder, correct?

tall turret
#

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

crude urchin
#

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>

#

Have you tried something like that?

tall turret
#

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

crude urchin
#

Can you show me your html with the url_for code? I am curious why it might be getting translated late that

tall turret
#

idk what that static/css is tho, i had a css folder, and i made a static folder, and moved it into there

crude urchin
#

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

tall turret
#

Yeah, I'm unsure

#

Why would it translate like that?

crude urchin
#

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

tall turret
#

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?

crude urchin
#

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

tall turret
crude urchin
#

Does the page ever go through some kind of flask rendering? Or are you redirecting directly to the html file?

tall turret
crude urchin
#

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?

tall turret
crude urchin
#

So you go to checkout, the user completes checkout, and when they are redirected back to your page there is no css?

tall turret
#

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

crude urchin
#

Gotcha, how do you render that page with the button?

#

And this page is index.html right?

tall turret
#

These are my total files

#

app.py loads, pricing.html(which gets button from css), and then should load stripes checkout

crude urchin
#

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?

tall turret
#

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)```
crude urchin
#

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 ?
tall turret
#

where would I add that route?

crude urchin
#

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

tall turret
#

yeah but at the bottom? or in place of another route?

crude urchin
#

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

tall turret
#

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?

crude urchin
#

Yup, that is what I was going for

tall turret
#

I think I was supposed to define this

crude urchin
#

Add it to your flask import
from flask import Flask, redirect, request, render_template

tall turret
#

I tried it with pricing and pages/pricing, it doesn't seem to find the file if I do that

#
#

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

crude urchin
#

Oh the render_template function assumes that your html file will be in the templates folder.

tall turret
#

I'll change the name of the folder

crude urchin
tall turret
#

like this

#

but the css inside the pricing.html is not found

crude urchin
#

So this is with the route that uses render_template?

tall turret
#

no, but I can try it with that method

crude urchin
#

Yes, please try that. I think that flask needs to render the file for url_for to work

tall turret
#

if you want I'm able to vc lol if its easier and you are bored

crude urchin
#

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

crude urchin
#

This might be something to ask on StackOverflow or a more flask-centric forum

tall turret
#

yeah I'll take a look, thank you so much for the help though

crude urchin
#

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!

tall turret
#

It's alright, I paid 3 tutors to look and they couldn't find it either hahah