[英]Routing after Login in Flask / Google app engine
i am building my first Webapp with Pyhton, Flask and GAE.我正在使用 Pyhton、Flask 和 GAE 构建我的第一个 Web 应用程序。 So far I've built the basic login system and the basic structure of the website.到目前为止,我已经构建了基本的登录系统和网站的基本结构。 Now I'm wondering how to redirect my users to a different version of my website after logging in.现在我想知道如何在登录后将我的用户重定向到我网站的不同版本。
Eg before login my navigation bar shows a "login" button.例如,在登录之前,我的导航栏会显示一个“登录”按钮。 After login this button needs to be replaced by lets say the "logout" button.登录后,此按钮需要替换为“注销”按钮。 Or i need to show some pages in my navbar that weren't accessible before login.或者我需要在我的导航栏中显示一些在登录前无法访问的页面。
My thoughts so far are:到目前为止,我的想法是:
So my Question remains.所以我的问题仍然存在。 Are there any best practices, what is the general approach in WebDev/Python Flask.是否有任何最佳实践,WebDev/Python Flask 中的一般方法是什么。
Since you're using Flask, you can achieve what you want with Jinja2.由于您使用的是 Flask,因此您可以使用 Jinja2 实现您想要的。
Option 1选项1
When user is logged in, your Python code will populate and return one or more variables eg当用户登录时,您的 Python 代码将填充并返回一个或多个变量,例如
LOGGED_IN_USER (the logged in user id)
USER_EMAIl (logged in user email)
When your web page loads, it checks for any of those variables.当您的网页加载时,它会检查任何这些变量。 If it's present, it displays a set of widgets such as your logout link, user name, etc and if absent, it displays another set of widget such as a login link.如果存在,它会显示一组小部件,例如您的注销链接、用户名等,如果不存在,它会显示另一组小部件,例如登录链接。
Sample Code示例代码
HTML Page网页
<ul class="navbar-nav ml-auto">
<!-- If user is logged in, show their email, logout link
and a link to access their setting -->
{% if LOGGED_IN_USER %}
<li class="nav-item dropdown" >
<a href="" class="dropdown-toggle" data-toggle="dropdown">{{USER_EMAIL}} <span class="caret"></span></a>
<ul class="dropdown-menu" >
<li ><a class="dropdown-item" href="{{LOG_OUT_URL}}">Logout</a></li>
<li ><a class="dropdown-item" href="/settings/">Settings</a></li>
</ul>
</li>
{% else %}
<!-- If user is not logged in, show a login link -->
<li class="nav-item active">
<a class="nav-link" href="/login/">Login</span></a>
</li>
{% endif %}
</ul>
Add the above code to a base template and have any page which needs to distinguish between logged-in/logged out users inherit from the template.将上述代码添加到基本模板中,并从模板继承任何需要区分登录/注销用户的页面。
Option 2选项 2
Have a base template with a block for the variable content.有一个带有变量内容块的基本模板。 Have separate html pages for logged in and logged out users eg logged_in.html, logged_out.html.为登录和注销的用户提供单独的 html 页面,例如logged_in.html、logged_out.html。 These 2 html pages will inherit your base template.这 2 个 html 页面将继承您的基本模板。
Your python code will return logged_in.html if user is signed in and logged_out.html if user is not signed in.如果用户已登录,您的 python 代码将返回logged_in.html,如果用户未登录,则返回logged_out.html。
I don't know about best practices per se.我不知道最佳实践本身。 At the end of the day, you try to write less code while ensuring your code is fast and readable to you (you can understand it when you come back to it in a few months time).归根结底,您尝试编写更少的代码,同时确保您的代码对您来说是快速且可读的(您可以在几个月后重新阅读它时理解它)。
Personally, I go with option 1就个人而言,我选择选项 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.