模板

  • 模板是html页面,可以根据视图中传递的数据填充值
  • 创建模板的目录如下图:

Django入门-4:模板的基本使用

 


  • 修改settings.py文件,设置TEMPLATES的DIRS值
    
    
    1. 'DIRS': [os.path.join(BASE_DIR, 'templates')],

  • 在模板中访问视图传递的数据
    
    
    1. {{输出值,可以是变量,也可以是对象.属性}}
    2. {%执行代码段%}

定义index.html模板


  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>首页</title>
  5. </head>
  6. <body>
  7. <h1>图书列表</h1>
  8. <ul>
  9. {% for book in booklist %}
  10. <li>
  11. <a href="{{ book.id }}">
  12. {{ book.btitle }}
  13. </a>
  14. </li>
  15. {% endfor %}
  16. </ul>
  17. </body>
  18. </html>

定义detail.html模板

  • 在模板中访问对象成员时,都以属性的方式访问,即方法也不能加括号
    
    
    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <title>详细页</title>
    5. </head>
    6. <body>
    7. <h1>{{ book.btitle }}</h1>
    8. <ul>
    9. {% for hero in book.heroinfo_set.all %}
    10. <li>{{ hero.hname }}---{{ hero.hcontent }}</li>
    11. {% endfor %}
    12. </ul>
    13. </body>
    14. </html>

使用模板

  • 编辑views.py文件,在方法中调用模板
    
    
    1. from django.http import HttpResponse
    2. from django.template import RequestContext, loader
    3. from models import BookInfo
    4. def index(request):
    5. booklist = BookInfo.objects.all()
    6. template = loader.get_template('booktest/index.html')
    7. context = RequestContext(request, {'booklist': booklist})
    8. return HttpResponse(template.render(context))
    9. def detail(reqeust, id):
    10. book = BookInfo.objects.get(pk=id)
    11. template = loader.get_template('booktest/detail.html')
    12. context = RequestContext(reqeust, {'book': book})
    13. return HttpResponse(template.render(context))

去除模板的硬编码

  • 在index.html模板中,超链接是硬编码的,此时的请求地址为“127.0.0.1/1/”
    
    
    1. <a href="{{book.id}}">
  • 看如下情况:将urlconf中详细页改为如下,链接就找不到了
    
    
    1. url(r'^book/([0-9]+)/$', views.detail),
  •  此时的请求地址应该为“127.0.0.1/book/1/”
  • Django入门-4:模板的基本使用
     
  • 问题总结:如果在模板中地址硬编码,将来urlconf修改后,地址将失效
  • 解决:使用命名的url设置超链接
  • 修改test1/urls.py文件,设置name
    
    
    1. url(r'^book/([0-9]+)/$', views.detail, name="detail"),
  • 修改index.html模板中的链接
    
    
    1. <a href="{%url 'detail' book.id%}">

URL name详解


  1. 不带参数的:
  2. {% url 'name' %}
  3. 带参数的:参数可以是变量名
  4. {% url 'name' 参数 %}

  • 另外,比如用户收藏夹中收藏的URL是旧的,如何让以前的 /add/3/4/自动跳转到现在新的网址呢?
  • 要知道Django不会帮你做这个,这个需要自己来写一个跳转方法
  • 具体思路是,在 views.py 写一个跳转的函数:
    
    
    1. from django.http import HttpResponseRedirect
    2. from django.core.urlresolvers import reverse # django 1.4.x - django 1.10.x
    3. # from django.urls import reverse # new in django 1.10.x
    4. def old_add2_redirect(request, a, b):
    5. return HttpResponseRedirect(
    6. reverse('add2', args=(a, b))
    7. )
  • urls.py中:
    
    
    1. url(r'^add/(\d+)/(\d+)/$', views.old_add2_redirect),
    2. url(r'^new_add/(\d+)/(\d+)/$', views.add2, name='add2'),

  • 这样,假如用户收藏夹中有 /add/4/5/ ,访问时就会自动跳转到新的 /new_add/4/5/ 了


开始可能觉得直接写网址简单,但是用多了你一定会发现,用“死网址”的方法很糟糕。


Render简写

  • Django提供了函数Render()简化视图调用模板、构造上下文
    
    
    1. from django.shortcuts import render
    2. from models import BookInfo
    3. def index(reqeust):
    4. booklist = BookInfo.objects.all()
    5. return render(reqeust, 'booktest/index.html', {'booklist': booklist})
    6. def detail(reqeust, id):
    7. book = BookInfo.objects.get(pk=id)
    8. return render(reqeust, 'booktest/detail.html', {'book': book})

相关文章:

  • 2022-01-03
  • 2021-06-15
  • 2022-12-23
  • 2021-07-09
  • 2022-12-23
  • 2022-02-07
  • 2021-12-19
猜你喜欢
  • 2021-05-24
  • 2021-11-16
  • 2022-02-20
  • 2021-09-10
  • 2021-10-16
  • 2021-07-04
  • 2021-05-29
相关资源
相似解决方案