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

时间:2018-08-17 01:37:22

标签: python python-3.x flask

我有一个使用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可以准备好阅读正在提交的表单。

1 个答案:

答案 0 :(得分:1)

嗯,应该考虑的是,在尝试解决问题数小时后,我在发布问题后找到了解决方案。

“#”仅表示表单已打开并在同一页面中提交。通过简单地将重定向移动到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='#'>

表示。