Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the effect of the reversal of Flask URL

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the role of Flask URL inversion". In daily operation, I believe many people have doubts about what role Flask URL inversion has. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the question of "what role Flask URL inversion has"! Next, please follow the small series to learn together!

What is the use of learning Flask Framework?

Web site development so far, especially the server side, involves knowledge, content, very extensive. The demands on programmers are getting higher and higher. If a mature, robust framework is adopted, then some basic tasks, such as security, data flow control, etc., can be handled by the framework, so that the application developer can focus on the specific business logic. Advantages of using framework: stability and scalability, can reduce the difficulty of development, improve development efficiency.

Flask framework is python web framework, the biggest feature is light, let developers free flexibility to develop features compatible. Python's flexibility gives Flask the same characteristics-whether it's user profiles or product recommendations-Python has a huge advantage over other languages. In addition, Flask is light, easy to use, and low in trial and error costs. Therefore, starting from building a lightweight blog that is easy to implement and able to expand user portraits and product recommendations in the later stages, Flask is the best choice for web frameworks.

How to learn Flask Framework?

1. Create a Flask framework: app.py

#Import Flask from Flask this framework Import Flask this class

from flask import Flask

import config

#Initialize Flask object

#One parameter to pass__name__

#Convenient flask plugins such as Flask-sqlalchemy when errors occur, so as to find the location of the problem.

app = Flask(__name__)

#Also here is the debug mode set

app.config.from_object(config)

# @app.route is a decorator

# @ begins with the top of the function, indicating that it is a decorator

#The function of this decorator is to map a url view function

#127.0.0.1:5000/ ---will request the hello_world function and then return the result to the browser.

@app.route('/')

def hello_world():

return 'Hello World! '

#If the function is currently running as an entry program to the function. Then execute app.run()

if __name__ == '__main__':

#app.run()

#Start an application server to accept user requests

#while True:

# listen()

#Enable debug mode to start the server in real time without restarting the server.

#Start debug mode case can only be changed.py file Other files do not work.

app.run(host="192.168.25.1",port=5001)

2. Use the config file config.py

#!/ usr/bin/env python

# -*- coding:utf-8 -*-

# author:xjl

# datetime:2019/11/4 10:09

# software: PyCharm

#Here are the configuration parameters set

DEBUG = Tru

3. Use url to pass parameters

(1) Function of parameters: You can specify different parameters to load different data in the same URL until then.

(2) How to use parameters in flask: parameters need to be placed in

@app.route('/article/')

def article(id):

return " %s" %id of parameter you requested

4: Reverse URL

What is called inverted URL: conversion from view url is called inverted url

The effect of inversion is:

1. When the page is redirected from, the reverse of the url will be used.

2. In the template, the reverse of the url will also be used.

Pages at times of jump and redirect from

def index():

print(url_for('my_list'))

print(url_for('article',id='abc'))

return 'Hello World! '

@app.route('/list/')

def my_list():

return "list"

@app.route('/article/')

def article(id):

return " %s" %id of parameter you requested

At this point, the study of "what is the role of Flask URL inversion" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report