使用Flask路由页面并在cPanel上部署 [英] Routing pages using flask and deploying on cPanel

查看:80
本文介绍了使用Flask路由页面并在cPanel上部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我是不熟悉Flask并尝试建立我的第一个网站的人,现在已经尝试了几天,几乎没有进展,这是一些代码:


<从flask导入pre> Flask,render_template,url_for,重定向,请求,会话

app = Flask(__ name__)

@app。 route('/',methods = [ POST, GET])
def buy():
试试:

如果request.method == ; POST:
email = request.form [ email]
#查询数据库以找到买家

会话的确切金额和名称[用户 ;] =电子邮件
return redirect(url_for( proceed)))

else:
return render_template( index.html,logged_in = True)

例外,例如e:
return(str(e))


@ app.route( / confirm)
def进行():
#进入形成并创建另一个表单
(如果用户在会话中:
返回f h1 {session [ user]} // h1
else:
返回f h1找不到电子邮件 / h1
如果__name__ == __main__:

app.run()

第一个购买功能运行良好,当用户被重定向以继续时出现问题。 /确认标记已添加到网站的网址中,并且该网站转到 404未找到页面。我在使用cPanel更改页面时遇到了问题。将网站的用户发送到完全不同的页面的最佳方法是什么,我可以在其中添加自己选择的HTML文件。

解决方案

在应用程序URL目录中的.htaccess中添加以下规则

  
上的RewriteEngine RewriteRule ^ http:/ /%{HTTP_HOST}%{REQUEST_URI} [END,NE]


Hy I'm new to flask and trying to get my first site up, I have been trying this for a couple of days now with little to no progress, Here is some code:

from flask import Flask, render_template, url_for, redirect, request, session

app = Flask(__name__)

@app.route('/', methods=["POST", "GET"])
def purchase():
    try:
        
        if request.method == "POST":
            email = request.form["email"]
            # Query the data base to find the exact amount and name of buyer

            session["user"] = email
            return redirect(url_for("proceed"))
            
        else:
            return render_template("index.html",logged_in=True)
            
    except Exception as e:
        return(str(e))


@app.route("/confirm")
def proceed():
    # Get information and create another form
    if "user" in session:
        return f"""<h1>{session["user"]}</h1>"""
    else:
        return f"""<h1>No email found</h1>"""
if __name__ == "__main__":
    
    app.run()

The first purchase function works just fine, the problem appears when the user get redirected to proceed. The '/confirm' tag is added to the url of the site and the site goes to a **404 Not Found ** page. I have had problems changing pages with cPanel. What would be the best way to send the user of my site to a totally different page where i can add an HTML file of my choosing.

解决方案

Add the following rules to your .htaccess in the directory of your application URL

RewriteEngine on
RewriteRule ^http://%{HTTP_HOST}%{REQUEST_URI} [END,NE]

这篇关于使用Flask路由页面并在cPanel上部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆