首页 > 其他 > 详细

Django基础笔记

时间:2018-09-09 22:56:46      阅读:190      评论:0      收藏:0      [点我收藏+]

模块的使用:
对HTML内容的渲染的模板 :

1.render
  from django.shortcuts import render
  def r(request):
    return render(request,‘index.html‘)
2.render_to_string
  from django.template.loader import render_to_string
  from django.http import HttpResponse
  def r(request):  
    html = render_to_string(‘index.html‘)
    return HttpResponse(html)

  

模板查找路径配置:
  settings.py 下的TEMPLATES配置,包含了模块引擎的配置
  DIRS:一个列表,可以存放所有的模块路径,以后在视图中使用render渲染模板的时候,会在这个列表的路径中查找模板
  APP_DIRS:默认为True,设置为True,会在INSTALLED_APPS的安装了的APP下的templates文件夹中查找模板
  INSTALLED_APPS:配置的作用是类似DIRS作用,让他寻找当前apps下的templates文件下的模块
DTL模块语法:

模块中变量的引用:

模块中变量的引用:
1.直接传递字典

views.py
  def index(request):
    context = {
      ‘username‘ : ‘ziliao‘
     }
    return render(request,‘index.html‘,context=context)
index.html
  {{ username }}

 

2.通过函数方式

views.py
    class Person(object):
    def __init__(self,username):
      self.username = username
  def index(request):
    p = Person(‘ziliao‘)
    context = {
      ‘person‘ : p
    }
    return render(request,‘index.html‘,context=context)
index.py
  { person.username }}

  

3.字典中包含字典

views.py:
  def index(request):
    context = {
      ‘persion‘ : {
      ‘username‘ : ‘ziliao‘
       }
    }
    return render(request,‘index.html‘,context=context)
index.html
  {{ persion.username }}

  

4.字典中包含列表

views.py:
  def index(request):
    context = {
    ‘persion‘ : [
      ‘图书馆‘,
      ‘广州‘
     ]
  }	
index.html
  {{ persion.0 }}	

从上述中,可以得到结论,无论后台传入的参数是字典,对象还是列表等
在前端的调用传入参数,都是通过 xxx.xxx的方式进行调用

标签的使用:if、for、url

if标签:(判断符 < = ... in )

views.py
  def index(request):
    context = {
    ‘age‘: 18
  }
  return render(request,‘index.html‘,context=context)	
index.html
  {% if age < 18 %}
    <p>未成年人</p>
  {% elif age > 18 %}
    <p>成年人</p>
  {% else %}
    <p>18岁骚年</p>
  {% endif %}

 

for标签:

views.py
  def index(request):
  context = {
    ‘books‘:[
    ‘三国演义‘,
    ‘水浒传‘,
    ‘红楼梦‘,
    ]
  }
  return render(request,‘index.html‘,context=context)
index.html
  <ul>
    {% for book in books %} #在books 后加reversed代表翻转输出
      <li>{{ book }}</li>
    {% endfor %}
  </ul>

 

在for循环中,DTL提供一些变量可供使用,变量如下:


forloop.counter:当前循环的下标,以1作为其始值

{% for book in books %}
  <tr>
    <td>{{ forloop.counter }}</td>   #变量的例子
    <td>{{ book.name }}</td>
    <td>{{ book.author }}</td>
    <td>{{ book.price }}</td>
  </tr>
{% endfor %}

  

在for循环中其内容为空:可加 empty

{% for comment in comments %}
  <li>{{ comment }}</li>
{% empty %} 
  <li>没有任何评论</li>
{% endfor %}

 

懵懂

url标签:
views.py
  #登录
  def login(request):
    next = request.GET.get(‘next‘)
    text = ‘登录页面,登录完成后要跳转的url是: %s‘ % next
    return HttpResponse(text)

  #首页
  def index(request):
    return render(request, ‘index.html‘)

  #最火的一篇文章
  def book_detail(request,book_id):
    text = ‘您的图书的id是:%s‘ % book_id
    return HttpResponse(text)
index.html
  <li><a href="{% url ‘city‘ %}">首页</a></li>
  <li><a href="{% url ‘detail‘ book_id=‘1‘ %}">最火的一篇文章</a></li>
  <li><a href="{% url ‘login‘ %}?next=/">登录</a></li>

  

解析:在登录中的url为: http://127.0.0.1:8000/login/?next=/
在最火的一篇文章的url为: http://127.0.0.1:8000/book/detail/1/
两个的不同之处在于,一个从前端获取参数值,一个传递参数值给前端


DTL过滤器:
  在模板中,有时候需要对一些数据进行处理以后才能使用,一般在python中我们是通过函数的形式来实现,
  而在模板中,则通过过滤器来实现,过滤器使用是 | 来使用

add
  将传进来的参数添加到原来的值上面,列表则是添加到一个列表中

views.py
  def index(request):
    context = {
    ‘value1‘:[‘1‘,‘2‘,‘3‘],
    ‘value2‘:[‘4‘,‘5‘,‘3‘]
    }
    return render(request,‘add.html‘, context=context)
index.html
  <p>{{ "1"|add:"2"}}</p>
  <p>{{ value1 | add:value2 }}</p>
结果为:3	
    [‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘]  

  

cut
  移除值中所有指定的字符串,类似于python中的replace(arg,‘‘)

views.py
  def index(request):
    return render(request,‘index.html‘)
index.html
    {{"hello / world"|cut:"/" }}
结果:hello world

 

date

views.py
  from datetime import datetime   def index(request):     context = {     ‘today‘:datetime.now()      }     return render(request,‘index.html‘, context=context) index.html   {{ today|date:"Y/m/d" }} 结果:2018/09/09

  

模板继承

base.html
  <body>
    <li>公共部分</li>
    <li>
      {% block content %}
      {% endblock %}
    </li>
  </body>
index.html
  {% extends ‘base.html‘ %}
  {% block comment %}
    新添加的内容
  {{ block.super }} #继承父模板在 block中写的内容

  {% endblock %}

 

加载静态文件

1.在setting设置 INSTALLED_APPS 添加 ‘django.contrib.staticfiles‘  

#在浏览器中请求静态文件的url
    #127.0.0.1、static/xx.jpg

2.

STATIC_URL = ‘/static/‘ 


绑定在app下的静态文件

3.

在调用某app中的静态文件,需要在 INSTALLED_APPS 中添加 其app名

4.加载静态文件
  

方式1、直接调用
      <img src=‘/static/1.jpg‘ alt=‘‘>
方式2、通过static方式调用
      {% load static %}
      <img src="{% static ‘1.jpg‘ %}" >
方式3、调用另一个app下同名的静态文件
      可以通过在不同app下创建以下目录结构
      --app1
        --static
          --app1	
            1.jpg
      --app2
        --static
          --app2
            1.jpg
      {% load static %}
      <img src="{% static ‘app1/1.jpg‘ %}" >

  

没有和app绑定的静态文件,在和app同级目录下创建static目录

5.重新在setting.py文件下指定配置

 STATICFILES_DIRS = [
   os.path.join(BASE_DIR,‘static‘)
  ]

6.在加载静态文件时,需要头添加{% load static %},如果不想在每一个文件都添加此内容,可以做以下操作   

在setting.py 文件下的TEMPLATES 配置下添加 ,可以把static添加到django内置中
    ‘builtins‘:[‘django.templatetags.static‘]

  




Django基础笔记

原文:https://www.cnblogs.com/tang-s/p/9615620.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!