Python one-day training 103 ---- Django template exercises, pythondjango
Requirements
Request. META is a Python dictionary that contains all the Header information of this HTTP request, such as the user IP address and user Agent. You can obtain this dictionary through request. META. items. You must output the META information to the webpage.
Output result
Source code index.html
Views. py
from django.shor
If you want the form form to submit a URL that is similar to action= "/index-5-6.html", you can use the {% url ' test1 ' param1=5 param2=6%} in the HTML template language urls.pyFrom django.conf.urls import URL, includefrom mytest import viewsurlpatterns = [ url R ' ^index-(? p. html ', Views.index, name= ' test1 '),]views.pyFrom django.http import httpresponsefrom django.shortcuts import renderfrom django.views import ViewDef index (req, param1 ,
If we need the template language to render the rendered HTML file to the front end when the backend has data dynamically extracted to the front endThe index function in our views.py has an S variable in the list that passes the data in curly braces to the {"list": s} to pass s to the list.The place to add the HTML is to use 2 curly braces to introduce the -------------------------------------------------------------------------------------------------
Release date: 2012-4 4Updated on:
Affected Systems:Django CMS 2.xDescription:--------------------------------------------------------------------------------Bugtraq id: 57559Django CMS uses pony as the Technical Support CMS and can be edited at the front end.The page_attribute template tags of earlier versions of django CMS 2.3.5 are not properly filtered. The HTML injection vulnerability exists in the imp
Perform different actions when the list is empty or non-empty: for in list%} ... {empty %} ... {% endfor%}The number of times the loop is accessed using Forloop.counter, which sequentially outputs the number of cycles, starting at 1: for in list%} ... forloop.counter }} ... {% endfor%}Starting from 0 counts: for in list%} ... Forloop.counter0 }} ... {% endfor%}Determine if it is the first cycle: for in list%} ... {ifforloop.first %} is the first round.
HTML> Head> title>DOcumenttitle> Head> Body> H1>{{User}}H1> BR> BR> Li>Method:{{user.say}}Li> Note that there are no brackets behind it. - Body>HTML>2. Modify the views.py file#-*-coding:cp936-*- fromDjango.httpImportHttpResponse#import templates related packages; loader: loader, loading HTML templates fromDjango.templateImportLoader,context#Learn to define the first class in a views.pyclassPerson ():def __init__(self,name,age,sex): Self.name=name S
1. Modify Index pageHTML> Head> title>{{User}}title> Head> Body> H1>Hello {{User}}H1> BR> BR> Li>Hello {{User.Name}}Li> Li>Hello {{user.age}}Li> Li>Hello {{user.sex}}Li> Body>HTML>#-*-coding:cp936-*- fromDjango.httpImportHttpResponse#import templates related packages; loader: loader, loading HTML templates fromDjango.templateImportLoader,context#Learn to define the first class in a views.pyclassPerson ():def __init__(self,name,age,sex):
Filter Format: variable | Upper (filter keyword)1. Index{% for book in user reversed%} {% EMPTY%}{% ENDFOR%}#-*-coding:cp936-*- fromDjango.httpImportHttpResponse fromDjango.templateImportLoader,contextdefIndex (Request): T=loader.get_template ('index.html')Booklist=['python','C + +','Java','PHP']#passing data to an HTML pageC=context ({'User': Booklist}) #Rendering Methodst=T.render (c)returnHttpResponse (t)Django Definition
HTML> Head> title>DOcumenttitle> Head> Body> H1>{{User}}H1> BR> BR> H1>Filter filtersH1> P>1, the use of the Linux pipeline thinkingP> P>2, first the value of book into uppercase, and then converted to lowercase, and then converted to uppercase lettersP>{% for book in user%}Li>{{book | upper |lower | capfirst}}Li>{% empty%}Li>No books foundLi>{% endfor%}Body>HTML>#-*-coding:cp936-*- fromDjango.httpImportHttpResponse fromDjango.templat
Templates href formathref= "{% url ' name ' params%}"Where: Name is the name alias that is configured for a URL in the url.py file, and each parameter specific value of the params URL is used as follows:Add a url,name parameter to the URL of the. py #在url: name= ' app-views-func ' urlpatterns = [URL (r ' ^ (\w+)/(\w+)/(\d+)/delete/$ ', Views.table_delete,name= ' Table_delete '),]#' Table_delete ' 3 2 1%} "#url中有三个参数 (\w+)/(\w+)/(\d+), so pass in three specific parameter values 3 2 1The resulting
' People5. Person.objects.filter (name__iexact= ' abc ') #名称为abc到那时不区分大小写, can find ABC,ABC,ABC, these are eligible6. Person.objects.filter (name__contains= ' abc ') #名称中包含 ' abc ' People7. Person.objects.filter (name__icontains= ' abc ') #名称中包含 ' abc ', and ABC is case insensitive8. Person.objects.filter (name__regex= "^ABC") #正则表达式查询9. Person.objects.filter (name__iregex= ' ^abc ') #正则表达式不区分大小写Filter is found to meet the conditions, of course, there are also excluded to meet certain condition
For URL files URL (r'^tp1/', VIEWS.TP1)For the views file, jump to tp1.html while uploading the list to the frontdef TP1 (Request): = [1, 2, 3, 4, 5, 6] return'tp1.html', {'list': List})For the inherited file master, the content will be replaced by writing {%block tag%}{%endblock%}"en">"UTF-8"> "stylesheet"href="/static/commons.css">class="pg-content"> Little Boy Management {% block content%}{% Endblock%}Inheritors TP1 First imports through {% extends ' master.html '%} master.htmlTh
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.