111- pass manner using post value (the most simple example)

From simple to complex, post There are several traditional values ​​presentation, here the most simple way, and comes with a few other knowledge points.

 

1, the first to write a template page:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Simple Post</title>
</head>
<body>
    <p>求幂运算:</p>
    <form action="" method="post">
        {% csrf_token %}
        <p>底数:<input type="text" name="base" /></p>
        <p>指数:<input type="text" name="index" /></p>
        <input type="submit" value="提交">
    </form>
</body>
</html>

 There are an additional four html knowledge:

(1) action can be blank, which returns the content to the current page (overwrites the previous contents), and the Action to be handled configuration views combining function

(2)input type类型,参考w3school:https://www.w3school.com.cn/tags/att_input_type.asp

 Content on the input, there is only one type of text this text, if you want to calculate, and so must be converted to int or float type

(3) In the latter example, html specification will follow the wording of XHTML, namely: All labels must be closed, <input> be written as: <input />

(4) XHTML specification

  • XHTML elements must be properly nested.
  • XHTML elements must be closed.
  • Label names must be lowercase.
  • XHTML documents must have a root element <html> </ html>.

 

2, how to deal with post views submitted by function

views function always first display form and submit action reprocessing table, which is the most basic points. Code structure is as follows:

Two main points:

(1) uppercase or POST methods typically expressed as a result, the post usually lowercase as a parameter

(2) a lot of tutorials will ignore else, but the couple learning, using if-else approach may be more lucid

def simple_post(request):
    if request.method == 'POST':
        pass
    else:
        pass

This code is expressed as:

If you receive a post submitted, will handle the "Submit" after things;

If you do not receive post submission, it is clear that is displayed directly on this page, because only showing a page, users are allowed to submit action.

The completion code is as follows:

# Simple post pass value 
DEF simple_post (Request): 
    IF request.method == 'the POST': 
        Base = request.POST.get ( 'Base') 
        index = request.POST.get ( 'index') 
        return the HttpResponse ( ' was obtained in base % S , index % S , exponentiation is: % D '% (Base , index , int (Base) ** int (index))) 
    the else: 
        return the render (Request,' simple_post.html ')

 

3, the initial page

4, after the submission page

 

Guess you like

Origin www.cnblogs.com/lzhshn/p/11407622.html