Python Web框架Django模板入门

创建目录HelloWorld/templates并建立appblog.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
<h1>{{ hello }}</h1>
</body>
</html>

修改文件HelloWorld/HelloWorld/settings.py,修改TEMPLATES中的DIRS[BASE_DIR+"/templates",]

...
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR+"/templates",],       # 修改位置
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]
...

修改文件HelloWorld/HelloWorld/views.py,增加一个新的对象,用于向模板提交数据:

from django.shortcuts import render

def appblog(request):
    context = {}
    context['hello'] = 'http://www.appblog.cn'
    return render(request, 'test.html', context)

配置路由HelloWorld/HelloWorld/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('appblog/', views.appblog),
]
上一篇 Python Web框架Django入门
下一篇 Python Web框架Django模板标签
目录
文章列表
1 React Native学习之仿异步获取网络数据
React Native学习之仿异步获取网络数据
2
Nginx 安装 lua-nginx-module
Nginx 安装 lua-nginx-module
3
SSH免密码登陆时Authentication refused - bad ownership or modes错误解决方法
SSH免密码登陆时Authentication refused - bad ownership or modes错误解决方法
4
在Spring Boot中使用Java线程池ExecutorService
在Spring Boot中使用Java线程池ExecutorService
5
腾讯企业邮箱配置图文教程
腾讯企业邮箱配置图文教程
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。