Django2.2 Templates page rendering data list jumps and simple page module inheritance

Disclaimer : This blog is with pycharm Django2.2, based on this content, will simply explain

  1. Database parameter passing in the form of additions and deletions to change search (complete small projects)
  2. Location routing parameters, keyword parameters, as well as html parsing reverse route
  3. Form of transfer request
  4. html page breaks and inheritance

First, three transmission parameters routing methods, and analytical reverse route

    1.1 positional parameters:       

re_path(r'gettime/(\d+)/(\d+)/(\d+)/',views.get_time),

GET_TIME DEF (Request, hour, minute, SECOND,): 
# ordinary mass participation
# return HttpResponse ( "Time ->% d:% d:% d"% (hour, minute, SECOND,))

# regular expressions mass participation
return HttpResponse ( "Time ->% s :% s:% s"% (hour, minute, second))

      The so-called positional parameters: the writing position can not change the parameters of a function parameter passing, will change the parameters associated error

    1.2 Keyword parameters:

# Keyword parameter passing date (specified in the url order, do not sort function accepts ---- P (uppercase) --- "short parameter) 
re_path (R & lt ' getData / (P <year> \ + D) / ( P <month The> \ + D) / (P <Day> \ + D) / ' , views.get_data)


def get_data(request,day,month,year):
    return HttpResponse("无序传参 Data %s :%s :%s" %(year,month,day))

      Keyword argument we can see that the location parameter routing decision does not affect the output parameters when writing reception

    1.3 reverse lookup routing (later recommended wording)

# With routing written (in plus incude namespace attribute) 
 path (R & lt ' App2 / ' , the include (( ' App2.urls ' , ' App2 ' ), namespace = ' SECOND ' )),

# Sub routing writing (hereinafter route plus the name attribute) 
# HTML parsing reverse routing 
    path (R & lt ' Learn / ' , views.learn, name = " Learn " ),

# Page references are resolved routes wording 
<h3> Class List, Click </ H3> 
    { % for Grade in grade_list% }
         <Li> <A the href = " {% URL 'SECOND: getstudent' g_ID = grade.id%} " >} {} {grade.g_name </a> </ Li> 
    { % endfor% }
     <a href= ""> </a>

Second, the page breaks and inheritance:

  Why split the page: When you write a web page, for example, between pages in the top navigation bar has multiple pages, this time we can use inheritance to write, save the tedious work of repeated write

# Basic page so written, a layout 
{% Block header% } {% endblock %} {% block search %} {% endblock %} {% block content %} {% endblock %} {% block footer %} { % Endblock%}

# inheritance (do not write the body of Han)
The extends% { "base.html"%} 
{%}% Block header
<h1 of> --- This is a first original parent class! header </ h1 of>
{% endblock%}


2.
Block Content%}% 
<h1 of> Inherited Display Content </ h1 of>
{% endblock%}
# both displays
{%}% Block header
{{Block. Super }}
<h1 of> override the inherited file header </ h1 >
{%}% endblock

Third, the examples demonstrate: a database of mass participation, url Jump List

  So the question is, what use is it, in fact ---------------- usefulness drops significantly (First can do basic anti-crawling, you can make a list and a list of the details page ) --------- himself on the code

 

 

 

# Database instance parameter passing 
    path (R & lt ' Grades / ' , views.grades, name = ' getgrade ' ),
    path(r'students/<int:g_id>/',views.students,name='getstudent'),
    # path(r'gettime/<int:hour>/<int:minute>/<int:second>/',views.get_time),

    # Show the student details 
    path (r ' student_detil / <int: the above mentioned id> / ' , views.student_detil, name = ' studentdetil ' ),
     # remove students 
    path (r ' delete_student / <int: s_id> / ' , views.delete_student, = name ' deletestudemt ' ),
# Display the details of students 
DEF student_detil (Request, the above mentioned id):
    student=Student.objects.get(pk=id)
    # name=student.s_name
    # grade=student.s_grade_id
    # stu_id=Student.objects.filter(s_grade_id=s_grade_id)
    return render(request,'student_delit.html',context={"student":student,})

# Remove students 
DEF delete_student (Request, s_id):
    student=Student.objects.get(pk=s_id)
    student.delete()

    return HttpResponse ( " student " + student.s_name + " deleted successfully! " )


def do_create_student(request):
    print(request.method)
    sName=request.POST.get('sName')
    grade_id=request.POST.get('choose')
    print(grade_id)
    student=Student()
    student.s_name=sName
    student.s_grade_id=grade_id
    student.save()
    return HttpResponse (sName + " ---> 170 " + grade_id + " Students " + " added successfully " )
 <h3>班级列表,点击查看</h3>
    {% for grade in grade_list %}
        <li><a href="{% url 'second:getstudent' g_id=grade.id %}">{{ grade.g_name }}</a></li>
    {% endfor %}
    <a href=""></a>




 <H3> 170 {{g_id}} class of students following list: </ h3>
    <ul>
        {% for student in students_list %}
            <li><a href="{% url 'second:studentdetil' id=student.pk %}">{{ student.s_name }}</a></li>
        {% endfor %}
    </ul>

    <hr>
    <H2> Add Student Information </ h2>
    <form action="{% url 'second:do_create_student' %}" method="post">
        姓名:<input type="text" name="sName">
        Where the classes:
        <select name="choose">
            <option value="1">1701班</option>
            <option value="2">1702班</option>
            <option value="3">1703班</option>
            <option value="4">1704班</option>
            <option value="5">1705班</option>
            <option value="6">1706班</option>
        </select>
        <the INPUT of the type = " the Submit " value = " OK to add the student " >
    </form>
    <hr><button> <a href= "{% url
     'second:getgrade' %}"> Back Class List </a> </ button>



 <H1> STUDENTS: </ h1>

    <li>姓名{{ student.s_name }}</li>
    <Li> Class located: 170 {{student.s_grade_id}} </ li>
    <button><a href="{% url 'second:deletestudemt' s_id=student.pk %}">删除该学生</a></button>

Run shot

 

 

 

 

 Fourth, the next update of the session Login Register, as well as to explain the basic types of conversation, hope you like! ! !

 

 

Guess you like

Origin www.cnblogs.com/cybg/p/12002518.html