Django-2-模板

path() 函数

Django path() 可以接收四个参数,分别是两个必选参数:route、view 和两个可选参数:kwargs、name。
语法格式:

path(route, view, kwargs=None, name=None)

route: 字符串,表示 URL 规则,与之匹配的 URL 会执行对应的第二个参数 view。
view: 用于执行与正则表达式匹配的 URL 请求。
kwargs: 视图使用的字典类型的参数。
name: 用来反向获取 URL。

Django模板

1)

在 learn目录下新建templates 文件夹,里面新建home.html

在这里插入图片描述
在 home.html 中写一些内容

<!DOCTYPE html>
<html>
	<head>
	    <title>欢迎光临</title>
	</head>
	<body>
		欢迎光临自强学堂
	</body>
</html>

views.py

from django.http import HttpResponse
from django.shortcuts import render 
 
def index(request):
    return render(request, 'home.html')

将视图函数对应到网址blog/urls.py


from django.contrib import admin
from django.urls import path
from learn import views as learn_views
urlpatterns = [
    path('home/', learn_views.index, name='home'),
    path('admin/', admin.site.urls),
]

访问http://127.0.0.1:8000/home

在这里插入图片描述

2)

在templates目录中创建list1.html文件:

<html>
	<title>Main</title>
	<body>
		<h1> The Zen of Python</h1>
	<hr/>
	{% for item in entries1 %}
	   <b> {
  { item.title  }}  </b> 
	<hr/>
	  {
  { item.content  }} 
	<p/>
	{% endfor %} 
	</body>
</html>

修改 learn/views.py

增加:

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

entries={
   'title':'1234','content':'5678'}
entries1=[
    {
   'title':'English','content':'Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren’t special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one– and preferably only one –obvious way to do it. Although that way may not be obvious at first unless you’re Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it’s a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea – let’s do more of those!'
     },
    {
   'title':'中文','content':'优美胜于丑陋 明了胜于隐晦 简洁胜于复杂 复杂胜于混乱 扁平胜于嵌套 宽松胜于紧凑 可读性很重要 即便是特例,也不可违背这些规则 虽然现实往往不那么完美 但是不应该放过任何异常 除非你确定需要如此 如果存在多种可能,不要猜测 肯定有一种——通常也是唯一一种——最佳的解决方案 虽然这并不容易,因为你不是Python之父 , 动手比不动手要好 ,但不假思索就动手还不如不做, 如果你的方案很难懂,那肯定不是一个好方案 如果你的方案很好懂,那肯定是一个好方案, 命名空间非常有用,我们应当多加利用'
     }
]

def list(request):
    return render(request,'list1.html',{
   'entries1':entries1})

修改blog/blog/urls.py

打开 blog/blog/urls.py 这个文件, 修改其中的代码:

from django.contrib import admin
from django.urls import path
from learn import views as learn_views  # new

urlpatterns = [
    path('', learn_views.index),  # new
    path('list/', learn_views.list),   
    path('admin/', admin.site.urls),
]

访问 http://127.0.0.1:8001/list/

在这里插入图片描述

网站模板的设计

网站模板的设计,一般的,网站有一些通用的部分,比如 导航,底部,访问统计代码等等:nav.html, bottom.html, tongji.html

写一个 base.html 来包含通用文件(include)

<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}默认标题{% endblock %} - 自强学堂</title>
</head>
<body> 
{% include 'nav.html' %} 

{% block content %}
<div>这里是默认内容,所有继承自这个模板的,如果不覆盖就显示这里的默认内容。</div>
{% endblock %}
 
{% include 'bottom.html' %} 
{% include 'tongji.html' %} 
</body>
</html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值