The web framework rescued us from the Wsgi. Now, we just need to write the function continuously, with the URL, we can continue the development of the Web App.
However, Web apps are more than just processing logic, and the pages that are presented to users are also important. Returns a string containing HTML in the function, a simple page can be, but think about more than 6,000 lines of HTML on Sina's homepage, are you sure you can write it correctly in a Python string? I can't do it anyway.
As the saying goes, Python engineers who don't know the front end are not good product managers. Students with web development experience understand that the most complex parts of Web apps are on HTML pages. HTML is not only correct, but also through CSS landscaping, coupled with complex JavaScript scripts to achieve a variety of interactive and animation effects. In summary, it is difficult to generate HTML pages.
Since it is unrealistic to spell strings in Python code, template technology has emerged.
Using the template, we need to prepare an HTML document beforehand, this HTML document is not normal HTML, but embedded some variables and instructions, and then, according to our incoming data, replace, get the final HTML, sent to the user:
This is the legendary Mvc:model-view-controller, Chinese name "model-View-controller".
Python's function of handling URLs is C:controller,controller is responsible for business logic, such as checking the existence of user names, removing user information and so on;
The template that contains the variable {{name}} is the v:view,view responsible for displaying the logic, and by simply replacing some variables, the view ultimately outputs the HTML that the user sees.
Where is the model in MVC? The model is used to pass to the view, so that when the view replaces the variable, it can take the corresponding data out of the model.
In the example above, the model is a dict:
{' name ': ' Michael '}
Just because Python supports keyword parameters, many web frameworks allow incoming keyword arguments, and then assemble a dict inside the frame as the model.
Now, let's rewrite the last direct output string as an example of HTML using the high-level, atmospheric MVC pattern:
From flask import flask, request, Render_templateapp = Flask (__name__) @app. Route ('/', methods=[' GET ', ' POST ') def home () : Return render_template (' home.html ') @app. Route ('/signin ', methods=[' GET ') def signin_form (): Return render_template (' form.html ') @app. Route ('/signin ', methods=[' POST ') def signin (): username = request.form[' username '] Password = request.form[' password ' if username== ' admin ' and password== ' password ': return render_template (' signin-ok.html ') , Username=username) return render_template (' form.html ', message= ' bad username or password ', username=username) if __ name__ = = ' __main__ ': App.run ()
Flask uses the render_template () function to render the template. Like web frameworks, Python templates are also available in many ways. Flask The default supported template is JINJA2, so we'll install the JINJA2 directly:
$ Easy_install JINJA2
Then, start writing the jinja2 template:
Home.html
Template to display the first page:
HomeHome
Form.html
Template to display the login form:
Sign In{{Message}}
{% ENDIF%} signin-ok.html
Template for Successful login:
Welcome, {{username}}Welcome, {{username}}!
What about the failed login template? We add a bit of conditional judgment to form.html and reuse form.html as a template for login failures.
Finally, be sure to put the template in the correct templates directory, templates and app.py in the same directory:
Start the Python app.py and see the page effect using the template:
With MVC, we work with M:model and C:controller in Python code, and V:view is handled through templates, so that we have succeeded in separating Python and HTML code to the fullest.
Another great benefit of using templates is that templates are easy to change, and when you're done saving, refreshing your browser will see the latest results, which is really important for front-end engineers debugging HTML, CSS, and JavaScript.
In the JINJA2 template, we use {{name}} to represent a variable that needs to be replaced. Many times, there is also a need for circular, conditional judgment and other directive statements, in JINJA2, with the {% ...%} to indicate instructions.
For example, the Loop output page:
{% for I in page_list%} {{i}} {% ENDFOR%}
If Page_list is a list:[1, 2, 3, 4, 5], the above template will output 5 hyperlinks.
In addition to JINJA2, common templates include:
- Mako: a template with <%.%> and ${xxx};
- Cheetah: Also a template with <%.%> and ${xxx};
- Django:django is a one-stop frame with a built-in template with {% ...%} and {{XXX}}.
Summary
With MVC, we've separated the Python code from the HTML code. The HTML code is all in the template and is more efficient to write.