django学习知识点汇总(templates)

问题1:项目中存在多个app,每个app包括相同的名称的html文件,在页面显示是,访问url会产出模板不是真正想访问的模板

 

模板一般放在app下的templates中,Django会自动去这个文件夹中找。但 假如我们每个app的templates中都有一个 index.html,当我们在views.py中使用的时候,直接写一个 render(request, 'index.html'),Django 能不能找到当前 app 的 templates 文件夹中的 index.html 文件夹呢?(答案是不一定能,有可能找错)

Django 模板查找机制: Django 查找模板的过程是在每个 app 的 templates 文件夹中找(而不只是当前 app 中的代码只在当前的 app 的 templates 文件夹中找)。各个 app 的 templates 形成一个文件夹列表,Django 遍历这个列表,一个个文件夹进行查找,当在某一个文件夹找到的时候就停止,所有的都遍历完了还找不到指定的模板的时候就是 Template Not Found (过程类似于Python找包)。这样设计有利当然也有弊,有利是的地方是一个app可以用另一个app的模板文件,弊是有可能会找错了。所以我们使用的时候在 templates 中建立一个 app 同名的文件夹,这样就好了。

这就需要把每个app中的 templates 文件夹中再建一个 app 的名称,仅和该app相关的模板放在 app/templates/app/ 目录下面,

文件目录

例子:learn/templates/learn/home.html

在learn的views.py 中

from django.shortcuts import render
def home(request):
    return render(request,'learn/home.html')

 

在url.py 中存在两个相同name url

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', calc_views.index, name='home'),
    url(r'^home/$',learn_views.home, name='home'),
    url(r'^add/$',calc_views.add ,name='add'),
    url(r'^new_add/(\d+)/(\d+)/$', calc_views.add2, name='add2'),
    url(r'^add/(\d+)/(\d+)/$', calc_views.old_add2_redirect),
    #url(r'^search/$','mysite.books.views.search')
]

 

访问:http://127.0.0.1:8000/home/

可以访问到learn/templates/learn/home.html

----------------------------------------------------------------------------------------------------------------------------

问题二:

通过设置显示页面的元素

views.py

# -*- coding: utf-8 -*-
from django.shortcuts import render


def home(request):
    #字符串显示
    #string = u"我在自强学堂学习Django,用它来建网站"
    #return render(request, 'learn/home.html', {'string': string})
    #list 显示
    #TotorialList = ["html","css","jquery","python","djano"]
    #return render(request,'learn/home.html',{'TotorialList':TotorialList})
    #显示字典内容
    #info_dict = {'site': u'自强','content':u'独立'}
    #return render(request,'learn/home.html',{'info_dict':info_dict})
    #在模板进行条件判断和for循环的详细操作
List = map(str,range(100))
    return render(request,'learn/home.html',{'List':List})

home.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title> welcome</title>
</head>
<body>
welcome learn km
<!--{{ string }}
{% for i in TotorialList %}
{{ i }}
{% endfor %}
-->
<!--
我是:{{info_dict.site }}{{info_dict.content}}
{% for key, value in info_dict.items %}
    {{ key }}: {{ value }}
{% endfor %}
-->
{% for item in List %}
{{ item }}{% if not forloop.last %},{% endif %}
{% endfor %}

<a href="{{ request.path }}?{{ request.GET.urlencode }}&delete=1">当前网址加参数 delete</a>
</body>
</html>

 

---------------------------------------------------------------------------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Django是一个基于Python语言的Web框架,其提供了一系列拓展和技术来增强其功能和性能。以下是一些常见的Django拓展知识点和技术: 1. Django REST framework:提供了一套用于构建RESTful API的工具集,包括序列化、验证、路由、视图等功能。 2. Django ORM:提供了一套用于操作数据库的对象关系映射工具,支持多种数据库后端。 3. Django Admin:提供了一个自动生成的管理后台,用于管理应用程序的模型数据。 4. Django Cache:提供了一套缓存框架,用于缓存数据库查询、视图响应等数据,提高应用程序的性能。 5. Django Signals:提供了一套信号机制,用于在模型保存、删除等事件发生时触发特定的操作。 6. Django Middleware:提供了一套中间件机制,用于在请求和响应处理过程中进行一些通用的操作,例如权限验证、日志记录等。 7. Django Celery:提供了一套分布式任务队列工具,用于异步执行一些耗时的操作,例如发送邮件、生成报表等。 8. Django Channels:提供了一套异步通信工具,用于实现实时应用程序,例如聊天室、在线游戏等。 9. Django Formtools:提供了一套表单处理工具,用于处理复杂的表单场景,例如分步骤提交、多表单提交等。 10. Django Debug Toolbar:提供了一套调试工具条,用于在开发过程中监控请求响应时间、数据库查询等性能指标,帮助开发者进行调试和性能优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值