Skip to main content
 首页 » 编程设计

python之如何将一页请求定向到 Flask 中的某些定义

2024年12月31日8telwanggs

我有一个使用 Dimension template 的网页,此模板在浏览网站时使用“弹出窗口”而不是新页面。该链接指向此模板的演示以供说明。我想做的是:客户转到 mysite.com/#recipescraper -> 提交表单的“弹出”-> 提交表单 -> 弹出窗口替换为“谢谢”弹出窗口。

到目前为止,在 Python 3.6 中我有以下代码:

@app.route('/') 
def homepage(): 
    return render_template('index.html') 
 
#--- Cookpad Scraper Stuff ---# 
@app.route('/#Scraper', methods=['GET','POST']) #allow get and post requests 
def scraper(): # sending via forms as a post request (behind the scenes) 
    #--- Check if its a post or get request: ---# 
    if request.method == 'POST': #this block is only entered if the form is submitted 
        url = request.form.get('recipe')  
        user = request.form['name'] 
        #--- Assign variables to the multiple choices ---# 
        category = request.form.get('category') 
        dessert = request.form.get('dessert')  
        main_dish = request.form.get('main_dish')  
        side_dish = request.form.get('side_dish')  
        soup = request.form.get('soup')  
        mommy = request.form.get('mommy') 
 
        #--- Make sure a valid url was submitted ---# 
        check_url = 'https://cookpad.com' 
        if check_url in url: #if a string is submitted with  https://cookpad.com in it 
            title = recipe(url, category, dessert, main_dish, side_dish,  soup, mommy, user) #puts the title_ext returned from recipe() into the title  variable 
            send_data(title, url, mommy, category) # send data to  rethinkdb: cookpad_scrape database 
            publish(user) #publishes the scraped recipe into wiki 
            telegram(user, title) #notifies telegram 
 
            return redirect(url_for('thanks', title=title, user=user))  #redirects to url.com/thanks?title=something&user=something_else. Variables  are in the link 
 
        else: #otherwise return bad_link.html 
            return redirect(url_for('bad_link', link=url, user=user)) 
 
    #--- Make the form ---# 
    return render_template('#Scraper') 

这有效 perfectly ,直到客户点击“提交”为止。出于某种原因,我得到一个 405 'Method not allowed'错误。当刮板页面位于单独的“mysite.com/scraper”而没有弹出窗口时,我确实确认此代码有效。我没有使用 CSS/JS 的经验,但我假设 #Scraper 链接在没有实际提交到 python 服务器的情况下被执行?

这是网页和弹出窗口的代码:

<!DOCTYPE HTML> 
<!-- 
    Dimension by HTML5 UP 
    html5up.net | @ajlkn 
    Free for personal and commercial use under the CCA 3.0 license (html5up.net/license) 
--> 
<html> 
    <head> 
 
            <title>Website</title> 
            <meta charset="utf-8" /> 
            <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> 
            <link rel="stylesheet" href="../static/css/main.css" /> 
            <!--[if lte IE 9]><link rel="stylesheet" href="../static/css/ie9.css" /><![endif]--> 
            <noscript><link rel="stylesheet" href="../static/css/noscript.css" /></noscript> 
        </head> 
        <body> 
 
            <!-- Wrapper --> 
                <div id="wrapper"> 
 
                    <!-- Header --> 
                        <header id="header"> 
                            <div class="logo"> 
                                <span class="image" http://theiveyleague.com/wp-content/uploads/2011/07/6-long-hair-mom-dad-daughter-son-son-baby-1024x731.jpg></span> 
                            </div> 
                            <div class="content"> 
                                <div class="inner"> 
                                    <h1>Hi. This is our website.</h1> 
                                    <p>All for one, one for all.</p> 
                                </div> 
                            </div> 
                            <nav> 
                                <ul> 
                                    <li><a href="#More">More</a></li> 
                                    <li><a href="#Scraper">Scraper</a></li> 
                                </ul> 
                            </nav> 
                        </header> 
 
                    <!-- Main --> 
                        <div id="main"> 
                            <!-- More --> 
                                <article id="More"> 
                                    <h2 class="major">More</h2> 
                                    <!-- <span class="image main"><img src="../static/images/pic03.jpg" alt="" /></span> --> 
                                  [REDACTED] 
                                </article> 
                            <!-- Scraper --> 
                                <article id="Scraper"> 
                                    <h1 class="major">Cookpad Scraper</h1> 
                                    <form method = "POST"> 
                                            What is the cookpad recipe url? <input type="text" name="recipe"><br> 
                                            What is your name? <input type="text" name="name"><br> 
                                            Which category applies? <br> 
                                                <input type="radio" id="bread" name="category" value="Bread"> 
                                                    <label for="bread">Bread</label> 
                                                <input type="radio" id="fruit" name="category" value="Fruit"> 
                                                    <label for="fruit">Fruit</label> 
                                                <input type="radio" id="veggies" name="category" value="Veggie" checked> 
                                                    <label for="veggies">Veggies</label> 
                                                <input type="radio" id="other" name="category" value="Other"> 
                                                    <label for="other">Other</label> <br> 
 
                                            <br>What type of meal is it? <br> 
                                                <input type="checkbox" id="dessert" name="dessert"> 
                                                    <label for="dessert">dessert</label> 
                                                <input type="checkbox" id="main_dish" name="main_dish" checked> 
                                                    <label for="main_dish">Main Dish</label> 
                                                <input type="checkbox" id="side_dish" name="side_dish"> 
                                                    <label for="side_dish">Side Dish</label> 
                                                <input type="checkbox" id="soup" name="soup">  
                                                    <label for="soup">Soup</label> <br> 
 
 
                                            <br>Did mom make it? </br> 
                                                <input type="radio" id="yes" name="mommy" value="yes" checked> 
                                                    <label for="yes">Yes</label> 
                                                <input type="radio" id="no" name="mommy" value="no"> 
                                                    <label for="no">No</label><br> 
 
                                            <br><input type="submit" value="Submit"><br> 
                                    </form> 
                                </article> 
                    <!-- Footer --> 
                        <footer id="footer"> 
                            <p class="copyright">&copy; Untitled. Design: <a href="https://html5up.net">HTML5 UP</a>.</p> 
                        </footer> 
 
                </div> 
 
            <!-- BG --> 
                <div id="bg"></div> 
 
            <!-- Scripts --> 
                <script src="../static/js/jquery.min.js"></script> 
                <script src="../static/js/skel.min.js"></script> 
                <script src="../static/js/util.js"></script> 
                <script src="../static/js/main.js"></script> 
 
        </body> 
    </html> 

基本上我试图将“mysite.com/#article”链接传递给 python,这样 python 就可以准备好读取正在提交的表单。

请您参考如下方法:

好吧,可以肯定的是,在尝试解决问题数小时后,我在发布问题后找到了解决方案。

'#' 仅表示表单在同一页面中打开和提交。通过简单地将重定向移动到 app.route('/') ,问题就解决了:我们现在可以“弹出”表单并根据需要提交它进行分析。

新的 python 代码:

@app.route('/', methods=['GET','POST']) 
def homepage(): 
        #--- Check if its a post or get request: ---# 
    if request.method == 'POST': #this block is only entered if the form is submitted 
        scraper() 
    return render_template('index.html') 
 
#--- Cookpad Scraper Stuff ---# 
def scraper(): # sending via forms as a post request (behind the scenes) 
    #--- Check if its a post or get request: ---# 
    if request.method == 'POST': #this block is only entered if the form is submitted 
        url = request.form.get('recipe')  
        user = request.form['name'] 
        #--- Assign variables to the multiple choices ---# 
        category = request.form.get('category') 
        dessert = request.form.get('dessert')  
        main_dish = request.form.get('main_dish')  
        side_dish = request.form.get('side_dish')  
        soup = request.form.get('soup')  
        mommy = request.form.get('mommy') 
 
        #--- Make sure a valid url was submitted ---# 
        check_url = 'https://cookpad.com' 
        if check_url in url: #if a string is submitted with https://cookpad.com in it 
            title = recipe(url, category, dessert, main_dish, side_dish, soup, mommy, user) #puts the title_ext returned from recipe() into the title variable 
            send_data(title, url, mommy, category) # send data to rethinkdb: cookpad_scrape database 
            publish(user) #publishes the scraped recipe into wiki 
            telegram(user, title) #notifies telegram 
 
            return redirect(url_for('thanks', title=title, user=user)) #redirects to url.com/thanks?title=something&user=something_else. Variables are in the link 
 
        else: #otherwise return bad_link.html 
            return redirect(url_for('bad_link', link=url, user=user)) 
 
    #--- Make the form ---# 
    return render_template('#Scraper') #suspect this is not needed 

Html 代码不变。 this 之后我找到了解决方案关于什么的绝妙答案

<form action='#'> 

表示。