zqxt_views/urls.py
path('', calc_views.index, name='home'),
calc/views.py
def index(request):
return render(request, 'home.html')
calc/templates/home.html
自强url跳转,一个地址自动 跳另一个地址
python manage.py shell
reverse('add2', args=(4,5))
reverse('add2', args=(444,555))
calc\views.py 跳转的函数
def old_add2_redirect(request, a, b):
return HttpResponseRedirect(
reverse('add2', args=(a, b))
)
urls.py
path('add///', calc_views.old_add2_redirect), #update
path('new_add///', calc_views.add2, name='add2'), #add
参数
calc\views.py
def index(request):
string = u"我在学习Django,用它来建网站"
TutorialList = ["HTML", "CSS", "jQuery", "Python", "Django"]
info_dict = {'site': u'django', 'content': u'各种IT技术教程'}
List = map(str, range(100))# 一个长度为100的 List
return render(request, 'home.html', {'string': string,'TutorialList': TutorialList,'info_dict': info_dict,'List': List})
template\home.html
1
2
3
4
自强学堂5
6
7
8 计算 4+5
9 {{ string }}
10
11 教程列表:12 {% for i in TutorialList %}13 {{ i }}14 {% endfor %}15
16
17 字典方法 1 站点:{{ info_dict.site }} 内容:{{ info_dict.content }}
18 字典方法 2
19 {% for key, value in info_dict.items %}20 {{ key }}: {{ value }}21 {% endfor %}22
23
24 {% for item in List %}25 {{ item }}{% if not forloop.last %},{% endif %}26 {% endfor %}27
28
29 变量 描述30 forloop.counter 索引从 1 开始算
31 forloop.counter0 索引从 0 开始算
32 forloop.revcounter 索引从最大长度到 1
33 forloop.revcounter0 索引从最大长度到 0
34 forloop.first 当遍历的元素为第一项时为真
35 forloop.last 当遍历的元素为最后一项时为真
36 forloop.parentloop
37 用在嵌套的 for 循环中,
38 获取上一层 for 循环的 forloop
39
40
41 {% for athlete in athlete_list %}42
{{ athlete.name }}43 {% empty %}44
抱歉,列表为空45 {% endfor %}46
47
48 {% url 'add2' 4 5 %} 获取URL
49
50 {{ request.user }} 获取当前用户:
51
52 如果登陆就显示内容,不登陆就不显示内容:53 {% if request.user.is_authenticated %}54 {{ request.user.username }},您好!55 {% else %}56 请登陆,这里放登陆链接57 {% endif %}
58
59
60 {{ request.path }} 获取当前网址
61 {{ request.GET.urlencode }} 获取当前 GET 参数:
63
64