Introduction to the web framework and django, and introduction to the web: django

Source: Internet
Author: User

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.

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.