Introduction to the web framework and django, and introduction to the web: django
All Web applications are essentially a socket server, and your browser is actually a socket Client.
Import socketdef f1 (request): "processes user requests and returns the following content: param request: all information of user requests: return: "f = open ('index. fsw ', 'rb') data = f. read () f. close () return data "" <body> Static webpage
This static page cannot be connected and interacted with the database, so it is also very low.
Import socketdef f1 (request): "processes user requests and returns the following content: param request: all information of user requests: return: "f = open ('index. fsw ', 'rb') data = f. read () f. close () return data "" <body> Dynamic page
There are two points to be mentioned here. The jinjia2 module is used first, so we should briefly introduce this module.
Rendering template (using the render_template method)
@app.route('/about/')def about(): # return render_template('about.html',user='username') return render_template('about.html',**{'user':'username'})
There are two ways to pass parameters when rendering a template: Use var = 'value' to pass a parameter, use a dictionary to organize multiple parameters, and add two*
Number is converted into a keyword parameter.
In the jinja2 template:
{{ ... }}
: Loads a variable. When the template is rendered, it will replace the value represented by the variable with the passed parameter of the same name.
{% ... %}
: Load a control statement.
{# ... #}
: Loads a comment, which is ignored during template rendering.
Variable:
Set the global variable: {% set name = 'xx' %}. Then you can use this variable.
Set local variables:
{% with foo = 42 %}{{ foo }}{% endwith %}
The foo variable can only be used in the with Tag.
{% if kenny.sick %}Kenny is sick.{% elif kenny.dead %}You killed Kenny! You bastard!!!{% else %}Kenny looks okay --- so far{% endif %}
If statement
# General loop <ul >{% for user in users %}< li >{{ user. username | e }}</li >{% endfor %} </ul> # traverse dictionary {% for key, value in my_dict.iteritems () %} <dt >{{ key | e }}</dt> <dd >{{ value | e }}</dd >{% endfor %}
For Loop
The most important part of the jinja2 module is the Macro. A macro is equivalent to a part of a set page, which can be introduced and can pass parameters to the macro. You can put some frequently used code snippets into a macro, and then extract some unfixed values as a variable. When using a macro, you can pass parameters to render the macro as part of the page.